Class: MultiAuthSample::OAuthROPCG
- Inherits:
-
CoreLibrary::HeaderAuth
- Object
- CoreLibrary::HeaderAuth
- MultiAuthSample::OAuthROPCG
- Defined in:
- lib/multi_auth_sample/http/auth/o_auth_ropcg.rb
Overview
Utility class for OAuth 2 authorization and token management.
Instance Method Summary collapse
-
#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(o_auth_ropcg_credentials, config) ⇒ OAuthROPCG
constructor
Initialization constructor.
-
#refresh_token(additional_params: nil) ⇒ OAuthToken
Refreshes OAuth token.
-
#token_expired?(token) ⇒ Boolean
Checks if OAuth token has expired.
-
#valid ⇒ Boolean
Validates the oAuth token.
Constructor Details
#initialize(o_auth_ropcg_credentials, config) ⇒ OAuthROPCG
Initialization constructor.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/multi_auth_sample/http/auth/o_auth_ropcg.rb', line 16 def initialize(o_auth_ropcg_credentials, config) auth_params = {} @_o_auth_client_id = o_auth_ropcg_credentials.o_auth_client_id unless o_auth_ropcg_credentials.nil? || o_auth_ropcg_credentials.o_auth_client_id.nil? @_o_auth_client_secret = o_auth_ropcg_credentials.o_auth_client_secret unless o_auth_ropcg_credentials.nil? || o_auth_ropcg_credentials.o_auth_client_secret.nil? @_o_auth_username = o_auth_ropcg_credentials.o_auth_username unless o_auth_ropcg_credentials.nil? || o_auth_ropcg_credentials.o_auth_username.nil? @_o_auth_password = o_auth_ropcg_credentials.o_auth_password unless o_auth_ropcg_credentials.nil? || o_auth_ropcg_credentials.o_auth_password.nil? @_o_auth_token = o_auth_ropcg_credentials.o_auth_token unless o_auth_ropcg_credentials.nil? || o_auth_ropcg_credentials.o_auth_token.nil? @_o_auth_api = OAuthAuthorizationController.new(config) auth_params['Authorization'] = "Bearer #{@_o_auth_token.access_token}" unless @_o_auth_token.nil? super auth_params end |
Instance Method Details
#build_basic_auth_header ⇒ String
Builds the basic auth header for endpoints in the OAuth Authorization Controller.
42 43 44 |
# File 'lib/multi_auth_sample/http/auth/o_auth_ropcg.rb', line 42 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.
11 12 13 |
# File 'lib/multi_auth_sample/http/auth/o_auth_ropcg.rb', line 11 def 'OAuthROPCG: OAuthToken is undefined or expired.' end |
#fetch_token(additional_params: nil) ⇒ OAuthToken
Fetches the token.
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/multi_auth_sample/http/auth/o_auth_ropcg.rb', line 49 def fetch_token(additional_params: nil) token = @_o_auth_api.request_token_o_auth_ropcg( build_basic_auth_header, @_o_auth_username, @_o_auth_password, _field_parameters: additional_params ) 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 |
#refresh_token(additional_params: nil) ⇒ OAuthToken
Refreshes OAuth token.
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/multi_auth_sample/http/auth/o_auth_ropcg.rb', line 72 def refresh_token(additional_params: nil) token = @_o_auth_api.refresh_token_o_auth_ropcg( OAuthROPCG.build_basic_auth_header, @_o_auth_token.refresh_token, _field_parameters: additional_params ) 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.
65 66 67 |
# File 'lib/multi_auth_sample/http/auth/o_auth_ropcg.rb', line 65 def token_expired?(token) token.respond_to?('expiry') && AuthHelper.token_expired?(token.expiry) end |
#valid ⇒ Boolean
Validates the oAuth token.
36 37 38 |
# File 'lib/multi_auth_sample/http/auth/o_auth_ropcg.rb', line 36 def valid !@_o_auth_token.nil? && !token_expired?(@_o_auth_token) end |