Class: PayWithAmazon::Login

Inherits:
Object
  • Object
show all
Defined in:
lib/pay_with_amazon/login.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id, region: :na, sandbox: false) ⇒ Login

Returns a new instance of Login.

Parameters:

  • client_id (String)


23
24
25
26
27
28
29
# 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

Instance Attribute Details

#client_idObject

Returns the value of attribute client_id.



18
19
20
# File 'lib/pay_with_amazon/login.rb', line 18

def client_id
  @client_id
end

#regionObject (readonly)

Returns the value of attribute region.



16
17
18
# File 'lib/pay_with_amazon/login.rb', line 16

def region
  @region
end

#sandboxObject

Returns the value of attribute sandbox.



18
19
20
# File 'lib/pay_with_amazon/login.rb', line 18

def sandbox
  @sandbox
end

Instance Method Details

#get_login_profile(access_token) ⇒ Object

This method will validate the access token and return the user’s profile information.

Parameters:

  • access_token (String)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/pay_with_amazon/login.rb', line 34

def (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)
   = JSON.parse(response.body)
  return 
end