Class: EdFi::Client::AccessToken

Inherits:
Object
  • Object
show all
Defined in:
lib/ed_fi/client/access_token.rb

Overview

The EdFi::Client::AccessToken represents an access token, as returned by “/oauth/token” calls.

Instance Method Summary collapse

Constructor Details

#initialize(access_token:, token_type:, issued_at: Time.current, expires_in:) ⇒ AccessToken

An EdFi::Client::AccessToken can be initiialized with the “/oauth/token” response Hash. If given, an additional “issued_at” Time value helps to more accurately calculate the token's expiration Time.

Parameters:

  • access_token (String)

    The actual token value to use as the Bearer token in subsequent requests.

  • token_type (String)

    The token type (e.g. “bearer”).

  • issued_at (Time)

    An optional value denoting the Time at which the token was issued. If unset, defaults to Time.current.

  • expires_in (Numeric)

    The token's lifetime, in seconds.



27
28
29
30
31
32
# File 'lib/ed_fi/client/access_token.rb', line 27

def initialize(access_token:, token_type:, issued_at: Time.current, expires_in:)
  @access_token = access_token.dup
  @token_type = token_type.dup
  @issued_at = issued_at.dup
  @expires_in = expires_in.dup
end

Instance Method Details

#expires_atTime

Gives the token's calculated expiration Time.

Returns:

  • (Time)


48
49
50
51
# File 'lib/ed_fi/client/access_token.rb', line 48

def expires_at
  return 1.second.ago if @access_token.blank?
  (@issued_at + @expires_in.seconds)
end

#tokenString

Gives a copy of the token value.

Returns:

  • (String)


39
40
41
# File 'lib/ed_fi/client/access_token.rb', line 39

def token
  @access_token.dup
end

#valid?true, false

Denotes whether the token is still “valid”, per its (calculated) expiration timesstamp. Note that a 5-second window is allotted for the request using the token to complete.

Returns:

  • (true, false)


59
60
61
62
# File 'lib/ed_fi/client/access_token.rb', line 59

def valid?
  safety_window = 5.seconds
  Time.current <= (expires_at - safety_window)
end