Class: Slk::Services::TokenStore
- Inherits:
-
Object
- Object
- Slk::Services::TokenStore
- Defined in:
- lib/slk/services/token_store.rb
Overview
Manages workspace tokens with optional encryption
Instance Attribute Summary collapse
-
#on_info ⇒ Object
Returns the value of attribute on_info.
-
#on_prompt_pub_key ⇒ Object
Returns the value of attribute on_prompt_pub_key.
-
#on_warning ⇒ Object
Returns the value of attribute on_warning.
Instance Method Summary collapse
- #add(name, token, cookie = nil) ⇒ Object
- #all_workspaces ⇒ Object
- #empty? ⇒ Boolean
- #exists?(name) ⇒ Boolean
-
#initialize(config: nil, encryption: nil, paths: nil) ⇒ TokenStore
constructor
A new instance of TokenStore.
- #migrate_encryption(old_ssh_key, new_ssh_key) ⇒ Object
-
#remove(name) ⇒ Object
rubocop:disable Naming/PredicateMethod.
- #workspace(name) ⇒ Object
- #workspace_names ⇒ Object
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_info ⇒ Object
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_key ⇒ Object
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_warning ⇒ Object
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, = nil) Models::Workspace.new(name: name, token: token, cookie: ) tokens = @loader.load_auto(@config) tokens[name] = { 'token' => token, 'cookie' => }.compact @saver.save(tokens, @config.ssh_key) end |
#all_workspaces ⇒ Object
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
59 60 61 |
# File 'lib/slk/services/token_store.rb', line 59 def empty? @loader.load_auto(@config).empty? end |
#exists?(name) ⇒ 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_names ⇒ Object
37 38 39 |
# File 'lib/slk/services/token_store.rb', line 37 def workspace_names @loader.load_auto(@config).keys end |