Class: CityPayApiClient::ApiKey

Inherits:
Object
  • Object
show all
Defined in:
lib/citypay_api_client/models/api_key.rb

Instance Method Summary collapse

Constructor Details

#initialize(client_id:, licence_key:) ⇒ ApiKey

Returns a new instance of ApiKey.



8
9
10
11
12
# File 'lib/citypay_api_client/models/api_key.rb', line 8

def initialize(client_id:, licence_key:)
  @client_id, @licence_key = client_id, licence_key
  @nonce = Random.new().bytes(16).bytes
  @datetime = DateTime.now.new_offset(0)
end

Instance Method Details

#client_idObject



14
15
16
# File 'lib/citypay_api_client/models/api_key.rb', line 14

def client_id
  @client_id
end

#datetime=(datetime) ⇒ Object



27
28
29
# File 'lib/citypay_api_client/models/api_key.rb', line 27

def datetime=(datetime)
  @datetime = datetime
end

#generateObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/citypay_api_client/models/api_key.rb', line 35

def generate

  if @client_id.nil?
    raise 'ClientId is not defined'
  end

  if @licence_key.nil?
    raise 'LicenceKey is not defined'
  end

  if @datetime.nil?
    raise 'Datetime is not defined'
  end

  if @nonce.nil?
    raise 'Nonce is not defined'
  end

  ds = @datetime.strftime("%Y%m%d%H%M")
  message = []
  message.push(*@client_id.bytes)
  message.push(*@nonce)
  message.push(*hex_to_bytes(ds))
  # message = message.join
  digest = OpenSSL::HMAC.digest('sha256', @licence_key, message.pack("c*"))

  dest = []
  dest.push(*@client_id.bytes)
  dest.push(*"\x3A".bytes)
  dest.push(*@nonce.each_entry.map { |b| '%02X' % (b & 0xFF) }.join.upcase.bytes)
  dest.push(*"\x3A".bytes)
  dest.push(*digest.bytes)
  Base64.strict_encode64(dest.pack("c*"))

end

#nonce=(nonce) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/citypay_api_client/models/api_key.rb', line 18

def nonce=(nonce)
  if nonce.is_a? String
    @nonce = nonce.scan(/../).map { |x| x.hex.chr }.join.bytes
  else
    @nonce = nonce
  end

end