Class: Slk::Services::TokenSaver

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

Overview

Handles saving tokens to encrypted or plaintext files. Uses atomic writes (temp file + mv) for the token file itself.

Constant Summary collapse

FILE_ERRORS =

File system errors we catch and wrap in TokenStoreError

[
  Errno::ENOENT,
  Errno::EACCES,
  Errno::EPERM,
  Errno::ENOSPC,
  Errno::EDQUOT,
  Errno::EROFS,
  Errno::EIO
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(encryption:, paths:) ⇒ TokenSaver

Returns a new instance of TokenSaver.



19
20
21
22
# File 'lib/slk/services/token_saver.rb', line 19

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

Instance Method Details

#save(tokens, ssh_key) ⇒ Object



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

def save(tokens, ssh_key)
  @paths.ensure_config_dir

  if ssh_key
    save_encrypted(tokens, ssh_key)
  else
    save_plaintext(tokens)
  end
end

#save_with_cleanup(tokens, ssh_key) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/slk/services/token_saver.rb', line 34

def save_with_cleanup(tokens, ssh_key)
  @paths.ensure_config_dir

  if ssh_key
    save_encrypted(tokens, ssh_key)
    FileUtils.rm_f(plain_tokens_file)
  else
    save_plaintext(tokens)
    FileUtils.rm_f(encrypted_tokens_file)
  end
end