class SebElink::Gateway

Attributes

defaults[R]
privkey[R]

Public Class Methods

new(privkey, defaults={}) click to toggle source
# File lib/seb_elink/gateway.rb, line 6
def initialize(privkey, defaults={})
  @privkey = privkey
  @defaults = SebElink::DEFAULTS.merge(defaults)
end

Public Instance Methods

ibank_api_uri() click to toggle source
# File lib/seb_elink/gateway.rb, line 11
def ibank_api_uri
  @ibank_api_uri ||= defaults[:IBANK_API_URI]
end
produce_footprint(options) click to toggle source

options: {

message_code: "000x",
version: "00x"
skip_validation: false, # true for from-SEB messages like 0003 and 0004
data: {
  IB_SND_ID: "TESTACC",
  ...
}

}

# File lib/seb_elink/gateway.rb, line 24
def produce_footprint(options)
  data_hash = options[:data]
  spec_set = spec_for(version: options[:version], message_code: options[:message_code])

  footprint_string = spec_set.map do |field, spec|
    next unless spec[:in_signature]

    unless options[:skip_validation]
      # 1. validate each field's length in .bytesize and format
      raise_nil_error(field) if data_hash[field] == nil
      raise_length_error(field) if data_hash[field].to_s.bytesize > spec[:max_length]
      raise_format_error(field) if !data_hash[field].to_s[spec[:format]]
    end

    # 2. build the 'len(p1)||p1..' string
    "#{data_hash[field].to_s.bytesize.to_s.rjust(3, "0")}#{data_hash[field]}"
  end.join("")
end
sign(options) click to toggle source

options: {

version: "00x",
message_footprint: "001a.."

}

# File lib/seb_elink/gateway.rb, line 55
def sign(options)
  Base64.encode64(
    privkey_rsa.sign(
      send("v#{options[:version]}_digest"), #=> digest algorythm, SHA1
      options[:message_footprint]
    )
  )
end
spec_for(options) click to toggle source

options: {

version: "00x",
message_code: "000x"

}

# File lib/seb_elink/gateway.rb, line 47
def spec_for(options)
  send(:class).const_get("V#{options[:version]}_MESSAGE#{options[:message_code]}_SPEC")
end
verify(options) click to toggle source

options: {

version: "00x",
message:,
base64_signature:
# OR
signature:

}

# File lib/seb_elink/gateway.rb, line 71
def verify(options)
  received_binary_signature = options[:signature] ||
    Base64.decode64(options[:base64_signature])

  ibank_pubkey_rsa.verify(
    send("v#{options[:version]}_digest"),
    received_binary_signature,
    options[:message]
  )
end

Private Instance Methods

ibank_pubkey_rsa() click to toggle source
# File lib/seb_elink/gateway.rb, line 91
def ibank_pubkey_rsa
  @ibank_pubkey_rsa ||= OpenSSL::X509::Certificate.new(defaults[:IBANK_CERT]).public_key
end
privkey_rsa() click to toggle source
# File lib/seb_elink/gateway.rb, line 87
def privkey_rsa
  @privkey_rsa ||= OpenSSL::PKey::RSA.new(privkey)
end
raise_format_error(field) click to toggle source
# File lib/seb_elink/gateway.rb, line 99
def raise_format_error(field)
  raise ArgumentError.new("#{field} value format does not match the spec")
end
raise_length_error(field) click to toggle source
# File lib/seb_elink/gateway.rb, line 95
def raise_length_error(field)
  raise ArgumentError.new("#{field} value is too long")
end
raise_nil_error(field) click to toggle source
# File lib/seb_elink/gateway.rb, line 103
def raise_nil_error(field)
  raise ArgumentError.new("#{field} key is absent")
end
v001_digest() click to toggle source
# File lib/seb_elink/gateway.rb, line 83
def v001_digest
  OpenSSL::Digest::SHA1.new
end