Class: Wsaa::CredentialStore

Inherits:
Object
  • Object
show all
Defined in:
lib/wsaa/credential_store.rb

Overview

File-based cache for WSAA credentials.

Stores credentials as YAML files with date-based filenames.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache_dir:, service:) ⇒ CredentialStore

Creates a new CredentialStore.

Parameters:

  • cache_dir (String)

    Directory for cache files.

  • service (String)

    The service name.



20
21
22
23
# File 'lib/wsaa/credential_store.rb', line 20

def initialize(cache_dir:, service:)
  @cache_dir = cache_dir
  @service = service
end

Instance Attribute Details

#cache_dirString (readonly)

Directory where cache files are stored.

Returns:

  • (String)

    the current value of cache_dir



12
13
14
# File 'lib/wsaa/credential_store.rb', line 12

def cache_dir
  @cache_dir
end

#serviceString (readonly)

The service name used in the cache filename.

Returns:

  • (String)

    the current value of service



12
13
14
# File 'lib/wsaa/credential_store.rb', line 12

def service
  @service
end

Instance Method Details

#cache_file_pathString

Returns the full path to the cache file.

Returns:

  • (String)

    The cache file path.



66
67
68
# File 'lib/wsaa/credential_store.rb', line 66

def cache_file_path
  File.join(cache_dir, cache_filename)
end

#clearObject

Deletes the cache file.



58
59
60
# File 'lib/wsaa/credential_store.rb', line 58

def clear
  File.delete(cache_file_path) if File.exist?(cache_file_path)
end

#readCredentials?

Reads cached credentials.

Returns:

  • (Credentials, nil)

    The cached credentials, or nil if not found or expired.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/wsaa/credential_store.rb', line 29

def read
  return nil unless File.exist?(cache_file_path)

  data = YAML.load_file(cache_file_path)
  expiration_time = parse_expiration_time(data['expiration_time'])

  credentials = Credentials.new(
    token: data['token'],
    sign: data['sign'],
    expiration_time: expiration_time
  )

  credentials.valid? ? credentials : nil
rescue StandardError
  nil
end

#write(credentials) ⇒ Credentials

Writes credentials to the cache.

Parameters:

  • credentials (Credentials)

    The credentials to cache.

Returns:



51
52
53
54
# File 'lib/wsaa/credential_store.rb', line 51

def write(credentials)
  File.write(cache_file_path, YAML.dump(credentials.to_h.transform_keys(&:to_s)))
  credentials
end