Class: Nucleus::Adapters::ExpiringTokenAuthClient
- Inherits:
-
TokenAuthClient
- Object
- AuthClient
- TokenAuthClient
- Nucleus::Adapters::ExpiringTokenAuthClient
- Defined in:
- lib/nucleus/core/adapter_extensions/auth/expiring_token_auth_client.rb
Instance Attribute Summary
Attributes inherited from TokenAuthClient
Attributes inherited from AuthClient
Instance Method Summary collapse
- #auth_header ⇒ Object
- #authenticate(username, password) ⇒ Object
-
#initialize(check_certificates = true) {|verify_ssl, username, password| ... } ⇒ ExpiringTokenAuthClient
constructor
Create a new instance of an ExpiringTokenAuthClient.
Methods inherited from AuthClient
Constructor Details
#initialize(check_certificates = true) {|verify_ssl, username, password| ... } ⇒ ExpiringTokenAuthClient
Create a new instance of an Nucleus::Adapters::ExpiringTokenAuthClient. An expiring token knows when it starts to be invalid, saving requests to the endpoint that would fail anyways. false if they are to be ignored (e.g. when using self-signed certificates in development environments) must provide the API token and its expiration date, usually retrieved from an HTTP call to the endpoint. false if they are to be ignored (e.g. when using self-signed certificates in development environments)
- 0
-
API token to be used for authenticated API requests,
nil if authentication failed, e.g. due to bad credentials
- 1
-
Expiration time until the token is valid
18 19 20 21 |
# File 'lib/nucleus/core/adapter_extensions/auth/expiring_token_auth_client.rb', line 18 def initialize(check_certificates = true, &token_expiration_parser) @token_expiration_parser = token_expiration_parser super(check_certificates) end |
Instance Method Details
#auth_header ⇒ Object
34 35 36 37 38 |
# File 'lib/nucleus/core/adapter_extensions/auth/expiring_token_auth_client.rb', line 34 def auth_header raise Errors::EndpointAuthenticationError, 'Authentication client was not authenticated yet' unless @api_token raise Errors::EndpointAuthenticationError, 'Cached authentication token expired' if expired? { 'Authorization' => "Bearer #{api_token}" } end |
#authenticate(username, password) ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/nucleus/core/adapter_extensions/auth/expiring_token_auth_client.rb', line 24 def authenticate(username, password) token, expiration_time = @token_expiration_parser.call(verify_ssl, username, password) raise Errors::EndpointAuthenticationError, 'Authentication failed, credentials seem to be invalid' unless token # verification passed, credentials are valid @api_token = token @expires = expiration_time self end |