Class: Verizon::Oauth2

Inherits:
CoreLibrary::HeaderAuth
  • Object
show all
Includes:
CoreLibrary
Defined in:
lib/verizon/http/auth/oauth_2.rb

Overview

Utility class for OAuth 2 authorization and token management.

Instance Method Summary collapse

Constructor Details

#initialize(oauth_2credentials, config) ⇒ Oauth2

Initialization constructor.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/verizon/http/auth/oauth_2.rb', line 17

def initialize(oauth_2credentials, config)
  auth_params = {}
  @_oauth_client_id = oauth_2credentials.oauth_client_id unless
    oauth_2credentials.nil? || oauth_2credentials.oauth_client_id.nil?
  @_oauth_client_secret = oauth_2credentials.oauth_client_secret unless
    oauth_2credentials.nil? || oauth_2credentials.oauth_client_secret.nil?
  @_oauth_token = oauth_2credentials.oauth_token unless
    oauth_2credentials.nil? || oauth_2credentials.oauth_token.nil?
  @_oauth_scopes = oauth_2credentials.oauth_scopes unless
    oauth_2credentials.nil? || oauth_2credentials.oauth_scopes.nil?
  @_o_auth_api = OauthAuthorizationController.new(config)
  auth_params['Authorization'] = "Bearer #{@_oauth_token.access_token}" unless @_oauth_token.nil?

  super auth_params
end

Instance Method Details

#build_basic_auth_headerString

Builds the basic auth header for endpoints in the OAuth Authorization Controller.

Returns:

  • (String)

    The value of the Authentication header.



41
42
43
# File 'lib/verizon/http/auth/oauth_2.rb', line 41

def build_basic_auth_header
  "Basic #{AuthHelper.get_base64_encoded_value(@_oauth_client_id, @_oauth_client_secret)}"
end

#error_messageObject

Display error message on occurrence of authentication failure.



12
13
14
# File 'lib/verizon/http/auth/oauth_2.rb', line 12

def error_message
  'OAuth2: OAuthToken is undefined or expired.'
end

#fetch_token(additional_params: nil) ⇒ OAuthToken

Fetches the token.

Parameters:

  • additional_params (Hash) (defaults to: nil)

    Any additional form parameters.

Returns:

  • (OAuthToken)

    The oAuth token instance.



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/verizon/http/auth/oauth_2.rb', line 48

def fetch_token(additional_params: nil)
  token = @_o_auth_api.request_token_oauth_2(
    build_basic_auth_header,
    scope: !@_oauth_scopes.nil? ? Array(@_oauth_scopes).compact.join(' ') : @_oauth_scopes,
    _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.

Parameters:

  • token (OAuthToken)

    The oAuth token instance.

Returns:

  • (Boolean)

    true if the token’s expiry exist and also the token is expired, false otherwise.



63
64
65
# File 'lib/verizon/http/auth/oauth_2.rb', line 63

def token_expired?(token)
  token.respond_to?('expiry') && AuthHelper.token_expired?(token.expiry)
end

#validBoolean

Validates the oAuth token.

Returns:

  • (Boolean)

    true if the token is present and not expired.



35
36
37
# File 'lib/verizon/http/auth/oauth_2.rb', line 35

def valid
  !@_oauth_token.nil? && !token_expired?(@_oauth_token)
end