Class: Verizon::ThingspaceOauth1
- Inherits:
-
CoreLibrary::HeaderAuth
- Object
- CoreLibrary::HeaderAuth
- Verizon::ThingspaceOauth1
- Includes:
- CoreLibrary
- Defined in:
- lib/verizon/http/auth/thingspace_oauth_1.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(thingspace_oauth_1_credentials, config) ⇒ ThingspaceOauth1
constructor
Initialization constructor.
-
#token_expired?(token) ⇒ Boolean
Checks if OAuth token has expired.
-
#valid ⇒ Boolean
Validates the oAuth token.
Constructor Details
#initialize(thingspace_oauth_1_credentials, config) ⇒ ThingspaceOauth1
Initialization constructor.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/verizon/http/auth/thingspace_oauth_1.rb', line 17 def initialize(thingspace_oauth_1_credentials, config) @_oauth_client_id = thingspace_oauth_1_credentials.oauth_client_id unless thingspace_oauth_1_credentials.nil? || thingspace_oauth_1_credentials.oauth_client_id.nil? @_oauth_client_secret = thingspace_oauth_1_credentials.oauth_client_secret unless thingspace_oauth_1_credentials.nil? || thingspace_oauth_1_credentials.oauth_client_secret.nil? @_oauth_token = thingspace_oauth_1_credentials.oauth_token unless thingspace_oauth_1_credentials.nil? || thingspace_oauth_1_credentials.oauth_token.nil? @_oauth_clock_skew = thingspace_oauth_1_credentials.oauth_clock_skew unless thingspace_oauth_1_credentials.nil? || thingspace_oauth_1_credentials.oauth_clock_skew.nil? @_oauth_token_provider = thingspace_oauth_1_credentials.oauth_token_provider unless thingspace_oauth_1_credentials.nil? || thingspace_oauth_1_credentials.oauth_token_provider.nil? @_oauth_on_token_update = thingspace_oauth_1_credentials.oauth_on_token_update unless thingspace_oauth_1_credentials.nil? || thingspace_oauth_1_credentials.oauth_on_token_update.nil? @_o_auth_api = OauthAuthorizationApi.new(config) super({}) end |
Instance Method Details
#apply(http_request) ⇒ Object
68 69 70 71 |
# File 'lib/verizon/http/auth/thingspace_oauth_1.rb', line 68 def apply(http_request) auth_params = { 'Authorization' => "Bearer #{@_oauth_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/verizon/http/auth/thingspace_oauth_1.rb', line 43 def build_basic_auth_header "Basic #{AuthHelper.get_base64_encoded_value(@_oauth_client_id, @_oauth_client_secret)}" end |
#error_message ⇒ Object
Display error message on occurrence of authentication failure.
12 13 14 |
# File 'lib/verizon/http/auth/thingspace_oauth_1.rb', line 12 def 'ThingspaceOauth1: 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 |
# File 'lib/verizon/http/auth/thingspace_oauth_1.rb', line 50 def fetch_token(additional_params: nil) token = @_o_auth_api.request_token_thingspace_oauth_1( 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.
64 65 66 |
# File 'lib/verizon/http/auth/thingspace_oauth_1.rb', line 64 def token_expired?(token) token.respond_to?('expiry') && AuthHelper.token_expired?(token.expiry, @_oauth_clock_skew) end |
#valid ⇒ Boolean
Validates the oAuth token.
36 37 38 39 |
# File 'lib/verizon/http/auth/thingspace_oauth_1.rb', line 36 def valid @_oauth_token = get_token_from_provider @_oauth_token.is_a?(OAuthToken) && !token_expired?(@_oauth_token) end |