File: local_credential_management_win.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (172 lines) | stat: -rw-r--r-- 6,209 bytes parent folder | download | duplicates (6)
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
// Copyright 2022 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/local_credential_management_win.h"

#include "base/functional/bind.h"
#include "base/notreached.h"
#include "base/task/sequenced_task_runner.h"
#include "build/build_config.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_observer.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h"
#include "components/user_prefs/user_prefs.h"
#include "device/fido/win/authenticator.h"
#include "device/fido/win/webauthn_api.h"

namespace {

bool ContainsUserCreatedCredential(
    const std::vector<device::DiscoverableCredentialMetadata>& credentials) {
  return std::ranges::any_of(
      credentials, [](const device::DiscoverableCredentialMetadata& cred) {
        return !cred.system_created;
      });
}

constexpr char kHasPlatformCredentialsPref[] =
    "webauthn.has_platform_credentials";

// CredentialPresenceCacher caches, in a `Profile` whether local credentials
// were found or not. This is done because we expect that enumerating platform
// credentials on Windows will get slower as the number of credentials
// increases, and we need to know whether there are any credentials in order to
// show the link (or not) on the passwords WebUI page.
//
// Thus, if credentials have been observed previously then that fact is cached
// and the link will appear on the passwords page without enumerating them
// again. Otherwise an enumeration will be attempted, which should be fast in
// the common case that there aren't any credentials.
//
// Since the platform authenticator is system-global, a `Profile` isn't quite
// the right sort of object to cache this information in. However, storing an
// installation-wide value would be much more work and, hopefully, this
// workaround can be eliminated in the future when webauthn.dll is faster.
//
// Since the `Profile` may be destroyed while the webauthn.dll call is still
// pending, this class observes the profile and handles that event.
class CredentialPresenceCacher : public ProfileObserver {
 public:
  CredentialPresenceCacher(
      Profile* profile,
      base::OnceCallback<void(
          std::optional<std::vector<device::DiscoverableCredentialMetadata>>)>
          callback)
      : profile_(profile), callback_(std::move(callback)) {}

  ~CredentialPresenceCacher() override {
    if (profile_) {
      profile_->RemoveObserver(this);
      profile_ = nullptr;
    }
  }

  void OnEnumerateResult(
      std::vector<device::DiscoverableCredentialMetadata> credentials) {
    if (profile_) {
      profile_->GetPrefs()->SetBoolean(
          kHasPlatformCredentialsPref,
          ContainsUserCreatedCredential(credentials));
    }
    std::sort(credentials.begin(), credentials.end(), CredentialComparator());
    std::move(callback_).Run(std::move(credentials));
  }

  // ProfileObserver:

  void OnProfileWillBeDestroyed(Profile* profile) override {
    DCHECK_EQ(profile, profile_);
    profile_->RemoveObserver(this);
    profile_ = nullptr;
  }

 private:
  raw_ptr<Profile> profile_;
  base::OnceCallback<void(
      std::optional<std::vector<device::DiscoverableCredentialMetadata>>)>
      callback_;
};

void EnumerateResultToBool(
    base::OnceCallback<void(bool)> callback,
    std::optional<std::vector<device::DiscoverableCredentialMetadata>>
        credentials) {
  std::move(callback).Run(credentials.has_value() &&
                          ContainsUserCreatedCredential(*credentials));
}

}  // namespace

LocalCredentialManagementWin::LocalCredentialManagementWin(
    device::WinWebAuthnApi* api,
    Profile* profile)
    : api_(api), profile_(profile) {}

// static
void LocalCredentialManagementWin::RegisterProfilePrefs(
    user_prefs::PrefRegistrySyncable* registry) {
  registry->RegisterBooleanPref(kHasPlatformCredentialsPref, false);
}

std::unique_ptr<LocalCredentialManagement> LocalCredentialManagement::Create(
    Profile* profile) {
  return std::make_unique<LocalCredentialManagementWin>(
      device::WinWebAuthnApi::GetDefault(), profile);
}

void LocalCredentialManagementWin::HasCredentials(
    base::OnceCallback<void(bool)> callback) {
  std::optional<bool> result;

  if (!api_->IsAvailable() || !api_->SupportsSilentDiscovery()) {
    result = false;
  } else if (profile_->GetPrefs()->GetBoolean(kHasPlatformCredentialsPref)) {
    result = true;
  }

  if (result.has_value()) {
    base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE, base::BindOnce(std::move(callback), *result));
    return;
  }

  auto cacher = std::make_unique<CredentialPresenceCacher>(
      profile_, base::BindOnce(EnumerateResultToBool, std::move(callback)));
  device::WinWebAuthnApiAuthenticator::EnumeratePlatformCredentials(
      api_, base::BindOnce(&CredentialPresenceCacher::OnEnumerateResult,
                           std::move(cacher)));
}

void LocalCredentialManagementWin::Enumerate(
    base::OnceCallback<void(
        std::optional<std::vector<device::DiscoverableCredentialMetadata>>)>
        callback) {
  if (!api_->IsAvailable() || !api_->SupportsSilentDiscovery()) {
    base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE, base::BindOnce(std::move(callback), std::nullopt));
    return;
  }

  auto cacher =
      std::make_unique<CredentialPresenceCacher>(profile_, std::move(callback));
  device::WinWebAuthnApiAuthenticator::EnumeratePlatformCredentials(
      api_, base::BindOnce(&CredentialPresenceCacher::OnEnumerateResult,
                           std::move(cacher)));
}

void LocalCredentialManagementWin::Delete(
    base::span<const uint8_t> credential_id,
    base::OnceCallback<void(bool)> callback) {
  device::WinWebAuthnApiAuthenticator::DeletePlatformCredential(
      api_, credential_id, std::move(callback));
}

void LocalCredentialManagementWin::Edit(
    base::span<uint8_t> credential_id,
    std::string new_username,
    base::OnceCallback<void(bool)> callback) {
  // Editing passkeys should not be an option in Windows.
  NOTREACHED();
}