Class: Legate::Auth::TokenStore

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(session_service) ⇒ TokenStore

Initialize a new token store

Parameters:



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

Parameters:

  • key (String)

    The cache key to clear

Returns:

  • (Boolean)

    True if the token was cleared



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.message}")
  false
end

#clear_allBoolean

Clear all tokens from the cache

Returns:

  • (Boolean)

    True if all tokens were cleared



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.message}")
  false
end

#get(key) ⇒ Legate::Auth::ExchangedCredential?

Get a token from the cache

Parameters:

  • key (String)

    The cache key

Returns:



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.message}")
  nil
end

#store(key, token) ⇒ Boolean

Store a token in the cache

Parameters:

Returns:

  • (Boolean)

    True if the token was stored



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.message}")
    false
  end
end