Class: AdsCommon::Auth::OAuth2JwtHandler
- Inherits:
-
BaseHandler
- Object
- BaseHandler
- AdsCommon::Auth::OAuth2JwtHandler
- Defined in:
- lib/ads_common/auth/oauth2_jwt_handler.rb
Overview
Credentials class to handle OAuth2.0 authentication.
Constant Summary collapse
- OAUTH2_CONFIG =
{ :token_credential_uri => 'https://accounts.google.com/o/oauth2/token', :audience => 'https://accounts.google.com/o/oauth2/token' }
Instance Method Summary collapse
-
#auth_string(credentials) ⇒ Object
Generates auth string for OAuth2.0 JWT method of authentication.
-
#get_token(credentials = nil) ⇒ Object
Overrides base get_token method to account for the token expiration.
- #handle_error(error) ⇒ Object
-
#initialize(config, scope) ⇒ OAuth2JwtHandler
constructor
Initializes the OAuthHandler2 with all the necessary details.
-
#property_changed(prop, value) ⇒ Object
Invalidates the stored token if the required credential has changed.
-
#refresh_token! ⇒ Object
Refreshes access token from refresh token.
Constructor Details
#initialize(config, scope) ⇒ OAuth2JwtHandler
Initializes the OAuthHandler2 with all the necessary details.
Args:
-
config: Config object with library configuration
-
scope: OAuth authorization scope
45 46 47 48 |
# File 'lib/ads_common/auth/oauth2_jwt_handler.rb', line 45 def initialize(config, scope) super(config) @scope, @client = scope, nil end |
Instance Method Details
#auth_string(credentials) ⇒ Object
Generates auth string for OAuth2.0 JWT method of authentication.
Args:
-
credentials: credentials set for authorization
Returns:
-
Authentication string
71 72 73 74 75 |
# File 'lib/ads_common/auth/oauth2_jwt_handler.rb', line 71 def auth_string(credentials) token = get_token(credentials) return ::Signet::OAuth2.( token[:access_token]) end |
#get_token(credentials = nil) ⇒ Object
Overrides base get_token method to account for the token expiration.
78 79 80 81 82 |
# File 'lib/ads_common/auth/oauth2_jwt_handler.rb', line 78 def get_token(credentials = nil) token = super(credentials) token = refresh_token! if !@client.nil? && @client.expired? return token end |
#handle_error(error) ⇒ Object
57 58 59 60 61 |
# File 'lib/ads_common/auth/oauth2_jwt_handler.rb', line 57 def handle_error(error) # TODO: Add support. get_logger().error(error) raise error end |
#property_changed(prop, value) ⇒ Object
Invalidates the stored token if the required credential has changed.
51 52 53 54 55 |
# File 'lib/ads_common/auth/oauth2_jwt_handler.rb', line 51 def property_changed(prop, value) oauth2_keys = [:oauth2_issuer, :oauth2_secret, :oauth2_keyfile, :oauth2_key] @client = nil if oauth2_keys.include?(prop) end |
#refresh_token! ⇒ Object
Refreshes access token from refresh token.
85 86 87 88 89 90 |
# File 'lib/ads_common/auth/oauth2_jwt_handler.rb', line 85 def refresh_token!() return nil if @token.nil? or @token[:refresh_token].nil? @client.refresh! @token = token_from_client(@client) return @token end |