Class: AdsCommon::Auth::OAuth2Handler
- Inherits:
-
BaseHandler
- Object
- BaseHandler
- AdsCommon::Auth::OAuth2Handler
- Defined in:
- lib/ads_common/auth/oauth2_handler.rb
Overview
Credentials class to handle OAuth2 authentication.
Constant Summary collapse
- OAUTH2_CONFIG =
{ :authorization_uri => 'https://accounts.google.com/o/oauth2/auth', :token_credential_uri => 'https://accounts.google.com/o/oauth2/token' }
- DEFAULT_CALLBACK =
'urn:ietf:wg:oauth:2.0:oob'
Instance Method Summary collapse
-
#auth_string(credentials) ⇒ Object
Generates auth string for OAuth2 method of authentication.
-
#get_token(credentials = nil, force_refresh = false) ⇒ Object
Overrides base get_token method to account for the token expiration.
- #handle_error(error) ⇒ Object
-
#initialize(config, scope) ⇒ OAuth2Handler
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) ⇒ OAuth2Handler
Initializes the OAuthHandler2 with all the necessary details.
Args:
-
config: Config object with library configuration
-
scope: OAuth authorization scope
47 48 49 50 51 52 53 54 |
# File 'lib/ads_common/auth/oauth2_handler.rb', line 47 def initialize(config, scope) super(config) @scopes = [] @scopes << scope unless scope.nil? additional_scopes = @config.read('authentication.oauth2_extra_scopes') @scopes += additional_scopes if additional_scopes.is_a?(Array) @client = nil end |
Instance Method Details
#auth_string(credentials) ⇒ Object
Generates auth string for OAuth2 method of authentication.
Args:
-
credentials: credentials set for authorization
Returns:
-
Authentication string
78 79 80 81 82 83 84 85 86 |
# File 'lib/ads_common/auth/oauth2_handler.rb', line 78 def auth_string(credentials) token = get_token(credentials) if token.nil? raise AdsCommon::Errors::AuthError.new( 'Could not get auth token. Are you missing a refresh token?') end return ::Signet::OAuth2.( token[:access_token]) end |
#get_token(credentials = nil, force_refresh = false) ⇒ Object
Overrides base get_token method to account for the token expiration.
89 90 91 92 93 94 |
# File 'lib/ads_common/auth/oauth2_handler.rb', line 89 def get_token(credentials = nil, force_refresh = false) token = super(credentials) token = refresh_token! if !@client.nil? && (force_refresh || @client.expired?) return token end |
#handle_error(error) ⇒ Object
64 65 66 67 68 |
# File 'lib/ads_common/auth/oauth2_handler.rb', line 64 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.
57 58 59 60 61 62 |
# File 'lib/ads_common/auth/oauth2_handler.rb', line 57 def property_changed(prop, value) oauth2_keys = [:oauth2_client_id, :oauth2_client_secret, :oauth2_token] if oauth2_keys.include?(prop) @token, @client = nil, nil end end |
#refresh_token! ⇒ Object
Refreshes access token from refresh token.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/ads_common/auth/oauth2_handler.rb', line 97 def refresh_token!() return nil if @token.nil? or @token[:refresh_token].nil? begin if @client.issued_at.is_a?(String) @client.issued_at = Time.parse(@client.issued_at) end proxy = @config.read('connection.proxy') connection = (proxy.nil?) ? nil : Faraday.new(:proxy => proxy) @client.refresh!(:connection => connection) rescue Signet::AuthorizationError => e raise AdsCommon::Errors::AuthError.new("OAuth2 token refresh failed", e, (e.response.nil?) ? nil : e.response.body) end @token = token_from_client(@client) return @token end |