Module: As2

Defined in:
lib/as2.rb,
lib/as2/client.rb,
lib/as2/config.rb,
lib/as2/parser.rb,
lib/as2/server.rb,
lib/as2/message.rb,
lib/as2/version.rb,
lib/as2/base64_helper.rb,
lib/as2/client/result.rb,
lib/as2/mime_generator.rb,
lib/as2/digest_selector.rb,
lib/as2/parser/disposition_notification_options.rb

Defined Under Namespace

Modules: Base64Helper, Config, Parser Classes: Client, DigestSelector, Message, MimeGenerator, Server

Constant Summary collapse

VERSION =
"0.12.0"

Class Method Summary collapse

Class Method Details

.base64_encode(content, scheme: 'rfc4648') ⇒ String

create a base64 string from content, based on the given encoding scheme

Parameters:

  • content (String)
  • scheme (String) (defaults to: 'rfc4648')

    one of As2.valid_base64_schemes

Returns:

  • (String)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/as2.rb', line 38

def self.base64_encode(content, scheme: 'rfc4648')
  case scheme.to_s
  when 'rfc2045'
    # "This method complies with RFC 2045."
    # https://ruby-doc.org/stdlib-3.0.4/libdoc/base64/rdoc/Base64.html#method-i-encode64
    # https://www.rfc-editor.org/rfc/rfc2045#section-6.8
    then Base64.encode64(content)
  when 'rfc4648'
    # "This method complies with RFC 4648."
    # https://ruby-doc.org/stdlib-3.0.4/libdoc/base64/rdoc/Base64.html#method-i-strict_encode64
    # https://www.rfc-editor.org/rfc/rfc4648#section-4
    then Base64.strict_encode64(content)
  else
    raise ArgumentError, "unsupported scheme '#{scheme}'. choose one of: #{valid_base64_schemes}"
  end
end

.canonicalize_line_endings(content) ⇒ String

canonicalize all line endings in the given text.

"\n" becomes "\r\n"

“rn” remains “rn”

Conversion to canonical form:
The entire body ... is converted to a universal canonical
form. ... For example, in the case of text/plain data, the text
must be converted to a supported character set and lines must
be delimited with CRLF delimiters in accordance with RFC 822.

www.rfc-editor.org/rfc/rfc2049#page-9

Parameters:

  • content (String)

Returns:

  • (String)

    content, but with all bare n replaced by rn



70
71
72
# File 'lib/as2.rb', line 70

def self.canonicalize_line_endings(content)
  content.gsub(/(?<!\r)\n/, "\r\n")
end

.choose_mic_algorithm(disposition_notification_options) ⇒ String?

Select which algorithm to use for calculating a MIC, based on preferences stated by sender & our list of available algorithms.

Parameters:

  • disposition_notification_options (String)

    The content of an HTTP Disposition-Notification-Options header

Returns:

  • (String, nil)

    either an algorithm name, or nil if none is found in given header

See Also:



82
83
84
85
# File 'lib/as2.rb', line 82

def self.choose_mic_algorithm(disposition_notification_options)
  parsed = As2::Parser::DispositionNotificationOptions.parse(disposition_notification_options)
  Array(parsed['signed-receipt-micalg']).find { |m| As2::DigestSelector.valid?(m) }
end

.configure(&block) ⇒ Object



14
15
16
# File 'lib/as2.rb', line 14

def self.configure(&block)
  Config.configure(&block)
end

.generate_message_id(server_info) ⇒ Object



22
23
24
# File 'lib/as2.rb', line 22

def self.generate_message_id(server_info)
  "<#{server_info.name}-#{Time.now.strftime('%Y%m%d-%H%M%S')}-#{SecureRandom.uuid}@#{server_info.domain}>"
end

.quoted_system_identifier(name) ⇒ Object

surround an As2-From/As2-To value with double-quotes, if it contains a space.



88
89
90
91
92
93
94
# File 'lib/as2.rb', line 88

def self.quoted_system_identifier(name)
  if name.to_s.include?(' ') && !name.to_s.start_with?('"')
    "\"#{name}\""
  else
    name
  end
end

.reset_config!Object



18
19
20
# File 'lib/as2.rb', line 18

def self.reset_config!
  Config.reset!
end

.unquoted_system_identifier(name) ⇒ Object

remove double-quotes from an As2-From/As2-To value, if it contains any. this is useful in client code which may not automatically strip these quotes from a header value.



98
99
100
101
102
103
104
# File 'lib/as2.rb', line 98

def self.unquoted_system_identifier(name)
  if !name.is_a?(String)
    return name
  end

  name.delete_prefix('"').delete_suffix('"').gsub('\"', '"')
end

.valid_base64_schemesObject



26
27
28
29
30
31
# File 'lib/as2.rb', line 26

def self.valid_base64_schemes
  [
    'rfc2045',
    'rfc4648'
  ]
end