Class: PaypalServerSdk::OAuth2
- Inherits:
-
CoreLibrary::HeaderAuth
- Object
- CoreLibrary::HeaderAuth
- PaypalServerSdk::OAuth2
- Includes:
- CoreLibrary
- Defined in:
- lib/paypal_server_sdk/http/auth/o_auth2.rb
Overview
Utility class for OAuth 2 authorization and token management.
Instance Method Summary collapse
- #apply(http_request) ⇒ Object
-
#build_basic_auth_header ⇒ String
Builds the basic auth header for endpoints in the OAuth Authorization Controller.
-
#error_message ⇒ Object
Display error message on occurrence of authentication failure.
-
#fetch_token(additional_params: nil) ⇒ OAuthToken
Fetches the token.
-
#initialize(client_credentials_auth_credentials, config) ⇒ OAuth2
constructor
Initialization constructor.
-
#token_expired?(token) ⇒ Boolean
Checks if OAuth token has expired.
-
#valid ⇒ Boolean
Validates the oAuth token.
Constructor Details
#initialize(client_credentials_auth_credentials, config) ⇒ OAuth2
Initialization constructor.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/paypal_server_sdk/http/auth/o_auth2.rb', line 17 def initialize(client_credentials_auth_credentials, config) @_o_auth_client_id = client_credentials_auth_credentials.o_auth_client_id unless client_credentials_auth_credentials.nil? || client_credentials_auth_credentials.o_auth_client_id.nil? @_o_auth_client_secret = client_credentials_auth_credentials.o_auth_client_secret unless client_credentials_auth_credentials.nil? || client_credentials_auth_credentials.o_auth_client_secret.nil? @_o_auth_token = client_credentials_auth_credentials.o_auth_token unless client_credentials_auth_credentials.nil? || client_credentials_auth_credentials.o_auth_token.nil? @_o_auth_clock_skew = client_credentials_auth_credentials.o_auth_clock_skew unless client_credentials_auth_credentials.nil? || client_credentials_auth_credentials.o_auth_clock_skew.nil? @_o_auth_token_provider = client_credentials_auth_credentials.o_auth_token_provider unless client_credentials_auth_credentials.nil? || client_credentials_auth_credentials.o_auth_token_provider.nil? @_o_auth_on_token_update = client_credentials_auth_credentials.o_auth_on_token_update unless client_credentials_auth_credentials.nil? || client_credentials_auth_credentials.o_auth_on_token_update.nil? @_o_auth_api = OAuthAuthorizationController.new(config) super({}) end |
Instance Method Details
#apply(http_request) ⇒ Object
70 71 72 73 |
# File 'lib/paypal_server_sdk/http/auth/o_auth2.rb', line 70 def apply(http_request) auth_params = { 'Authorization' => "Bearer #{@_o_auth_token.access_token}" } AuthHelper.apply(auth_params, http_request.method(:add_header)) end |
#build_basic_auth_header ⇒ String
Builds the basic auth header for endpoints in the OAuth Authorization Controller.
43 44 45 |
# File 'lib/paypal_server_sdk/http/auth/o_auth2.rb', line 43 def build_basic_auth_header "Basic #{AuthHelper.get_base64_encoded_value(@_o_auth_client_id, @_o_auth_client_secret)}" end |
#error_message ⇒ Object
Display error message on occurrence of authentication failure.
12 13 14 |
# File 'lib/paypal_server_sdk/http/auth/o_auth2.rb', line 12 def 'ClientCredentialsAuth: OAuthToken is undefined or expired.' end |
#fetch_token(additional_params: nil) ⇒ OAuthToken
Fetches the token.
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/paypal_server_sdk/http/auth/o_auth2.rb', line 50 def fetch_token(additional_params: nil) token = @_o_auth_api.request_token( { 'authorization' => build_basic_auth_header }, _field_parameters: additional_params ).data if token.respond_to?('expires_in') && !token.expires_in.nil? token.expiry = AuthHelper.get_token_expiry(token.expires_in, Time.now.utc.to_i) end token end |
#token_expired?(token) ⇒ Boolean
Checks if OAuth token has expired.
66 67 68 |
# File 'lib/paypal_server_sdk/http/auth/o_auth2.rb', line 66 def token_expired?(token) token.respond_to?('expiry') && AuthHelper.token_expired?(token.expiry, @_o_auth_clock_skew) end |
#valid ⇒ Boolean
Validates the oAuth token.
36 37 38 39 |
# File 'lib/paypal_server_sdk/http/auth/o_auth2.rb', line 36 def valid @_o_auth_token = get_token_from_provider @_o_auth_token.is_a?(OAuthToken) && !token_expired?(@_o_auth_token) end |