Module: Lich::Common::GUI::MasterPasswordChange

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

Overview

Module for handling the master password change dialog Provides functionality to show the dialog and manage password changes.

Examples:

Showing the change password dialog

Lich::Common::GUI::MasterPasswordChange.show_change_master_password_dialog(parent, data_dir)

Class Method Summary collapse

Class Method Details

.show_change_master_password_dialog(parent, data_dir) ⇒ Boolean

Displays a dialog for changing the master password.

Examples:

success = Lich::Common::GUI::MasterPasswordChange.show_change_master_password_dialog(parent, data_dir)

Parameters:

  • parent (Gtk::Window)

    The parent window for the dialog.

  • data_dir (String)

    The directory where data is stored.

Returns:

  • (Boolean)

    Returns true if the password was changed successfully, false otherwise.

Raises:

  • (StandardError)

    Raises an error if there is an issue during the password change process.



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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'documented/common/gui/master_password_change.rb', line 26

def self.show_change_master_password_dialog(parent, data_dir)
  # Create dialog
  dialog = Gtk::Dialog.new(
    title: "Change Master Password",
    parent: parent,
    flags: :modal,
    buttons: [
      ["Change Password", Gtk::ResponseType::APPLY],
      ["Cancel", Gtk::ResponseType::CANCEL]
    ]
  )

  dialog.set_default_size(400, -1)
  dialog.border_width = 10

  # Set accessible properties for screen readers
  Accessibility.make_window_accessible(
    dialog,
    "Master Password Change Dialog",
    "Dialog for changing the master password"
  )

  # Create content area
  content_area = dialog.content_area
  content_area.spacing = 10

  # Add header
  header_label = Gtk::Label.new
  header_label.set_markup("<span weight='bold'>Change Master Password</span>")
  header_label.set_xalign(0)

  Accessibility.make_accessible(
    header_label,
    "Change Master Password Header",
    "Title for the master password change dialog",
    :label
  )

  content_area.add(header_label)

  # Add current password entry
  current_password_box = Gtk::Box.new(:horizontal, 5)
  current_password_label = Gtk::Label.new("Current Master Password:")
  current_password_label.set_xalign(0)
  current_password_box.pack_start(current_password_label, expand: false, fill: false, padding: 0)

  current_password_entry = Gtk::Entry.new
  current_password_entry.visibility = false

  Accessibility.make_entry_accessible(
    current_password_entry,
    "Current Master Password Entry",
    "Enter your current master password"
  )

  current_password_box.pack_start(current_password_entry, expand: true, fill: true, padding: 0)
  content_area.add(current_password_box)

  # Add new password entry
  new_password_box = Gtk::Box.new(:horizontal, 5)
  new_password_label = Gtk::Label.new("New Master Password:")
  new_password_label.set_xalign(0)
  new_password_box.pack_start(new_password_label, expand: false, fill: false, padding: 0)

  new_password_entry = Gtk::Entry.new
  new_password_entry.visibility = false

  Accessibility.make_entry_accessible(
    new_password_entry,
    "New Master Password Entry",
    "Enter your new master password (minimum 8 characters)"
  )

  new_password_box.pack_start(new_password_entry, expand: true, fill: true, padding: 0)
  content_area.add(new_password_box)

  # Add confirm password entry
  confirm_password_box = Gtk::Box.new(:horizontal, 5)
  confirm_password_label = Gtk::Label.new("Confirm New Password:")
  confirm_password_label.set_xalign(0)
  confirm_password_box.pack_start(confirm_password_label, expand: false, fill: false, padding: 0)

  confirm_password_entry = Gtk::Entry.new
  confirm_password_entry.visibility = false

  Accessibility.make_entry_accessible(
    confirm_password_entry,
    "Confirm Master Password Entry",
    "Confirm your new master password"
  )

  confirm_password_box.pack_start(confirm_password_entry, expand: true, fill: true, padding: 0)
  content_area.add(confirm_password_box)

  # Add status label
  status_label = Gtk::Label.new("")
  status_label.set_xalign(0)

  Accessibility.make_accessible(
    status_label,
    "Status Message",
    "Shows status messages during password change",
    :label
  )

  content_area.add(status_label)

  # Add show password checkbox
  MasterPasswordPromptUI.new.create_and_wire_show_password_checkbox(
    content_area,
    [current_password_entry, new_password_entry, confirm_password_entry]
  )

  # Show all widgets
  dialog.show_all

  # Track whether password was changed successfully
  password_changed = false

  # Set up response handler
  dialog.signal_connect('response') do |dlg, response|
    if response == Gtk::ResponseType::APPLY
      current_password = current_password_entry.text
      new_password = new_password_entry.text
      confirm_password = confirm_password_entry.text

      # Validate inputs
      if current_password.empty?
        status_label.set_markup("<span foreground='red'>Current password cannot be empty.</span>")
        next
      end

      if new_password.empty?
        status_label.set_markup("<span foreground='red'>New password cannot be empty.</span>")
        next
      end

      if new_password.length < 8
        status_label.set_markup("<span foreground='red'>New password must be at least 8 characters.</span>")
        next
      end

      if new_password != confirm_password
        status_label.set_markup("<span foreground='red'>New passwords do not match.</span>")
        next
      end

      # Validate current password
      yaml_file = YamlState.yaml_file_path(data_dir)
      unless File.exist?(yaml_file)
        status_label.set_markup("<span foreground='red'>No account data found.</span>")
        next
      end

      unless validate_current_password(current_password, yaml_file)
        status_label.set_markup("<span foreground='red'>Current password is incorrect.</span>")
        next
      end

      # Load YAML data
      begin
        yaml_data = YAML.load_file(yaml_file)

        # Perform re-encryption
        if re_encrypt_all_accounts(yaml_data, data_dir, current_password, new_password)
          password_changed = true

          # Show success message
          success_dialog = Gtk::MessageDialog.new(
            parent: parent,
            flags: :modal,
            type: :info,
            buttons: :ok,
            message: "Master password changed successfully."
          )

          Accessibility.make_window_accessible(
            success_dialog,
            "Success Message",
            "Master password change success notification"
          )

          success_dialog.run
          success_dialog.destroy

          # Close dialog
          dlg.destroy
        else
          status_label.set_markup("<span foreground='red'>Failed to change master password. Please try again.</span>")
        end
      rescue StandardError => e
        Lich.log "error: Error during master password change: #{e.message}"
        status_label.set_markup("<span foreground='red'>An error occurred: #{e.message}</span>")
      end
    elsif response == Gtk::ResponseType::CANCEL
      dlg.destroy
    end
  end

  password_changed
end