class CityPayApiClient::ApiKey
Public Class Methods
new(client_id:, licence_key:)
click to toggle source
# 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
Public Instance Methods
client_id()
click to toggle source
# File lib/citypay_api_client/models/api_key.rb, line 14 def client_id @client_id end
datetime=(datetime)
click to toggle source
# File lib/citypay_api_client/models/api_key.rb, line 27 def datetime=(datetime) @datetime = datetime end
generate()
click to toggle source
# 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)
click to toggle source
# 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
Private Instance Methods
hex_to_bytes(str)
click to toggle source
# File lib/citypay_api_client/models/api_key.rb, line 31 def hex_to_bytes(str) str.scan(/../).map { |x| x.hex.chr }.join.bytes end