Class: Sorcery::Providers::Paypal

Inherits:
Base
  • Object
show all
Includes:
Sorcery::Protocols::Oauth2
Defined in:
lib/sorcery/providers/paypal.rb

Overview

This class adds support for OAuth with paypal.com.

config.paypal.key = <key>
config.paypal.secret = <secret>
...

Instance Attribute Summary collapse

Attributes inherited from Base

#access_token, #callback_url, #key, #original_callback_url, #secret, #site, #state, #user_info_mapping

Instance Method Summary collapse

Methods included from Sorcery::Protocols::Oauth2

#authorize_url, #build_client, #oauth_version

Methods inherited from Base

#auth_hash, descendants, #has_callback?, name

Constructor Details

#initializePaypal

Returns a new instance of Paypal.



14
15
16
17
18
19
20
21
22
23
# File 'lib/sorcery/providers/paypal.rb', line 14

def initialize
  super

  @scope           = 'openid email'
  @site            = 'https://api.paypal.com'
  @auth_url        = 'https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize'
  @user_info_url   = 'https://api.paypal.com/v1/identity/openidconnect/userinfo?schema=openid'
  @token_url       = 'https://api.paypal.com/v1/identity/openidconnect/tokenservice'
  @state           = SecureRandom.hex(16)
end

Instance Attribute Details

#auth_urlObject

Returns the value of attribute auth_url.



12
13
14
# File 'lib/sorcery/providers/paypal.rb', line 12

def auth_url
  @auth_url
end

#scopeObject

Returns the value of attribute scope.



12
13
14
# File 'lib/sorcery/providers/paypal.rb', line 12

def scope
  @scope
end

#token_urlObject

Returns the value of attribute token_url.



12
13
14
# File 'lib/sorcery/providers/paypal.rb', line 12

def token_url
  @token_url
end

#user_info_urlObject

Returns the value of attribute user_info_url.



12
13
14
# File 'lib/sorcery/providers/paypal.rb', line 12

def 
  @user_info_url
end

Instance Method Details

#get_access_token(args, options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sorcery/providers/paypal.rb', line 35

def get_access_token(args, options = {})
  client = build_client(options)
  client.auth_code.get_token(
    args[:code],
    {
      redirect_uri: @callback_url,
      parse: options.delete(:parse)
    },
    options
  )
end

#get_user_hash(access_token) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/sorcery/providers/paypal.rb', line 25

def get_user_hash(access_token)
  response = access_token.get()
  body = JSON.parse(response.body)
  auth_hash(access_token).tap do |h|
    h[:user_info] = body
    h[:uid] = body['user_id']
    h[:email] = body['email']
  end
end

#login_url(_params, _session) ⇒ Object



47
48
49
# File 'lib/sorcery/providers/paypal.rb', line 47

def (_params, _session)
  authorize_url(authorize_url: auth_url)
end

#process_callback(params, _session) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/sorcery/providers/paypal.rb', line 51

def process_callback(params, _session)
  args = {}.tap do |a|
    a[:code] = params[:code] if params[:code]
  end

  get_access_token(args, token_url: token_url, token_method: :post)
end