Class: Legate::Auth::TokenStore
- Inherits:
-
Object
- Object
- Legate::Auth::TokenStore
- Defined in:
- lib/legate/auth/token_store.rb
Overview
Provides a token store for caching authentication tokens This class wraps a session service and provides methods for storing, retrieving, and clearing tokens
Instance Method Summary collapse
-
#clear(key) ⇒ Boolean
Clear a token from the cache.
-
#clear_all ⇒ Boolean
Clear all tokens from the cache.
-
#get(key) ⇒ Legate::Auth::ExchangedCredential?
Get a token from the cache.
-
#initialize(session_service) ⇒ TokenStore
constructor
Initialize a new token store.
-
#store(key, token) ⇒ Boolean
Store a token in the cache.
Constructor Details
#initialize(session_service) ⇒ TokenStore
Initialize a new token store
14 15 16 17 |
# File 'lib/legate/auth/token_store.rb', line 14 def initialize(session_service) @session_service = session_service @scope = 'auth' # Scoped state namespace for authentication end |
Instance Method Details
#clear(key) ⇒ Boolean
Clear a token from the cache
67 68 69 70 71 72 73 |
# File 'lib/legate/auth/token_store.rb', line 67 def clear(key) @session_service.clear_scoped_state(@scope, key) true rescue StandardError => e Legate.logger.error("Failed to clear token: #{e.}") false end |
#clear_all ⇒ Boolean
Clear all tokens from the cache
77 78 79 80 81 82 83 |
# File 'lib/legate/auth/token_store.rb', line 77 def clear_all @session_service.clear_scoped_state(@scope, '*') true rescue StandardError => e Legate.logger.error("Failed to clear all tokens: #{e.}") false end |
#get(key) ⇒ Legate::Auth::ExchangedCredential?
Get a token from the cache
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/legate/auth/token_store.rb', line 42 def get(key) # Retrieve from scoped state token_data = @session_service.load_scoped_state(@scope, key) return nil unless token_data # Deserialize to token object token = Legate::Auth::ExchangedCredential.from_h(token_data) # Check expiration if token.expired? Legate.logger.debug("Retrieved expired token from cache (key: #{key})") # Clear expired token clear(key) return nil end token rescue StandardError => e Legate.logger.error("Failed to retrieve token: #{e.}") nil end |
#store(key, token) ⇒ Boolean
Store a token in the cache
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/legate/auth/token_store.rb', line 23 def store(key, token) return false unless token.is_a?(Legate::Auth::ExchangedCredential) begin # Serialize token to hash token_data = token.to_h # Store in scoped state @session_service.save_scoped_state(@scope, key, token_data) true rescue StandardError => e Legate.logger.error("Failed to store token: #{e.}") false end end |