class PayWithAmazon::Login

Login with Amazon API

This class allows you to obtain user profile information once a user has logged into your application using their Amazon credentials.

Attributes

client_id[RW]
region[R]
sandbox[RW]

Public Class Methods

new(client_id, region: :na, sandbox: false) click to toggle source

@param client_id [String] @optional region [Symbol] Default: :na @optional sandbox [Boolean] Default: false

# File lib/pay_with_amazon/login.rb, line 23
def initialize(client_id, region: :na, sandbox: false)
  @client_id = client_id
  @region = region
  @endpoint = region_hash[@region]
  @sandbox = sandbox
  @sandbox_str = @sandbox ? "api.sandbox" : "api"
end

Public Instance Methods

get_login_profile(access_token) click to toggle source

This method will validate the access token and return the user's profile information. @param access_token [String]

# File lib/pay_with_amazon/login.rb, line 34
def get_login_profile(access_token)
  decoded_access_token = URI.decode(access_token)
  encoded_access_token = URI.encode(decoded_access_token)
  uri = URI("https://#{@sandbox_str}.#{@endpoint}/auth/o2/tokeninfo?access_token=#{encoded_access_token}")
  req = Net::HTTP::Get.new(uri.request_uri)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  response = http.request(req)
  decode = JSON.parse(response.body)

  if decode['aud'] != @client_id
    raise "Invalid Access Token"
  end

  uri = URI.parse("https://#{@sandbox_str}.#{@endpoint}/user/profile")
  req = Net::HTTP::Get.new(uri.request_uri)
  req['Authorization'] = "bearer " + decoded_access_token
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  response = http.request(req)
  decoded_login_profile = JSON.parse(response.body)
  return decoded_login_profile
end

Private Instance Methods

region_hash() click to toggle source
# File lib/pay_with_amazon/login.rb, line 62
def region_hash
  {
    :jp => 'amazon.co.jp',
    :uk => 'amazon.co.uk',
    :de => 'amazon.de',
    :eu => 'amazon.co.uk',
    :us => 'amazon.com',
    :na => 'amazon.com'
  }
end