Skip to content

Commit

Permalink
Escalator: Allow failed password attempts to be retried
Browse files Browse the repository at this point in the history
There is a limit of 3 attempts before quitting, but the user can try
again after that.

The error messages now display to help the user understand the issue.
  • Loading branch information
hughdavenport authored and trflynn89 committed Mar 4, 2024
1 parent 74b655d commit d8f756c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
28 changes: 17 additions & 11 deletions Userland/Applications/Escalator/EscalatorWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,20 @@ EscalatorWindow::EscalatorWindow(StringView executable, Vector<StringView> argum
m_icon_image_widget->set_bitmap(app_icon.bitmap_for_size(32));

m_ok_button = *main_widget->find_descendant_of_type_named<GUI::DialogButton>("ok_button");
m_ok_button->on_click = [this](auto) {
auto result = check_password();
if (result.is_error()) {
GUI::MessageBox::show_error(this, ByteString::formatted("Failed to execute command: {}", result.error()));
size_t password_attempts = 0;
m_ok_button->on_click = [this, password_attempts](auto) mutable {
if (check_password()) {
auto result = execute_command();
if (result.is_error()) {
GUI::MessageBox::show_error(this, ByteString::formatted("Failed to execute command: {}", result.error()));
}
close();
} else {
if (++password_attempts >= 3) {
GUI::MessageBox::show_error(this, ByteString::formatted("Too many failed attempts"));
close();
}
}
close();
};
m_ok_button->set_default(true);

Expand All @@ -72,25 +80,23 @@ EscalatorWindow::EscalatorWindow(StringView executable, Vector<StringView> argum
m_password_input->set_focus(true);
}

ErrorOr<void> EscalatorWindow::check_password()
bool EscalatorWindow::check_password()
{
ByteString password = m_password_input->text();
if (password.is_empty()) {
GUI::MessageBox::show_error(this, "Please enter a password."sv);
return {};
return false;
}

// FIXME: PasswordBox really should store its input directly as a SecretString.
Core::SecretString password_secret = Core::SecretString::take_ownership(password.to_byte_buffer());
if (!m_current_user.authenticate(password_secret)) {
GUI::MessageBox::show_error(this, "Incorrect or disabled password."sv);
m_password_input->select_all();
return {};
return false;
}

// Caller will close Escalator if error is returned.
TRY(execute_command());
return {};
return true;
}

ErrorOr<void> EscalatorWindow::execute_command()
Expand Down
2 changes: 1 addition & 1 deletion Userland/Applications/Escalator/EscalatorWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class EscalatorWindow final : public GUI::Window {
private:
EscalatorWindow(StringView executable, Vector<StringView> arguments, Options const& options);

ErrorOr<void> check_password();
bool check_password();

Vector<StringView> m_arguments;
StringView m_executable;
Expand Down

0 comments on commit d8f756c

Please sign in to comment.