Class: Slk::Services::TokenStore

Inherits:
Object
  • Object
show all
Defined in:
lib/slk/services/token_store.rb

Overview

Manages workspace tokens with optional encryption

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: nil, encryption: nil, paths: nil) ⇒ TokenStore

Returns a new instance of TokenStore.



12
13
14
15
16
17
18
19
20
21
# File 'lib/slk/services/token_store.rb', line 12

def initialize(config: nil, encryption: nil, paths: nil)
  @config = config || Configuration.new
  @encryption = encryption || Encryption.new
  @paths = paths || Support::XdgPaths.new
  @loader = TokenLoader.new(encryption: @encryption, paths: @paths)
  @saver = TokenSaver.new(encryption: @encryption, paths: @paths)
  @on_warning = nil
  @on_info = nil
  @on_prompt_pub_key = nil
end

Instance Attribute Details

#on_infoObject

Returns the value of attribute on_info.



10
11
12
# File 'lib/slk/services/token_store.rb', line 10

def on_info
  @on_info
end

#on_prompt_pub_keyObject

Returns the value of attribute on_prompt_pub_key.



10
11
12
# File 'lib/slk/services/token_store.rb', line 10

def on_prompt_pub_key
  @on_prompt_pub_key
end

#on_warningObject

Returns the value of attribute on_warning.



10
11
12
# File 'lib/slk/services/token_store.rb', line 10

def on_warning
  @on_warning
end

Instance Method Details

#add(name, token, cookie = nil) ⇒ Object



45
46
47
48
49
50
# File 'lib/slk/services/token_store.rb', line 45

def add(name, token, cookie = nil)
  Models::Workspace.new(name: name, token: token, cookie: cookie)
  tokens = @loader.load_auto(@config)
  tokens[name] = { 'token' => token, 'cookie' => cookie }.compact
  @saver.save(tokens, @config.ssh_key)
end

#all_workspacesObject



31
32
33
34
35
# File 'lib/slk/services/token_store.rb', line 31

def all_workspaces
  @loader.load_auto(@config).map do |name, data|
    Models::Workspace.new(name: name, token: data['token'], cookie: data['cookie'])
  end
end

#empty?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/slk/services/token_store.rb', line 59

def empty?
  @loader.load_auto(@config).empty?
end

#exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/slk/services/token_store.rb', line 41

def exists?(name)
  @loader.load_auto(@config).key?(name)
end

#migrate_encryption(old_ssh_key, new_ssh_key) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/slk/services/token_store.rb', line 63

def migrate_encryption(old_ssh_key, new_ssh_key)
  return if old_ssh_key == new_ssh_key

  tokens = @loader.load(old_ssh_key)
  return if tokens.empty?

  @encryption.on_prompt_pub_key = @on_prompt_pub_key
  @encryption.validate_key_type!(new_ssh_key) if new_ssh_key
  @saver.save_with_cleanup(tokens, new_ssh_key)
  notify_encryption_change(old_ssh_key, new_ssh_key)
end

#remove(name) ⇒ Object

rubocop:disable Naming/PredicateMethod



52
53
54
55
56
57
# File 'lib/slk/services/token_store.rb', line 52

def remove(name) # rubocop:disable Naming/PredicateMethod
  tokens = @loader.load_auto(@config)
  removed = tokens.delete(name)
  @saver.save(tokens, @config.ssh_key) if removed
  !removed.nil?
end

#workspace(name) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/slk/services/token_store.rb', line 23

def workspace(name)
  tokens = @loader.load_auto(@config)
  data = tokens[name]
  raise WorkspaceNotFoundError, "Workspace '#{name}' not found" unless data

  Models::Workspace.new(name: name, token: data['token'], cookie: data['cookie'])
end

#workspace_namesObject



37
38
39
# File 'lib/slk/services/token_store.rb', line 37

def workspace_names
  @loader.load_auto(@config).keys
end