Class: R2D2::AndroidPayToken

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/r2d2/android_pay_token.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#decrypt_message, #derive_hkdf_keys, #generate_shared_secret, #to_length_value, #verify_mac

Constructor Details

#initialize(token_attrs) ⇒ AndroidPayToken

Returns a new instance of AndroidPayToken.



7
8
9
10
11
# File 'lib/r2d2/android_pay_token.rb', line 7

def initialize(token_attrs)
  self.ephemeral_public_key = token_attrs["ephemeralPublicKey"]
  self.tag = token_attrs["tag"]
  self.encrypted_message = token_attrs["encryptedMessage"]
end

Instance Attribute Details

#encrypted_messageObject

Returns the value of attribute encrypted_message.



5
6
7
# File 'lib/r2d2/android_pay_token.rb', line 5

def encrypted_message
  @encrypted_message
end

#ephemeral_public_keyObject

Returns the value of attribute ephemeral_public_key.



5
6
7
# File 'lib/r2d2/android_pay_token.rb', line 5

def ephemeral_public_key
  @ephemeral_public_key
end

#tagObject

Returns the value of attribute tag.



5
6
7
# File 'lib/r2d2/android_pay_token.rb', line 5

def tag
  @tag
end

Instance Method Details

#decrypt(private_key_pem) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/r2d2/android_pay_token.rb', line 13

def decrypt(private_key_pem)
  private_key = OpenSSL::PKey::EC.new(private_key_pem)

  shared_secret = generate_shared_secret(private_key, ephemeral_public_key)

  # derive the symmetric_encryption_key and mac_key
  hkdf_keys = derive_hkdf_keys(ephemeral_public_key, shared_secret, 'Android')

  # verify the tag is a valid value
  verify_mac(hkdf_keys[:mac_key], encrypted_message, tag)

  JSON.parse(decrypt_message(encrypted_message, hkdf_keys[:symmetric_encryption_key]))
end