Class: Craigslist::API::AccessToken
- Inherits:
-
Object
- Object
- Craigslist::API::AccessToken
- Defined in:
- lib/craigslist/api/access_token.rb
Overview
An OAuth2 bearer token with an expiry.
Immutable. TokenProvider discards and re-fetches rather than mutating.
Constant Summary collapse
- LEEWAY =
Treat a token as expired this many seconds early, so a token does not die in flight between the expiry check and the server receiving it.
60
Instance Attribute Summary collapse
-
#expires_at ⇒ Time
readonly
The moment the token stops being valid.
-
#scopes ⇒ Array<String>
readonly
Scopes the token was granted.
-
#token_type ⇒ String
readonly
Usually "Bearer".
-
#value ⇒ String
readonly
The raw token value.
Class Method Summary collapse
-
.from_payload(payload, now: Time.now) ⇒ AccessToken
Builds a token from the token endpoint's JSON payload.
Instance Method Summary collapse
-
#expired?(now: Time.now) ⇒ Boolean
Whether the token is expired, or close enough to it.
-
#initialize(value:, expires_in:, scopes: [], token_type: "Bearer", now: Time.now) ⇒ AccessToken
constructor
A new instance of AccessToken.
-
#inspect ⇒ Object
(also: #to_s)
Keeps the token value out of logs and exception output.
-
#to_header ⇒ String
Value for the
Authorizationheader.
Constructor Details
#initialize(value:, expires_in:, scopes: [], token_type: "Bearer", now: Time.now) ⇒ AccessToken
Returns a new instance of AccessToken.
25 26 27 28 29 30 31 |
# File 'lib/craigslist/api/access_token.rb', line 25 def initialize(value:, expires_in:, scopes: [], token_type: "Bearer", now: Time.now) @value = value @expires_at = now + expires_in.to_i @scopes = Array(scopes).flat_map { |s| s.to_s.split(/\s+/) }.freeze @token_type = token_type || "Bearer" freeze end |
Instance Attribute Details
#expires_at ⇒ Time (readonly)
Returns the moment the token stops being valid.
17 18 19 |
# File 'lib/craigslist/api/access_token.rb', line 17 def expires_at @expires_at end |
#scopes ⇒ Array<String> (readonly)
Returns scopes the token was granted.
20 21 22 |
# File 'lib/craigslist/api/access_token.rb', line 20 def scopes @scopes end |
#token_type ⇒ String (readonly)
Returns usually "Bearer".
23 24 25 |
# File 'lib/craigslist/api/access_token.rb', line 23 def token_type @token_type end |
#value ⇒ String (readonly)
Returns the raw token value.
14 15 16 |
# File 'lib/craigslist/api/access_token.rb', line 14 def value @value end |
Class Method Details
.from_payload(payload, now: Time.now) ⇒ AccessToken
Builds a token from the token endpoint's JSON payload.
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/craigslist/api/access_token.rb', line 38 def self.from_payload(payload, now: Time.now) value = payload["access_token"] raise AuthenticationError, "token endpoint returned no access_token" if value.nil? || value.empty? new( value: value, expires_in: payload.fetch("expires_in", 3600), scopes: payload["scopes"] || payload["scope"] || [], token_type: payload["token_type"], now: now ) end |
Instance Method Details
#expired?(now: Time.now) ⇒ Boolean
Returns whether the token is expired, or close enough to it.
52 53 54 |
# File 'lib/craigslist/api/access_token.rb', line 52 def expired?(now: Time.now) now >= (expires_at - LEEWAY) end |
#inspect ⇒ Object Also known as: to_s
Keeps the token value out of logs and exception output.
62 63 64 |
# File 'lib/craigslist/api/access_token.rb', line 62 def inspect "#<#{self.class.name} token=[FILTERED] expires_at=#{expires_at.iso8601} scopes=#{scopes.inspect}>" end |
#to_header ⇒ String
Returns value for the Authorization header.
57 58 59 |
# File 'lib/craigslist/api/access_token.rb', line 57 def to_header "#{token_type} #{value}" end |