Class: Slk::Services::TokenLoader

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

Overview

Handles loading tokens from encrypted or plaintext files

Instance Method Summary collapse

Constructor Details

#initialize(encryption:, paths:) ⇒ TokenLoader

Returns a new instance of TokenLoader.



7
8
9
10
# File 'lib/slk/services/token_loader.rb', line 7

def initialize(encryption:, paths:)
  @encryption = encryption
  @paths = paths
end

Instance Method Details

#encrypted_file_exists?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/slk/services/token_loader.rb', line 44

def encrypted_file_exists?
  File.exist?(encrypted_tokens_file)
end

#encrypted_tokens_fileObject



52
53
54
# File 'lib/slk/services/token_loader.rb', line 52

def encrypted_tokens_file
  @paths.config_file('tokens.age')
end

#load(ssh_key) ⇒ Object



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

def load(ssh_key)
  if encrypted_file_exists? && ssh_key
    decrypt_with_key(ssh_key)
  elsif encrypted_file_exists?
    raise EncryptionError, 'Cannot read encrypted tokens without SSH key'
  elsif plain_file_exists?
    parse_plain_file
  else
    {}
  end
end

#load_auto(config) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/slk/services/token_loader.rb', line 24

def load_auto(config)
  if encrypted_file_exists?
    load_encrypted(config)
  elsif plain_file_exists?
    parse_plain_file
  else
    {}
  end
end

#load_encrypted(config) ⇒ Object



34
35
36
37
# File 'lib/slk/services/token_loader.rb', line 34

def load_encrypted(config)
  raise_missing_key_error unless config.ssh_key
  decrypt_with_key(config.ssh_key)
end

#plain_file_exists?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/slk/services/token_loader.rb', line 48

def plain_file_exists?
  File.exist?(plain_tokens_file)
end

#plain_tokens_fileObject



56
57
58
# File 'lib/slk/services/token_loader.rb', line 56

def plain_tokens_file
  @paths.config_file('tokens.json')
end

#raise_missing_key_errorObject

Raises:



39
40
41
42
# File 'lib/slk/services/token_loader.rb', line 39

def raise_missing_key_error
  raise EncryptionError,
        'Cannot read encrypted tokens - no SSH key configured. Run: slk config set ssh_key <path>'
end