Module: Lich::Common::GUI::PasswordManager

Defined in:
documented/common/gui/password_manager.rb

Class Method Summary collapse

Class Method Details

.change_password(entry:, new_password:, account_name: nil, master_password: nil) ⇒ Hash

Changes the password for a given entry based on the specified encryption mode.

Examples:

Changing a password in standard mode

entry = { encryption_mode: :standard, password: "old_password" }
updated_entry = PasswordManager.change_password(entry: entry, new_password: "new_password", account_name: "user@example.com")

Parameters:

  • entry (Hash)

    The entry containing the password and encryption mode.

  • new_password (String)

    The new password to set.

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

    The account name for standard mode (required).

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

    The master password for enhanced mode (required).

Returns:

  • (Hash)

    The updated entry with the new password.

Raises:

  • (ArgumentError)

    If account_name or master_password is missing in their respective modes.

  • (NotImplementedError)

    If the encryption mode is not implemented.

  • (ArgumentError)

    If the encryption mode is unknown.



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
# File 'documented/common/gui/password_manager.rb', line 22

def self.change_password(entry:, new_password:, account_name: nil, master_password: nil)
  mode = entry[:encryption_mode]&.to_sym || :plaintext

  case mode
  when :plaintext
    # Plaintext mode - store password directly
    entry[:password] = new_password
  when :standard
    # Standard mode - encrypt with account name
    raise ArgumentError, 'account_name required for standard mode' if .nil?

    entry[:password] = PasswordCipher.encrypt(
      new_password,
      mode: :standard,
      account_name: 
    )
  when :enhanced
    # Enhanced encryption mode - encrypt with master password
    raise ArgumentError, 'master_password required for enhanced mode' if master_password.nil?

    entry[:password] = PasswordCipher.encrypt(
      new_password,
      mode: :enhanced,
      master_password: master_password
    )
  when :ssh_key
    # Certificate encryption mode - future feature, not yet implemented
    raise NotImplementedError, "#{mode} mode not yet implemented"
  else
    raise ArgumentError, "Unknown encryption mode: #{mode}"
  end

  entry
end

.get_password(entry:, account_name: nil, master_password: nil) ⇒ String

Retrieves the password for a given entry based on the specified encryption mode.

Examples:

Retrieving a password in enhanced mode

entry = { encryption_mode: :enhanced, password: "encrypted_password" }
password = PasswordManager.get_password(entry: entry, master_password: "master_password")

Parameters:

  • entry (Hash)

    The entry containing the password and encryption mode.

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

    The account name for standard mode (required).

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

    The master password for enhanced mode (required).

Returns:

  • (String)

    The decrypted or plaintext password.

Raises:

  • (ArgumentError)

    If account_name or master_password is missing in their respective modes.

  • (NotImplementedError)

    If the encryption mode is not implemented.

  • (ArgumentError)

    If the encryption mode is unknown.



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
# File 'documented/common/gui/password_manager.rb', line 68

def self.get_password(entry:, account_name: nil, master_password: nil)
  mode = entry[:encryption_mode]&.to_sym || :plaintext
  encrypted_password = entry[:password]

  case mode
  when :plaintext
    # Plaintext mode - return password directly
    encrypted_password
  when :standard
    # Standard mode - decrypt with account name
    raise ArgumentError, 'account_name required for standard mode' if .nil?

    PasswordCipher.decrypt(
      encrypted_password,
      mode: :standard,
      account_name: 
    )
  when :enhanced
    # Enhanced encryption mode - decrypt with master password
    raise ArgumentError, 'master_password required for enhanced mode' if master_password.nil?

    PasswordCipher.decrypt(
      encrypted_password,
      mode: :enhanced,
      master_password: master_password
    )
  when :ssh_key
    # Certificate encryption mode - future feature, not yet implemented
    raise NotImplementedError, "#{mode} mode not yet implemented"
  else
    raise ArgumentError, "Unknown encryption mode: #{mode}"
  end
end