Module: Lich::Common::CLI::EncryptionModeChange

Defined in:
documented/common/cli/cli_encryption_mode_change.rb

Class Method Summary collapse

Class Method Details

.change_mode(new_mode, provided_password = nil) ⇒ Integer

Changes the encryption mode of the application. This method validates the provided mode and handles password requirements based on the current and new modes.

Examples:

Changing to enhanced mode

Lich::Common::CLI::EncryptionModeChange.change_mode(:enhanced, "my_password")

Parameters:

  • new_mode (Symbol)

    The new encryption mode to set. Valid options are :plaintext, :standard, :enhanced.

  • provided_password (String, nil) (defaults to: nil)

    An optional password provided for mode transition.

Returns:

  • (Integer)

    Returns 0 on success, 1 on read error, 2 if the login file is not found, 3 for invalid mode, or 4 if cancelled by the user.

Raises:

  • (StandardError)

    Raises an error if an unexpected issue occurs during the mode change.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'documented/common/cli/cli_encryption_mode_change.rb', line 19

def self.change_mode(new_mode, provided_password = nil)
  data_dir = DATA_DIR
  yaml_file = Lich::Common::GUI::YamlState.yaml_file_path(data_dir)

  # Validate file exists
  unless File.exist?(yaml_file)
    puts "error: Login file not found: #{yaml_file}"
    Lich.log "error: CLI encryption mode change failed - file not found"
    return 2
  end

  # Validate mode
  valid_modes = [:plaintext, :standard, :enhanced]
  unless valid_modes.include?(new_mode)
    puts "error: Invalid encryption mode: #{new_mode}"
    puts "Valid modes: plaintext, standard, enhanced"
    Lich.log "error: CLI encryption mode change failed - invalid mode: #{new_mode}"
    return 3
  end

  # Load current mode
  begin
    yaml_data = YAML.load_file(yaml_file)
    current_mode = yaml_data['encryption_mode']&.to_sym || :plaintext
     = yaml_data['accounts']&.length || 0
  rescue StandardError => e
    puts "error: Failed to read login file: #{e.message}"
    Lich.log "error: CLI encryption mode change failed - read error: #{e.message}"
    return 1
  end

  # Check if already in target mode
  if current_mode == new_mode
    puts "info: Already using #{new_mode} encryption mode"
    Lich.log "info: CLI encryption mode change - already in target mode"
    return 0
  end

  puts "Changing encryption mode: #{current_mode}#{new_mode}"
  puts "Accounts to re-encrypt: #{}"
  puts ""

  # Handle password requirements based on mode transition
  new_master_password = nil

  # If leaving Enhanced mode, validate current password
  if current_mode == :enhanced
    puts "Current mode is Enhanced encryption."
    master_password = PasswordManager.prompt_for_master_password
    if master_password.nil?
      puts "Cancelled"
      Lich.log "info: CLI encryption mode change cancelled by user"
      return 4
    end

    # Validate the password
    validation_test = yaml_data['master_password_validation_test']
    unless Lich::Common::GUI::MasterPasswordManager.validate_master_password(
      master_password, validation_test
    )
      puts "error: Incorrect master password"
      Lich.log "error: CLI encryption mode change failed - password validation failed"
      return 1
    end

    puts "✓ Master password validated"
    puts ""
  end

  # If entering Enhanced mode, get/create master password
  if new_mode == :enhanced
    new_master_password = if provided_password
                            provided_password
                          else
                            PasswordManager.get_master_password_from_keychain_or_prompt
                          end

    if new_master_password.nil?
      puts "Cancelled"
      Lich.log "info: CLI encryption mode change cancelled by user"
      return 4
    end

    puts "✓ Master password accepted"
    puts ""
  end

  # Warn about plaintext mode
  if new_mode == :plaintext
    puts "⚠️  WARNING: Plaintext mode disables encryption"
    puts "Passwords will be stored unencrypted and visible in the file."
    puts ""
    print "Continue? (yes/no): "
    input = $stdin.gets
    if input.nil? || input.strip.downcase != 'yes'
      puts "Cancelled"
      Lich.log "info: CLI encryption mode change cancelled by user"
      return 4
    end
    puts ""
  end

  # Call domain method to perform the change
  success = Lich::Common::GUI::YamlState.change_encryption_mode(
    data_dir,
    new_mode,
    new_master_password
  )

  unless success
    puts "error: Failed to change encryption mode"
    Lich.log "error: CLI encryption mode change failed at domain level"
    return 1
  end

  puts "✓ Encryption mode changed: #{current_mode}#{new_mode}"
  puts "#{} accounts re-encrypted" if  > 0

  Lich.log "info: CLI encryption mode change successful: #{current_mode}#{new_mode}"
  0
rescue StandardError => e
  puts "error: Unexpected error during encryption mode change: #{e.message}"
  Lich.log "error: CLI encryption mode change failed: #{e.class}: #{e.message}"
  1
end