Class: SebElink::Gateway

Inherits:
Object
  • Object
show all
Includes:
MessageSpecs
Defined in:
lib/seb_elink/gateway.rb

Constant Summary

Constants included from MessageSpecs

MessageSpecs::V001_MESSAGE0002_SPEC, MessageSpecs::V001_MESSAGE0003_SPEC, MessageSpecs::V001_MESSAGE0004_SPEC

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(privkey, defaults = {}) ⇒ Gateway

Returns a new instance of Gateway.



6
7
8
9
# File 'lib/seb_elink/gateway.rb', line 6

def initialize(privkey, defaults={})
  @privkey = privkey
  @defaults = SebElink::DEFAULTS.merge(defaults)
end

Instance Attribute Details

#defaultsObject (readonly)

Returns the value of attribute defaults.



4
5
6
# File 'lib/seb_elink/gateway.rb', line 4

def defaults
  @defaults
end

#privkeyObject (readonly)

Returns the value of attribute privkey.



4
5
6
# File 'lib/seb_elink/gateway.rb', line 4

def privkey
  @privkey
end

Instance Method Details

#ibank_api_uriObject



11
12
13
# File 'lib/seb_elink/gateway.rb', line 11

def ibank_api_uri
  @ibank_api_uri ||= defaults[:IBANK_API_URI]
end

#produce_footprint(options) ⇒ Object

options: {

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

}



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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) ⇒ Object

options:

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



55
56
57
58
59
60
61
62
# 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) ⇒ Object

options:

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



47
48
49
# 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) ⇒ Object

options:

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



71
72
73
74
75
76
77
78
79
80
# 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