Class: AtprotoAuth::State::TokenSet

Inherits:
Object
  • Object
show all
Defined in:
lib/atproto_auth/state/token_set.rb

Overview

Represents a set of OAuth tokens and their associated metadata

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token:, token_type:, expires_in:, scope:, sub:, refresh_token: nil) ⇒ TokenSet

Creates a new TokenSet from a token response

Parameters:

  • access_token (String)

    The access token

  • token_type (String)

    Token type (must be “DPoP”)

  • expires_in (Integer)

    Token lifetime in seconds

  • refresh_token (String, nil) (defaults to: nil)

    Optional refresh token

  • scope (String)

    Space-separated list of granted scopes

  • sub (String)

    DID of the authenticated user



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/atproto_auth/state/token_set.rb', line 17

def initialize( # rubocop:disable Metrics/ParameterLists
  access_token:,
  token_type:,
  expires_in:,
  scope:,
  sub:,
  refresh_token: nil
)
  validate_token_type!(token_type)
  validate_required!("access_token", access_token)
  validate_required!("scope", scope)
  validate_required!("sub", sub)
  validate_expires_in!(expires_in)

  @access_token = access_token
  @refresh_token = refresh_token
  @token_type = token_type
  @scope = scope
  @sub = sub
  @expires_at = Time.now + expires_in
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



7
8
9
# File 'lib/atproto_auth/state/token_set.rb', line 7

def access_token
  @access_token
end

#expires_atObject (readonly)

Returns the value of attribute expires_at.



7
8
9
# File 'lib/atproto_auth/state/token_set.rb', line 7

def expires_at
  @expires_at
end

#refresh_tokenObject (readonly)

Returns the value of attribute refresh_token.



7
8
9
# File 'lib/atproto_auth/state/token_set.rb', line 7

def refresh_token
  @refresh_token
end

#scopeObject (readonly)

Returns the value of attribute scope.



7
8
9
# File 'lib/atproto_auth/state/token_set.rb', line 7

def scope
  @scope
end

#subObject (readonly)

Returns the value of attribute sub.



7
8
9
# File 'lib/atproto_auth/state/token_set.rb', line 7

def sub
  @sub
end

#token_typeObject (readonly)

Returns the value of attribute token_type.



7
8
9
# File 'lib/atproto_auth/state/token_set.rb', line 7

def token_type
  @token_type
end

Instance Method Details

#expired?(buffer = 30) ⇒ Boolean

Whether the access token has expired

Returns:

  • (Boolean)


47
48
49
# File 'lib/atproto_auth/state/token_set.rb', line 47

def expired?(buffer = 30)
  Time.now >= (@expires_at - buffer)
end

#renewable?Boolean

Whether this token set includes a refresh token

Returns:

  • (Boolean)


41
42
43
# File 'lib/atproto_auth/state/token_set.rb', line 41

def renewable?
  !@refresh_token.nil?
end