File: password_credential_controller.cc

package info (click to toggle)
chromium 140.0.7339.127-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,192,880 kB
  • sloc: cpp: 35,093,808; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,797; asm: 949,904; xml: 747,503; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (176 lines) | stat: -rw-r--r-- 6,159 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/webauthn/password_credential_controller.h"

#include <iterator>
#include <memory>
#include <string>
#include <utility>

#include "base/check.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/password_manager/chrome_password_manager_client.h"
#include "chrome/browser/ui/passwords/passwords_model_delegate.h"
#include "components/password_manager/core/browser/form_fetcher.h"
#include "components/password_manager/core/browser/form_fetcher_impl.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"

using content::GlobalRenderFrameHostId;
using content::RenderFrameHost;
using content::WebContents;
using password_manager::PasswordForm;
using password_manager::PasswordFormDigest;

namespace {

PasswordFormDigest GetSynthesizedFormForUrl(GURL url) {
  PasswordFormDigest digest{PasswordForm::Scheme::kHtml, std::string(), url};
  digest.signon_realm = digest.url.spec();
  return digest;
}

password_manager::PasswordManagerClient* GetPasswordManagerClient(
    RenderFrameHost& render_frame_host) {
  WebContents* web_contents =
      WebContents::FromRenderFrameHost(&render_frame_host);

  if (!web_contents) {
    return nullptr;
  }

  return ChromePasswordManagerClient::FromWebContents(web_contents);
}

#if !BUILDFLAG(IS_ANDROID)
std::u16string GetAuthenticationMessage(std::string_view rp_id) {
#if BUILDFLAG(IS_LINUX)
  return u"";
#else
  return l10n_util::GetStringFUTF16(IDS_PASSWORD_MANAGER_FILLING_REAUTH,
                                    base::UTF8ToUTF16(rp_id));
#endif  // BUILDFLAG(IS_LINUX)
}
#endif  // !BUILDFLAG(IS_ANDROID)

}  // namespace

#if BUILDFLAG(IS_ANDROID)
PasswordCredentialController::PasswordCredentialController(
    GlobalRenderFrameHostId render_frame_host_id)
    : render_frame_host_id_(render_frame_host_id) {}
#else
PasswordCredentialController::PasswordCredentialController(
    GlobalRenderFrameHostId render_frame_host_id,
    AuthenticatorRequestDialogModel* model)
    : render_frame_host_id_(render_frame_host_id), model_(model) {
  model_observer_.Observe(model_);
}
#endif  // BUILDFLAG(IS_ANDROID)

PasswordCredentialController::~PasswordCredentialController() = default;

void PasswordCredentialController::FetchPasswords(
    const GURL& url,
    PasswordCredentialsReceivedCallback callback) {
  callback_ = std::move(callback);
  form_fetcher_ = GetFormFetcher(url);
  form_fetcher_->Fetch();
  form_fetcher_->AddConsumer(this);
}

void PasswordCredentialController::OnFetchCompleted() {
  CHECK(!callback_.is_null());
  PasswordCredentials credentials;
  std::ranges::transform(form_fetcher_->GetBestMatches(),
                         std::back_inserter(credentials),
                         [](const PasswordForm& form) {
                           return std::make_unique<PasswordForm>(form);
                         });
  std::erase_if(credentials, [](const std::unique_ptr<PasswordForm>& form) {
    return form->IsFederatedCredential() || form->username_value.empty();
  });
  std::move(callback_).Run(std::move(credentials));
}

std::unique_ptr<password_manager::FormFetcher>
PasswordCredentialController::GetFormFetcher(const GURL& url) {
  return std::make_unique<password_manager::FormFetcherImpl>(
      GetSynthesizedFormForUrl(url),
      GetPasswordManagerClient(*GetRenderFrameHost()),
      /*should_migrate_http_passwords=*/false);
}

RenderFrameHost* PasswordCredentialController::GetRenderFrameHost() const {
  RenderFrameHost* ret = RenderFrameHost::FromID(render_frame_host_id_);
  CHECK(ret);
  return ret;
}

#if !BUILDFLAG(IS_ANDROID)

bool PasswordCredentialController::IsAuthRequired() {
  // TODO(crbug.com/392549444): For the prototype, require screen lock only if
  // it's enabled (e.g. via PWM settings). This may change.
  auto* pwm_client = GetPasswordManagerClient(*GetRenderFrameHost());
  return pwm_client && pwm_client->GetPasswordFeatureManager()
                           ->IsBiometricAuthenticationBeforeFillingEnabled();
}

void PasswordCredentialController::SetPasswordSelectedCallback(
    content::AuthenticatorRequestClientDelegate::PasswordSelectedCallback
        callback) {
  password_selected_callback_ = callback;
}

void PasswordCredentialController::OnPasswordCredentialSelected(
    PasswordCredentialPair password) {
  if (!IsAuthRequired() || model_->local_auth_token.has_value()) {
    password_selected_callback_.Run(password_manager::CredentialInfo(
        password_manager::CredentialType::CREDENTIAL_TYPE_PASSWORD,
        password.first, password.first, GURL(), password.second,
        url::SchemeHostPort()));
    return;
  }
  filling_password_ = std::move(password);
  model_->SetStep(AuthenticatorRequestDialogModel::Step::kPasswordOsAuth);
}

void PasswordCredentialController::OnStepTransition() {
  if (!model_) {
    return;
  }

  if (model_->step() ==
      AuthenticatorRequestDialogModel::Step::kPasswordOsAuth) {
    CHECK(filling_password_.has_value());
    auto manage_passwords_ui_controller = PasswordsModelDelegateFromWebContents(
        WebContents::FromRenderFrameHost(GetRenderFrameHost()));
    if (!manage_passwords_ui_controller) {
      return;
    }
    manage_passwords_ui_controller->AuthenticateUserWithMessage(
        GetAuthenticationMessage(model_->relying_party_id),
        base::BindOnce(&PasswordCredentialController::OnAuthenticationCompleted,
                       weak_ptr_factory_.GetWeakPtr(),
                       std::move(filling_password_.value())));
    return;
  }
}

void PasswordCredentialController::OnAuthenticationCompleted(
    PasswordCredentialPair password,
    bool success) {
  if (!success) {
    model_->CancelAuthenticatorRequest();
    return;
  }
  password_selected_callback_.Run(password_manager::CredentialInfo(
      password_manager::CredentialType::CREDENTIAL_TYPE_PASSWORD,
      password.first, password.first, GURL(), password.second,
      url::SchemeHostPort()));
}

#endif  // !BUILDFLAG(IS_ANDROID)