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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
content / browser / webid / digital_credentials / digital_identity_request_impl_unittest.cc [blame]
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/webid/digital_credentials/digital_identity_request_impl.h"
#include <optional>
#include "base/json/json_reader.h"
#include "base/test/scoped_feature_list.h"
#include "base/values.h"
#include "content/browser/webid/test/stub_digital_identity_provider.h"
#include "content/public/common/content_features.h"
#include "services/data_decoder/public/cpp/data_decoder.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace content {
namespace {
constexpr char kOpenid4vpProtocol[] = "openid4vp";
constexpr char kPreviewProtocol[] = "preview";
using InterstitialType = content::DigitalIdentityInterstitialType;
// StubDigitalIdentityProvider which enables overriding
// DigitalIdentityProvider::IsLowRiskOrigin().
class TestDigitalIdentityProviderWithCustomRisk
: public StubDigitalIdentityProvider {
public:
explicit TestDigitalIdentityProviderWithCustomRisk(bool are_origins_low_risk)
: are_origins_low_risk_(are_origins_low_risk) {}
~TestDigitalIdentityProviderWithCustomRisk() override = default;
bool IsLowRiskOrigin(const url::Origin& to_check) const override {
return are_origins_low_risk_;
}
private:
bool are_origins_low_risk_;
};
base::Value ParseJsonAndCheck(const std::string& json) {
std::optional<base::Value> parsed = base::JSONReader::Read(json);
return parsed.has_value() ? std::move(*parsed) : base::Value();
}
base::Value GenerateOnlyAgeOpenid4VpRequest() {
constexpr char kJson[] = R"({
"client_id": "digital-credentials.dev",
"client_id_scheme": "web-origin",
"response_type": "vp_token",
"nonce": "Q7pqzU98nsE9O9t--NpdmmeZSQOkmYKXsWCiJi39UGs=",
"presentation_definition": {
"id": "mDL-request-demo",
"input_descriptors": [
{
"id": "org.iso.18013.5.1.mDL",
"format": {
"mso_mdoc": {
"alg": [
"ES256"
]
}
},
"constraints": {
"limit_disclosure": "required",
"fields": [
{
"path": [
"$['org.iso.18013.5.1']['age_over_78']"
],
"intent_to_retain": false
}
]
}
}
]
}
})";
return ParseJsonAndCheck(kJson);
}
base::Value GenerateOnlyAgePreviewRequest() {
constexpr char kJson[] = R"({
"selector": {
"format": [
"mdoc"
],
"doctype": "org.iso.18013.5.1.mDL",
"fields": [
{
"namespace": "org.iso.18013.5.1",
"name": "age_over_21",
"intentToRetain": false
}
]
},
"nonce": "vvm3Q1VN1tXybccprmZhbZFIjBGSB4VNMuqQfD4Uiko=",
"readerPublicKey": "BMK9ink7wCHIKXxxWQy-S6TLN4jo1ab7NBlC-lSvqqMUmgMSadLa9PYYDocWitOmafZqWmZc5lQvdCZQx5mTNvs="
})";
return ParseJsonAndCheck(kJson);
}
// Does depth-first traversal of nested dicts rooted at `root`. Returns first
// matching base::Value with key `find_key`.
base::Value* FindValueWithKey(base::Value& root, const std::string& find_key) {
if (root.is_list()) {
base::Value::List& list = root.GetList();
for (base::Value& list_item : list) {
if (base::Value* out = FindValueWithKey(list_item, find_key)) {
return out;
}
}
return nullptr;
}
if (root.is_dict()) {
base::Value::Dict& dict = root.GetDict();
for (auto it : dict) {
if (it.first == find_key) {
return &it.second;
}
if (base::Value* out = FindValueWithKey(it.second, find_key)) {
return out;
}
}
}
return nullptr;
}
bool HasNoListElements(const base::Value* value) {
return !value || !value->is_list() || value->GetList().size() == 0u;
}
bool IsNonEmptyList(const base::Value* value) {
return !HasNoListElements(value);
}
// Removes `find_key` if present from `dict`. Ignores nested base::Value::Dicts.
void RemoveDictKey(base::Value::Dict& dict, const std::string& find_key) {
for (auto it = dict.begin(); it != dict.end(); ++it) {
if (it->first == find_key) {
dict.erase(it);
break;
}
}
}
// Used to modify an Openid4VpRequest on the fly.
bool SetPathItem(base::Value& to_modify, const std::string& path_item) {
base::Value* paths = FindValueWithKey(to_modify, "path");
if (HasNoListElements(paths)) {
return false;
}
paths->GetList().resize(0);
paths->GetList().Append(path_item);
return true;
}
// Used to modify a Preview on the fly.
bool SetFieldNameValue(base::Value& to_modify,
const std::string& field_name_value) {
base::Value* fields = FindValueWithKey(to_modify, "fields");
if (HasNoListElements(fields)) {
return false;
}
fields->GetList().front().GetDict().Set("name", field_name_value);
return true;
}
std::optional<InterstitialType> ComputeInterstitialType(
const std::string& protocol,
base::Value request) {
auto provider = std::make_unique<TestDigitalIdentityProviderWithCustomRisk>(
/*are_origins_low_risk=*/false);
return DigitalIdentityRequestImpl::ComputeInterstitialType(
url::Origin(), provider.get(), protocol, std::move(request));
}
} // anonymous namespace
class DigitalIdentityRequestImplTest : public testing::Test {
public:
void SetUp() override {
scoped_feature_list_.InitAndEnableFeatureWithParameters(
features::kWebIdentityDigitalCredentials, {{"dialog", ""}});
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
TEST_F(DigitalIdentityRequestImplTest,
Openid4VpProtocol_ComputeInterstitialType_OnlyAgeOver) {
EXPECT_EQ(std::nullopt,
ComputeInterstitialType(kOpenid4vpProtocol,
GenerateOnlyAgeOpenid4VpRequest()));
}
TEST_F(DigitalIdentityRequestImplTest,
Openid4VpProtocol_ComputeInterstitialType_OnlyAgeInYears) {
base::Value request = GenerateOnlyAgeOpenid4VpRequest();
ASSERT_TRUE(SetPathItem(request, "$['org.iso.18013.5.1']['age_in_years']"));
EXPECT_EQ(std::nullopt,
ComputeInterstitialType(kOpenid4vpProtocol, std::move(request)));
}
TEST_F(DigitalIdentityRequestImplTest,
Openid4VpProtocol_ComputeIntersitialType_OnlyAgeBirthYear) {
base::Value request = GenerateOnlyAgeOpenid4VpRequest();
ASSERT_TRUE(SetPathItem(request, "$['org.iso.18013.5.1']['age_birth_year']"));
EXPECT_EQ(std::nullopt,
ComputeInterstitialType(kOpenid4vpProtocol, std::move(request)));
}
TEST_F(DigitalIdentityRequestImplTest,
Openid4VpProtocol_ComputeIntersitialType_OnlyBirthDate) {
base::Value request = GenerateOnlyAgeOpenid4VpRequest();
ASSERT_TRUE(SetPathItem(request, "$['org.iso.18013.5.1']['birth_date']"));
EXPECT_EQ(std::nullopt,
ComputeInterstitialType(kOpenid4vpProtocol, std::move(request)));
}
base::Value GenerateNonAgeOpenid4VpRequest() {
base::Value request = GenerateOnlyAgeOpenid4VpRequest();
CHECK(SetPathItem(request, "$['org.iso.18013.5.1']['given_name']"));
return request;
}
TEST_F(DigitalIdentityRequestImplTest,
Openid4VpProtocol_ComputeIntersitialType_OnlyNonAgeDataElement) {
EXPECT_EQ(InterstitialType::kLowRisk,
ComputeInterstitialType(kOpenid4vpProtocol,
GenerateNonAgeOpenid4VpRequest()));
}
TEST_F(DigitalIdentityRequestImplTest,
Openid4VpProtocol_LowRiskOriginTakesPrecedenceOverRequestType) {
auto provider = std::make_unique<TestDigitalIdentityProviderWithCustomRisk>(
/*are_origins_low_risk=*/true);
EXPECT_EQ(std::nullopt, DigitalIdentityRequestImpl::ComputeInterstitialType(
url::Origin(), provider.get(), kOpenid4vpProtocol,
GenerateNonAgeOpenid4VpRequest()));
}
TEST_F(DigitalIdentityRequestImplTest,
Openid4VpProtocol_ComputeInterstitialType_EmptyPathList) {
base::Value request = GenerateOnlyAgeOpenid4VpRequest();
base::Value* paths = FindValueWithKey(request, "path");
ASSERT_TRUE(IsNonEmptyList(paths));
paths->GetList().resize(0);
EXPECT_EQ(InterstitialType::kLowRisk,
ComputeInterstitialType(kOpenid4vpProtocol, std::move(request)));
}
TEST_F(DigitalIdentityRequestImplTest,
Openid4VpProtocol_ComputeInterstitialType_RequestMultiplePaths) {
base::Value request = GenerateOnlyAgeOpenid4VpRequest();
base::Value* paths = FindValueWithKey(request, "path");
ASSERT_TRUE(IsNonEmptyList(paths));
base::Value::List& path_list = paths->GetList();
path_list.Append(path_list.front().Clone());
EXPECT_EQ(InterstitialType::kLowRisk,
ComputeInterstitialType(kOpenid4vpProtocol, std::move(request)));
}
TEST_F(DigitalIdentityRequestImplTest,
Openid4VpProtocol_ComputeInterstitialType_NoPath) {
base::Value request = GenerateOnlyAgeOpenid4VpRequest();
base::Value* fields = FindValueWithKey(request, "fields");
ASSERT_TRUE(IsNonEmptyList(fields));
RemoveDictKey(fields->GetList().front().GetDict(), "path");
EXPECT_EQ(InterstitialType::kLowRisk,
ComputeInterstitialType(kOpenid4vpProtocol, std::move(request)));
}
TEST_F(DigitalIdentityRequestImplTest,
Openid4VpProtocol_ComputeInterstitialType_EmptyFieldsList) {
base::Value request = GenerateOnlyAgeOpenid4VpRequest();
base::Value* fields = FindValueWithKey(request, "fields");
ASSERT_TRUE(IsNonEmptyList(fields));
fields->GetList().resize(0);
EXPECT_EQ(InterstitialType::kLowRisk,
ComputeInterstitialType(kOpenid4vpProtocol, std::move(request)));
}
TEST_F(DigitalIdentityRequestImplTest,
Openid4VpProtocol_ComputeInterstitialType_RequestMultipleAgeAssertions) {
base::Value request = GenerateOnlyAgeOpenid4VpRequest();
base::Value* fields = FindValueWithKey(request, "fields");
ASSERT_TRUE(IsNonEmptyList(fields));
base::Value new_field = ParseJsonAndCheck(R"({
"path": [
"$['org.iso.18013.5.1']['age_over_60']"
]
})");
fields->GetList().Append(std::move(new_field));
EXPECT_EQ(InterstitialType::kLowRisk,
ComputeInterstitialType(kOpenid4vpProtocol, std::move(request)));
}
TEST_F(DigitalIdentityRequestImplTest,
Openid4VpProtocol_ComputeInterstitialType_RequestMultipleFields) {
base::Value request = GenerateOnlyAgeOpenid4VpRequest();
base::Value* fields = FindValueWithKey(request, "fields");
ASSERT_TRUE(IsNonEmptyList(fields));
base::Value new_field = ParseJsonAndCheck(R"({
"path": [
"$['org.iso.18013.5.1']['given_name']"
]
})");
fields->GetList().Append(std::move(new_field));
EXPECT_EQ(InterstitialType::kLowRisk,
ComputeInterstitialType(kOpenid4vpProtocol, std::move(request)));
}
TEST_F(DigitalIdentityRequestImplTest,
Openid4VpProtocol_ComputeInterstitialType_NoConstraints) {
base::Value request = GenerateOnlyAgeOpenid4VpRequest();
base::Value* input_descriptors =
FindValueWithKey(request, "input_descriptors");
ASSERT_TRUE(IsNonEmptyList(input_descriptors));
RemoveDictKey(input_descriptors->GetList().front().GetDict(), "constraints");
EXPECT_EQ(InterstitialType::kLowRisk,
ComputeInterstitialType(kOpenid4vpProtocol, std::move(request)));
}
TEST_F(DigitalIdentityRequestImplTest,
Openid4VpProtocol_ComputeInterstitialType_EmptyInputDescriptorList) {
base::Value request = GenerateOnlyAgeOpenid4VpRequest();
base::Value* input_descriptors =
FindValueWithKey(request, "input_descriptors");
ASSERT_TRUE(IsNonEmptyList(input_descriptors));
input_descriptors->GetList().resize(0);
EXPECT_EQ(InterstitialType::kLowRisk,
ComputeInterstitialType(kOpenid4vpProtocol, std::move(request)));
}
TEST_F(DigitalIdentityRequestImplTest,
Openid4VpProtocol_ComputeInterstitialType_RequestMultipleDocuments) {
base::Value request = GenerateOnlyAgeOpenid4VpRequest();
base::Value* input_descriptors =
FindValueWithKey(request, "input_descriptors");
ASSERT_TRUE(IsNonEmptyList(input_descriptors));
base::Value::List& input_descriptor_list = input_descriptors->GetList();
input_descriptor_list.Append(input_descriptor_list.front().Clone());
EXPECT_EQ(InterstitialType::kLowRisk,
ComputeInterstitialType(kOpenid4vpProtocol, std::move(request)));
}
TEST_F(DigitalIdentityRequestImplTest,
Openid4VpProtocol_ComputeInterstitialType_NonMdlInputDescriptorId) {
base::Value request = GenerateOnlyAgeOpenid4VpRequest();
base::Value* input_descriptors =
FindValueWithKey(request, "input_descriptors");
ASSERT_TRUE(IsNonEmptyList(input_descriptors));
base::Value::List& input_descriptor_list = input_descriptors->GetList();
ASSERT_TRUE(input_descriptor_list.front().is_dict());
input_descriptor_list.front().GetDict().Set("id", "not_mdl");
EXPECT_EQ(InterstitialType::kLowRisk,
ComputeInterstitialType(kOpenid4vpProtocol, std::move(request)));
}
TEST_F(DigitalIdentityRequestImplTest,
Openid4VpProtocol_ComputeInterstitialType_NoPresentationDefinition) {
base::Value request = GenerateOnlyAgeOpenid4VpRequest();
RemoveDictKey(request.GetDict(), "presentation_definition");
EXPECT_EQ(InterstitialType::kLowRisk,
ComputeInterstitialType(kOpenid4vpProtocol, std::move(request)));
}
TEST_F(DigitalIdentityRequestImplTest,
PreviewProtocol_ComputeInterstitialType_OnlyAgeOver) {
EXPECT_EQ(std::nullopt,
ComputeInterstitialType(kPreviewProtocol,
GenerateOnlyAgePreviewRequest()));
}
TEST_F(DigitalIdentityRequestImplTest,
PreviewProtocol_ComputeInterstitialType_OnlyAgeInYears) {
base::Value request = GenerateOnlyAgePreviewRequest();
ASSERT_TRUE(SetFieldNameValue(request, "age_in_years"));
EXPECT_EQ(std::nullopt,
ComputeInterstitialType(kPreviewProtocol, std::move(request)));
}
TEST_F(DigitalIdentityRequestImplTest,
PreviewProtocol_ComputeIntersitialType_OnlyAgeBirthYear) {
base::Value request = GenerateOnlyAgePreviewRequest();
ASSERT_TRUE(SetFieldNameValue(request, "age_birth_year"));
EXPECT_EQ(std::nullopt,
ComputeInterstitialType(kPreviewProtocol, std::move(request)));
}
TEST_F(DigitalIdentityRequestImplTest,
PreviewProtocol_ComputeIntersitialType_OnlyBirthDate) {
base::Value request = GenerateOnlyAgePreviewRequest();
ASSERT_TRUE(SetFieldNameValue(request, "birth_date"));
EXPECT_EQ(std::nullopt,
ComputeInterstitialType(kPreviewProtocol, std::move(request)));
}
TEST_F(DigitalIdentityRequestImplTest,
PreviewProtocol_ComputeIntersitialType_GivenName) {
base::Value request = GenerateOnlyAgePreviewRequest();
ASSERT_TRUE(SetFieldNameValue(request, "given_name"));
EXPECT_EQ(InterstitialType::kLowRisk,
ComputeInterstitialType(kPreviewProtocol, std::move(request)));
}
} // namespace content