Class: Google::Auth::BearerTokenCredentials
- Inherits:
-
Object
- Object
- Google::Auth::BearerTokenCredentials
- Includes:
- BaseClient
- Defined in:
- lib/googleauth/bearer_token.rb
Overview
Implementation of Bearer Token authentication scenario.
Bearer tokens are strings representing an authorization grant.
They can be OAuth2 ("ya.29") tokens, JWTs, IDTokens -- anything
that is sent as a Bearer
in an Authorization
header.
Not all 'authentication' strings can be used with this class,
e.g. an API key cannot since API keys are sent in a
x-goog-api-key
header or as a query parameter.
This class should be used when the end-user is managing the authentication token separately, e.g. with a separate service. This means that tasks like tracking the lifetime of and refreshing the token are outside the scope of this class.
There is no JSON representation for this type of credentials. If the end-user has credentials in JSON format they should typically use the corresponding credentials type, e.g. ServiceAccountCredentials with the service account JSON.
Instance Attribute Summary collapse
-
#expires_at ⇒ Time?
readonly
The token expiration time provided by the end-user.
-
#token ⇒ String
(also: #bearer_token)
readonly
The token to be sent as a part of Bearer claim.
-
#universe_domain ⇒ String
The universe domain of the universe this token is for.
Attributes included from BaseClient
Class Method Summary collapse
-
.make_creds(options = {}) ⇒ Google::Auth::BearerTokenCredentials
Create the BearerTokenCredentials.
Instance Method Summary collapse
-
#duplicate(options = {}) ⇒ Google::Auth::BearerTokenCredentials
Creates a duplicate of these credentials.
-
#expires_within?(seconds) ⇒ Boolean
Determines if the credentials object has expired.
-
#initialize(options = {}) ⇒ BearerTokenCredentials
constructor
Initialize the BearerTokenCredentials.
Methods included from BaseClient
#apply, #apply!, #needs_access_token?, #notify_refresh_listeners, #on_refresh, #updater_proc
Constructor Details
#initialize(options = {}) ⇒ BearerTokenCredentials
Initialize the BearerTokenCredentials.
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/googleauth/bearer_token.rb', line 83 def initialize = {} raise ArgumentError, "Bearer token must be provided" if [:token].nil? || [:token].empty? @token = [:token] @expires_at = case [:expires_at] when Time [:expires_at] when Numeric Time.at [:expires_at] end @universe_domain = [:universe_domain] || "googleapis.com" end |
Instance Attribute Details
#expires_at ⇒ Time? (readonly)
Returns The token expiration time provided by the end-user.
52 53 54 |
# File 'lib/googleauth/bearer_token.rb', line 52 def expires_at @expires_at end |
#token ⇒ String (readonly) Also known as: bearer_token
Returns The token to be sent as a part of Bearer claim.
47 48 49 |
# File 'lib/googleauth/bearer_token.rb', line 47 def token @token end |
#universe_domain ⇒ String
Returns The universe domain of the universe this token is for.
56 57 58 |
# File 'lib/googleauth/bearer_token.rb', line 56 def universe_domain @universe_domain end |
Class Method Details
.make_creds(options = {}) ⇒ Google::Auth::BearerTokenCredentials
Create the BearerTokenCredentials.
69 70 71 |
# File 'lib/googleauth/bearer_token.rb', line 69 def make_creds = {} new end |
Instance Method Details
#duplicate(options = {}) ⇒ Google::Auth::BearerTokenCredentials
Creates a duplicate of these credentials.
114 115 116 117 118 119 120 |
# File 'lib/googleauth/bearer_token.rb', line 114 def duplicate = {} self.class.new( token: [:token] || @token, expires_at: [:expires_at] || @expires_at, universe_domain: [:universe_domain] || @universe_domain ) end |
#expires_within?(seconds) ⇒ Boolean
Determines if the credentials object has expired.
101 102 103 104 |
# File 'lib/googleauth/bearer_token.rb', line 101 def expires_within? seconds return false if @expires_at.nil? # Treat nil expiration as "never expires" Time.now + seconds >= @expires_at end |