Class: OmniAuth::GustoOauth2::TokenClient
- Inherits:
-
Object
- Object
- OmniAuth::GustoOauth2::TokenClient
- 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.
Defined Under Namespace
Classes: TokenResult
Constant Summary collapse
- TOKEN_URL =
'https://api.gusto.com/oauth/token'
Instance Attribute Summary collapse
-
#client_id ⇒ Object
readonly
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
readonly
Returns the value of attribute client_secret.
Instance Method Summary collapse
-
#initialize(client_id:, client_secret:, redirect_uri: nil) ⇒ TokenClient
constructor
A new instance of TokenClient.
-
#refresh_token(refresh_token) ⇒ TokenResult
Refresh an access token using a refresh token.
-
#token_expired?(expires_at, buffer_seconds: 300) ⇒ Boolean
Check if a token is expired or about to expire.
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_id ⇒ Object (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_secret ⇒ Object (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.
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.}") rescue JSON::ParserError => e TokenResult.new(success: false, error: "Invalid JSON response: #{e.}") rescue StandardError => e TokenResult.new(success: false, error: "Unexpected error: #{e.}") end |
#token_expired?(expires_at, buffer_seconds: 300) ⇒ Boolean
Check if a token is expired or about to expire
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 |