Class: AtPay::Tokenator

Inherits:
Object
  • Object
show all
Defined in:
lib/atpay/tokenator.rb

Constant Summary collapse

TARGETS =
{
  /url<(.*?)>/ => [:@url, 6],
  /card<(.*?)>/ => [:@card, 7],
  /email<(.*?)>/ => [:@email, 8],
  /member<(.*?)>/ => [:@member, 9]
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, session = nil) ⇒ Tokenator

A bit clunky but useful for testing token decomposition. If you provide a test session then the config values there will be used so that decryption will function without @Pay’s private key.



26
27
28
29
30
31
32
33
34
# File 'lib/atpay/tokenator.rb', line 26

def initialize(token, session = nil)
  trim_tail token
  @token = token
  @session = session
  @version = Tokenator.token_version(token)
  strip_version

  @checksum = Digest::SHA1.hexdigest(token) # Before or after removing version?
end

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



3
4
5
# File 'lib/atpay/tokenator.rb', line 3

def amount
  @amount
end

#expiresObject (readonly)

Returns the value of attribute expires.



3
4
5
# File 'lib/atpay/tokenator.rb', line 3

def expires
  @expires
end

#groupObject (readonly)

Returns the value of attribute group.



3
4
5
# File 'lib/atpay/tokenator.rb', line 3

def group
  @group
end

#ipObject (readonly)

Returns the value of attribute ip.



3
4
5
# File 'lib/atpay/tokenator.rb', line 3

def ip
  @ip
end

#partner_idObject (readonly)

Returns the value of attribute partner_id.



3
4
5
# File 'lib/atpay/tokenator.rb', line 3

def partner_id
  @partner_id
end

#site_frameObject (readonly)

Returns the value of attribute site_frame.



3
4
5
# File 'lib/atpay/tokenator.rb', line 3

def site_frame
  @site_frame
end

#sourceObject (readonly)

Returns the value of attribute source.



3
4
5
# File 'lib/atpay/tokenator.rb', line 3

def source
  @source
end

#tokenObject (readonly)

Returns the value of attribute token.



3
4
5
# File 'lib/atpay/tokenator.rb', line 3

def token
  @token
end

#user_dataObject (readonly)

Returns the value of attribute user_data.



3
4
5
# File 'lib/atpay/tokenator.rb', line 3

def user_data
  @user_data
end

#versionObject (readonly)

Returns the value of attribute version.



3
4
5
# File 'lib/atpay/tokenator.rb', line 3

def version
  @version
end

Class Method Details

.find_by_checksum(token) ⇒ Object

Check and make sure we haven’t seen this token before. NOTE: This is really for internal use by @Pay.



44
45
46
47
48
# File 'lib/atpay/tokenator.rb', line 44

def find_by_checksum(token)
  checksum = Digest::SHA1.hexdigest(token)

  SecurityKey.find_by_encoded_key(checksum) || ValidationToken.find_by_encoded_key(checksum)
end

.token_version(token) ⇒ Object

Get the version of the given token.



38
39
40
# File 'lib/atpay/tokenator.rb', line 38

def token_version(token)
  token.scan('-').empty? ? 0 : unpack_version(token.split('-')[0])
end

Instance Method Details

#body(key) ⇒ Object

Here we parse the body of the token. All the useful shit comes out of this.



71
72
73
74
# File 'lib/atpay/tokenator.rb', line 71

def body(key)
  payload(nonce, key, @token)
  part_out_payload
end

#browser_data(key) ⇒ Object

With a site token you want to call this after header. It will pull the ip address and header sha out of the token.



78
79
80
81
82
83
84
# File 'lib/atpay/tokenator.rb', line 78

def browser_data(key)
  length = @token.slice!(0, 4).unpack("l>")[0]
  @site_frame = boxer(key).open(nonce, @token.slice!(0, length))

  length = @token.slice!(0, 4).unpack("l>")[0]
  @ip = @token.slice!(0, length)
end

#headerObject

We want to pull the header out of the token. This means we grab the nonce and the partner id from the token. The version frame should be removed before calling header.



63
64
65
66
67
# File 'lib/atpay/tokenator.rb', line 63

def header
  decode
  nonce
  destination
end

#to_hObject

Return parts in a hash structure, handy for ActiveRecord.



91
92
93
94
95
96
97
98
# File 'lib/atpay/tokenator.rb', line 91

def to_h
  {
    sale_price: @amount,
    expires_at: Time.at(Time.now + @expires),
    group: @group,
    encoded_key: @checksum
  }
end