Class: Craigslist::API::AccessToken

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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_atTime (readonly)

Returns the moment the token stops being valid.

Returns:

  • (Time)

    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

#scopesArray<String> (readonly)

Returns scopes the token was granted.

Returns:

  • (Array<String>)

    scopes the token was granted



20
21
22
# File 'lib/craigslist/api/access_token.rb', line 20

def scopes
  @scopes
end

#token_typeString (readonly)

Returns usually "Bearer".

Returns:

  • (String)

    usually "Bearer"



23
24
25
# File 'lib/craigslist/api/access_token.rb', line 23

def token_type
  @token_type
end

#valueString (readonly)

Returns the raw token value.

Returns:

  • (String)

    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.

Parameters:

  • payload (Hash)

Returns:

Raises:



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.

Returns:

  • (Boolean)

    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

#inspectObject 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_headerString

Returns value for the Authorization header.

Returns:

  • (String)

    value for the Authorization header



57
58
59
# File 'lib/craigslist/api/access_token.rb', line 57

def to_header
  "#{token_type} #{value}"
end