Class: Elelem::MCP::TokenStorage

Inherits:
Object
  • Object
show all
Defined in:
lib/elelem/mcp/token_storage.rb

Constant Summary collapse

STORAGE_DIR =
File.expand_path("~/.config/elelem/tokens")

Instance Method Summary collapse

Constructor Details

#initialize ⇒ TokenStorage

Returns a new instance of TokenStorage.



8
9
10
# File 'lib/elelem/mcp/token_storage.rb', line 8

def initialize
  FileUtils.mkdir_p(STORAGE_DIR, mode: 0o700)
end

Instance Method Details

#load(resource_url) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/elelem/mcp/token_storage.rb', line 23

def load(resource_url)
  path = token_path(resource_url)
  return nil unless File.exist?(path)

  JSON.parse(File.read(path), symbolize_names: true)
rescue JSON::ParserError
  nil
end

#load_client(resource_url) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/elelem/mcp/token_storage.rb', line 38

def load_client(resource_url)
  path = client_path(resource_url)
  return nil unless File.exist?(path)

  JSON.parse(File.read(path), symbolize_names: true)
rescue JSON::ParserError
  nil
end

#save(resource_url, access_token:, refresh_token: nil, expires_in: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/elelem/mcp/token_storage.rb', line 12

def save(resource_url, access_token:, refresh_token: nil, expires_in: nil)
  data = {
    access_token: access_token,
    refresh_token: refresh_token,
    expires_at: expires_in ? Time.now.to_i + expires_in : nil
  }
  path = token_path(resource_url)
  File.write(path, data.to_json)
  File.chmod(0o600, path)
end

#save_client(resource_url, client_data) ⇒ Object



32
33
34
35
36
# File 'lib/elelem/mcp/token_storage.rb', line 32

def save_client(resource_url, client_data)
  path = client_path(resource_url)
  File.write(path, client_data.to_json)
  File.chmod(0o600, path)
end