Class: OmniAuth::GustoOauth2::TokenClient

Inherits:
Object
  • Object
show all
Defined in:
lib/omniauth/gusto_oauth2/token_client.rb

Overview

Client for managing Gusto OAuth2 tokens outside the OmniAuth flow.

Gusto access tokens expire after 2 hours. Refresh tokens never expire but are single-use — each refresh returns a new refresh token.

Examples:

Basic usage

client = OmniAuth::GustoOauth2::TokenClient.new(
  client_id: ENV['GUSTO_CLIENT_ID'],
  client_secret: ENV['GUSTO_CLIENT_SECRET']
)

result = client.refresh_token(.refresh_token)
if result.success?
  .update!(
    access_token: result.access_token,
    refresh_token: result.refresh_token,
    token_expires_at: Time.at(result.expires_at)
  )
end

Defined Under Namespace

Classes: TokenResult

Constant Summary collapse

TOKEN_URL =
'https://api.gusto.com/oauth/token'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id:, client_secret:, redirect_uri: nil) ⇒ TokenClient

Returns a new instance of TokenClient.



56
57
58
59
60
# File 'lib/omniauth/gusto_oauth2/token_client.rb', line 56

def initialize(client_id:, client_secret:, redirect_uri: nil)
  @client_id = client_id
  @client_secret = client_secret
  @redirect_uri = redirect_uri
end

Instance Attribute Details

#client_idObject (readonly)

Returns the value of attribute client_id.



54
55
56
# File 'lib/omniauth/gusto_oauth2/token_client.rb', line 54

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



54
55
56
# File 'lib/omniauth/gusto_oauth2/token_client.rb', line 54

def client_secret
  @client_secret
end

Instance Method Details

#refresh_token(refresh_token) ⇒ TokenResult

Refresh an access token using a refresh token. Gusto refresh tokens are single-use — always store the new refresh_token.

Parameters:

  • refresh_token (String)

    The refresh token to use

Returns:

  • (TokenResult)

    Result object with new tokens or error



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/omniauth/gusto_oauth2/token_client.rb', line 67

def refresh_token(refresh_token)
  return TokenResult.new(success: false, error: 'Refresh token is required') if refresh_token.nil? || refresh_token.empty?

  response = make_refresh_request(refresh_token)

  if response.success?
    parse_success_response(response)
  else
    parse_error_response(response)
  end
rescue Faraday::Error => e
  TokenResult.new(success: false, error: "Network error: #{e.message}")
rescue JSON::ParserError => e
  TokenResult.new(success: false, error: "Invalid JSON response: #{e.message}")
rescue StandardError => e
  TokenResult.new(success: false, error: "Unexpected error: #{e.message}")
end

#token_expired?(expires_at, buffer_seconds: 300) ⇒ Boolean

Check if a token is expired or about to expire

Parameters:

  • expires_at (Time, Integer)

    Token expiration time

  • buffer_seconds (Integer) (defaults to: 300)

    Buffer before expiration (default: 300 = 5 minutes)

Returns:

  • (Boolean)

    True if token is expired or will expire within buffer



90
91
92
93
94
95
# File 'lib/omniauth/gusto_oauth2/token_client.rb', line 90

def token_expired?(expires_at, buffer_seconds: 300)
  return true if expires_at.nil?

  expires_at_time = expires_at.is_a?(Integer) ? Time.at(expires_at) : expires_at
  Time.now >= (expires_at_time - buffer_seconds)
end