authentication_dialog.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef ASH_IN_SESSION_AUTH_AUTHENTICATION_DIALOG_H_
  5. #define ASH_IN_SESSION_AUTH_AUTHENTICATION_DIALOG_H_
  6. #include <memory>
  7. #include "ash/components/login/auth/auth_performer.h"
  8. #include "ash/components/login/auth/public/user_context.h"
  9. #include "ash/public/cpp/in_session_auth_dialog_controller.h"
  10. #include "ash/public/cpp/in_session_auth_token_provider.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/time/time.h"
  13. #include "base/unguessable_token.h"
  14. #include "components/account_id/account_id.h"
  15. #include "ui/views/controls/label.h"
  16. #include "ui/views/widget/widget.h"
  17. #include "ui/views/window/dialog_delegate.h"
  18. namespace views {
  19. class Textfield;
  20. }
  21. namespace ash {
  22. struct CryptohomeError;
  23. // To be used for in-session authentication. Currently, only password
  24. // is supported, however, there are plans to enrich this dialog to eventually
  25. // support all configured forms of authentication on the system.
  26. class AuthenticationDialog : public views::DialogDelegateView {
  27. public:
  28. class TestApi {
  29. public:
  30. explicit TestApi(AuthenticationDialog* dialog) : dialog_(dialog) {}
  31. views::Textfield* GetPasswordTextfield() {
  32. return dialog_->password_field_;
  33. }
  34. private:
  35. base::raw_ptr<AuthenticationDialog> const dialog_;
  36. };
  37. // |on_auth_complete| is called when the user has been authenticated
  38. // or when the dialog has been aborted
  39. explicit AuthenticationDialog(
  40. InSessionAuthDialogController::OnAuthComplete on_auth_complete,
  41. InSessionAuthTokenProvider* auth_token_provider,
  42. std::unique_ptr<AuthPerformer> auth_performer,
  43. const AccountId& account_id);
  44. ~AuthenticationDialog() override;
  45. // Creates and displays a new instance of a widget that hosts the
  46. // AuthenticationDialog.
  47. void Show();
  48. private:
  49. // Called post widget initialization. For now, this configures the Ok button
  50. // with custom behavior needed to handle retry of password entry. Also focuses
  51. // the text input field.
  52. void Init();
  53. // Calls `on_auth_complere_` with `success` == true if
  54. // authentication was successful, and `success` == false if the dialog was
  55. // aborted.
  56. void NotifyResult(bool success,
  57. const base::UnguessableToken& token,
  58. base::TimeDelta timeout);
  59. // Modifies the Ok button to display the proper string and registers
  60. // `ValidateAuthFactor` as a callback.
  61. void ConfigureOkButton();
  62. // Disables the use of the OK and Cancel buttons, makes password text field
  63. // read-only.
  64. void SetUIDisabled(bool is_disabled);
  65. // Registered as a callback to the Ok button. Disables UI, and validates the
  66. // auth factor.
  67. void ValidateAuthFactor();
  68. // Passed as a callback to `AuthPerformer::AuthenticateWithPassword`, notifies
  69. // the dialog of authentication success or failure, in case of failure we
  70. // modify the UI appropriately, in case of success we close the dialog.
  71. void OnAuthFactorValidityChecked(
  72. std::unique_ptr<UserContext> user_context,
  73. absl::optional<CryptohomeError> cryptohome_error);
  74. // Registered as a callback to the Cancel and Close buttons. Calls
  75. // `NotifyResult` with `success` == false.
  76. void CancelAuthAttempt();
  77. // Configures the different subviews such as the password textfield and the
  78. // error message label.
  79. void ConfigureChildViews();
  80. // Passed as a callback to `AuthPerformer::StartAuthSession` in
  81. // `OnAuthFactorValidityChecked` when trying to validate the password
  82. // and discovering that the auth session is no longer active
  83. void OnAuthSessionInvalid(bool user_exists,
  84. std::unique_ptr<UserContext> user_context,
  85. absl::optional<CryptohomeError> cryptohome_error);
  86. // Passed as a callback to `AuthPerformer::StartAuthSession`. Saves the
  87. // password key label to pass it later to authentication attempts and handles
  88. // errors from cryptohome
  89. void OnAuthSessionStarted(bool user_exists,
  90. std::unique_ptr<UserContext> user_context,
  91. absl::optional<CryptohomeError> cryptohome_error);
  92. base::raw_ptr<views::Textfield> password_field_;
  93. base::raw_ptr<views::Label> invalid_password_label_;
  94. // See implementation of `CancelAuthAttempt` for details.
  95. bool is_closing_ = false;
  96. InSessionAuthDialogController::OnAuthComplete on_auth_complete_;
  97. // Called when user submits an auth factor to check its validity
  98. std::unique_ptr<AuthPerformer> auth_performer_;
  99. // Non owning pointer, initialized and owned by
  100. // `ChromeBrowserMainExtraPartsAsh`.
  101. // `auth_token_provider_` will outlive this dialog since it will
  102. // be destroyed after `AshShellInit`, which owns the aura
  103. // window hierarchy.
  104. base::raw_ptr<InSessionAuthTokenProvider> auth_token_provider_;
  105. std::unique_ptr<UserContext> user_context_;
  106. base::WeakPtrFactory<AuthenticationDialog> weak_factory_{this};
  107. };
  108. } // namespace ash
  109. #endif // ASH_IN_SESSION_AUTH_AUTHENTICATION_DIALOG_H_