File: passkey_credential.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 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 (136 lines) | stat: -rw-r--r-- 4,590 bytes parent folder | download | duplicates (5)
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
// 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 "components/password_manager/core/browser/passkey_credential.h"

#include <optional>
#include <string>
#include <utility>
#include <vector>

#include "base/feature_list.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "components/strings/grit/components_strings.h"
#include "ui/base/l10n/l10n_util.h"

#if !BUILDFLAG(IS_ANDROID)
#include "base/containers/span.h"
#include "components/sync/protocol/webauthn_credential_specifics.pb.h"
#include "components/webauthn/core/browser/passkey_model_utils.h"
#endif  // !BUILDFLAG(IS_ANDROID)

namespace password_manager {

#if !BUILDFLAG(IS_ANDROID)

namespace {

std::vector<uint8_t> ProtobufBytesToVector(const std::string& bytes) {
  return std::vector<uint8_t>(bytes.begin(), bytes.end());
}

}  // namespace

// static
std::vector<PasskeyCredential> PasskeyCredential::FromCredentialSpecifics(
    base::span<const sync_pb::WebauthnCredentialSpecifics> passkeys) {
  std::vector<sync_pb::WebauthnCredentialSpecifics> filtered =
      webauthn::passkey_model_utils::FilterShadowedCredentials(passkeys);
  std::vector<password_manager::PasskeyCredential> ret;
  ret.reserve(filtered.size());
  for (const sync_pb::WebauthnCredentialSpecifics& passkey : filtered) {
    ret.emplace_back(
        password_manager::PasskeyCredential::Source::kAndroidPhone,
        RpId(passkey.rp_id()),
        CredentialId(ProtobufBytesToVector(passkey.credential_id())),
        UserId(ProtobufBytesToVector(passkey.user_id())),
        Username(passkey.has_user_name() ? passkey.user_name() : ""),
        DisplayName(
            passkey.has_user_display_name() ? passkey.user_display_name() : ""),
        passkey.creation_time() != 0
            ? std::optional<base::Time>(
                  base::Time::FromMillisecondsSinceUnixEpoch(
                      passkey.creation_time()))
            : std::nullopt);
  }
  return ret;
}

#endif  // !BUILDFLAG(IS_ANDROID)

namespace {

int GetAuthenticationLabelForPasskeysFromAndroid() {
#if BUILDFLAG(IS_ANDROID)
  return IDS_PASSWORD_MANAGER_PASSKEY;
#else
  return IDS_PASSWORD_MANAGER_USE_SCREEN_LOCK;
#endif  // BUILDFLAG(IS_ANDROID)
}

}  // namespace

PasskeyCredential::PasskeyCredential(Source source,
                                     RpId rp_id,
                                     CredentialId credential_id,
                                     UserId user_id,
                                     Username username,
                                     DisplayName display_name,
                                     std::optional<base::Time> creation_time)
    : source_(source),
      rp_id_(std::move(rp_id)),
      credential_id_(std::move(credential_id)),
      user_id_(std::move(user_id)),
      username_(std::move(username)),
      display_name_(std::move(display_name)),
      creation_time_(std::move(creation_time)) {}

PasskeyCredential::~PasskeyCredential() = default;

PasskeyCredential::PasskeyCredential(const PasskeyCredential&) = default;
PasskeyCredential& PasskeyCredential::operator=(const PasskeyCredential&) =
    default;

PasskeyCredential::PasskeyCredential(PasskeyCredential&&) = default;
PasskeyCredential& PasskeyCredential::operator=(PasskeyCredential&&) = default;

std::u16string PasskeyCredential::GetAuthenticatorLabel() const {
  return authenticator_label_.value_or(GetAuthenticatorLabelBySourceType());
}

void PasskeyCredential::SetAuthenticatorLabel(
    const std::u16string& authenticator_label) {
  authenticator_label_ = authenticator_label;
}

std::u16string PasskeyCredential::GetAuthenticatorLabelBySourceType() const {
  int id;
  switch (source_) {
    case Source::kWindowsHello:
      id = IDS_PASSWORD_MANAGER_PASSKEY_FROM_WINDOWS_HELLO;
      break;
    case Source::kTouchId:
      id = IDS_PASSWORD_MANAGER_PASSKEY_FROM_CHROME_PROFILE;
      break;
    case Source::kICloudKeychain:
      id = IDS_PASSWORD_MANAGER_PASSKEY_FROM_ICLOUD_KEYCHAIN;
      break;
    case Source::kAndroidPhone:
      id = GetAuthenticationLabelForPasskeysFromAndroid();
      break;
    case Source::kGooglePasswordManager:
      id = IDS_PASSWORD_MANAGER_PASSKEY_FROM_GOOGLE_PASSWORD_MANAGER;
      break;
    case Source::kOther:
      id = IDS_PASSWORD_MANAGER_USE_GENERIC_DEVICE;
      break;
  }
  return l10n_util::GetStringUTF16(id);
}

bool operator==(const PasskeyCredential& lhs,
                const PasskeyCredential& rhs) = default;

}  // namespace password_manager