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 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
|
// 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/functional/callback_helpers.h"
#include "base/json/json_reader.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/mock_callback.h"
#include "base/test/scoped_command_line.h"
#include "base/test/scoped_feature_list.h"
#include "base/values.h"
#include "content/browser/webid/test/mock_digital_identity_provider.h"
#include "content/browser/webid/test/stub_digital_identity_provider.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/digital_identity_provider.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
#include "content/test/test_render_frame_host.h"
#include "content/test/test_render_view_host.h"
#include "services/data_decoder/public/cpp/data_decoder.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/mojom/webid/digital_identity_request.mojom.h"
namespace content {
namespace {
constexpr char kOpenid4vpProtocol[] = "openid4vp";
constexpr char kPreviewProtocol[] = "preview";
using base::Value;
using base::ValueView;
using testing::_;
using testing::DoAll;
using testing::Eq;
using testing::Optional;
using testing::WithArg;
using InterstitialType = content::DigitalIdentityInterstitialType;
using DigitalCredentialCreateRequestPtr =
blink::mojom::DigitalCredentialCreateRequestPtr;
using DigitalCredentialCreateRequest =
blink::mojom::DigitalCredentialCreateRequest;
using DigitalCredentialGetRequestPtr =
blink::mojom::DigitalCredentialGetRequestPtr;
using DigitalCredentialGetRequest = blink::mojom::DigitalCredentialGetRequest;
using RequestDigitalIdentityStatus = blink::mojom::RequestDigitalIdentityStatus;
using DigitalIdentityCallback =
DigitalIdentityProvider::DigitalIdentityCallback;
using DigitalCredential = DigitalIdentityProvider::DigitalCredential;
using GetCallback = blink::mojom::DigitalIdentityRequest::GetCallback;
using RequestData = blink::mojom::RequestData;
// 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(RenderFrameHost& render_frame_host) 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 GenerateOnlyAgeOpenid4VpRequestWithPresentationDefinition() {
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 GenerateOnlyAgeOpenid4VpRequestWithDCQL() {
constexpr char kJson[] = R"({
"response_type": "vp_token",
"response_mode": "w3c_dc_api",
"client_id": "web-origin:https://www.digital-credentials.dev",
"nonce": "d_xvsQ_PF1oPVZbjAfWu_xgwh3dJf_W5zgWB3U2xWw8",
"dcql_query": {
"credentials": [
{
"id": "cred1",
"format": "mso_mdoc",
"meta": {
"doctype_value": "org.iso.18013.5.1.mDL"
},
"claims": [
{
"path": [
"org.iso.18013.5.1",
"age_over_21"
]
}
]
}
]
}
})";
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 with Presentation Definition 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 an Openid4VpRequest with DCQL on the fly.
bool SetDCQLPathItem(base::Value& to_modify,
const std::string& field_name_value) {
base::Value* paths = FindValueWithKey(to_modify, "path");
if (HasNoListElements(paths)) {
return false;
}
paths->GetList().resize(1);
paths->GetList().Append(field_name_value);
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;
}
} // anonymous namespace
class DigitalIdentityRequestImplInterstitialTest
: public RenderViewHostTestHarness {
public:
void SetUp() override {
RenderViewHostTestHarness::SetUp();
scoped_feature_list_.InitAndEnableFeatureWithParameters(
features::kWebIdentityDigitalCredentials, {{"dialog", ""}});
}
std::optional<InterstitialType> ComputeInterstitialType(
const std::string& protocol,
base::Value request_data,
bool are_origins_low_risk = false) {
auto provider = std::make_unique<TestDigitalIdentityProviderWithCustomRisk>(
are_origins_low_risk);
std::vector<ProtocolAndParsedRequest> requests;
requests.emplace_back(protocol, std::move(request_data));
return DigitalIdentityRequestImpl::ComputeInterstitialType(
*main_rfh(), provider.get(), std::move(requests));
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
TEST_F(
DigitalIdentityRequestImplInterstitialTest,
Openid4VpProtocolPresentationDefinition_ComputeInterstitialType_OnlyAgeOver) {
EXPECT_EQ(ComputeInterstitialType(
kOpenid4vpProtocol,
GenerateOnlyAgeOpenid4VpRequestWithPresentationDefinition()),
std::nullopt);
}
TEST_F(
DigitalIdentityRequestImplInterstitialTest,
Openid4VpProtocolPresentationDefinition_ComputeInterstitialType_OnlyAgeInYears) {
base::Value request =
GenerateOnlyAgeOpenid4VpRequestWithPresentationDefinition();
ASSERT_TRUE(SetPathItem(request, "$['org.iso.18013.5.1']['age_in_years']"));
EXPECT_EQ(ComputeInterstitialType(kOpenid4vpProtocol, std::move(request)),
std::nullopt);
}
TEST_F(
DigitalIdentityRequestImplInterstitialTest,
Openid4VpProtocolPresentationDefinition_ComputeIntersitialType_OnlyAgeBirthYear) {
base::Value request =
GenerateOnlyAgeOpenid4VpRequestWithPresentationDefinition();
ASSERT_TRUE(SetPathItem(request, "$['org.iso.18013.5.1']['age_birth_year']"));
EXPECT_EQ(ComputeInterstitialType(kOpenid4vpProtocol, std::move(request)),
std::nullopt);
}
TEST_F(
DigitalIdentityRequestImplInterstitialTest,
Openid4VpProtocolPresentationDefinition_ComputeIntersitialType_OnlyBirthDate) {
base::Value request =
GenerateOnlyAgeOpenid4VpRequestWithPresentationDefinition();
ASSERT_TRUE(SetPathItem(request, "$['org.iso.18013.5.1']['birth_date']"));
EXPECT_EQ(ComputeInterstitialType(kOpenid4vpProtocol, std::move(request)),
std::nullopt);
}
base::Value GenerateNonAgeOpenid4VpRequest() {
base::Value request =
GenerateOnlyAgeOpenid4VpRequestWithPresentationDefinition();
CHECK(SetPathItem(request, "$['org.iso.18013.5.1']['given_name']"));
return request;
}
TEST_F(
DigitalIdentityRequestImplInterstitialTest,
Openid4VpProtocolPresentationDefinition_ComputeIntersitialType_OnlyNonAgeDataElement) {
EXPECT_EQ(ComputeInterstitialType(kOpenid4vpProtocol,
GenerateNonAgeOpenid4VpRequest()),
InterstitialType::kLowRisk);
}
TEST_F(
DigitalIdentityRequestImplInterstitialTest,
Openid4VpProtocolPresentationDefinition_LowRiskOriginTakesPrecedenceOverRequestType) {
EXPECT_EQ(ComputeInterstitialType(kOpenid4vpProtocol,
GenerateNonAgeOpenid4VpRequest(),
/*are_origins_low_risk=*/true),
std::nullopt);
}
TEST_F(
DigitalIdentityRequestImplInterstitialTest,
Openid4VpProtocolPresentationDefinition_ComputeInterstitialType_EmptyPathList) {
base::Value request =
GenerateOnlyAgeOpenid4VpRequestWithPresentationDefinition();
base::Value* paths = FindValueWithKey(request, "path");
ASSERT_TRUE(IsNonEmptyList(paths));
paths->GetList().resize(0);
EXPECT_EQ(ComputeInterstitialType(kOpenid4vpProtocol, std::move(request)),
InterstitialType::kLowRisk);
}
TEST_F(
DigitalIdentityRequestImplInterstitialTest,
Openid4VpProtocolPresentationDefinition_ComputeInterstitialType_RequestMultiplePaths) {
base::Value request =
GenerateOnlyAgeOpenid4VpRequestWithPresentationDefinition();
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(ComputeInterstitialType(kOpenid4vpProtocol, std::move(request)),
InterstitialType::kLowRisk);
}
TEST_F(DigitalIdentityRequestImplInterstitialTest,
Openid4VpProtocolPresentationDefinition_ComputeInterstitialType_NoPath) {
base::Value request =
GenerateOnlyAgeOpenid4VpRequestWithPresentationDefinition();
base::Value* fields = FindValueWithKey(request, "fields");
ASSERT_TRUE(IsNonEmptyList(fields));
RemoveDictKey(fields->GetList().front().GetDict(), "path");
EXPECT_EQ(ComputeInterstitialType(kOpenid4vpProtocol, std::move(request)),
InterstitialType::kLowRisk);
}
TEST_F(
DigitalIdentityRequestImplInterstitialTest,
Openid4VpProtocolPresentationDefinition_ComputeInterstitialType_EmptyFieldsList) {
base::Value request =
GenerateOnlyAgeOpenid4VpRequestWithPresentationDefinition();
base::Value* fields = FindValueWithKey(request, "fields");
ASSERT_TRUE(IsNonEmptyList(fields));
fields->GetList().resize(0);
EXPECT_EQ(ComputeInterstitialType(kOpenid4vpProtocol, std::move(request)),
InterstitialType::kLowRisk);
}
TEST_F(
DigitalIdentityRequestImplInterstitialTest,
Openid4VpProtocolPresentationDefinition_ComputeInterstitialType_RequestMultipleAgeAssertions) {
base::Value request =
GenerateOnlyAgeOpenid4VpRequestWithPresentationDefinition();
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(ComputeInterstitialType(kOpenid4vpProtocol, std::move(request)),
InterstitialType::kLowRisk);
}
TEST_F(
DigitalIdentityRequestImplInterstitialTest,
Openid4VpProtocolPresentationDefinition_ComputeInterstitialType_RequestMultipleFields) {
base::Value request =
GenerateOnlyAgeOpenid4VpRequestWithPresentationDefinition();
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(ComputeInterstitialType(kOpenid4vpProtocol, std::move(request)),
InterstitialType::kLowRisk);
}
TEST_F(
DigitalIdentityRequestImplInterstitialTest,
Openid4VpProtocolPresentationDefinition_ComputeInterstitialType_NoConstraints) {
base::Value request =
GenerateOnlyAgeOpenid4VpRequestWithPresentationDefinition();
base::Value* input_descriptors =
FindValueWithKey(request, "input_descriptors");
ASSERT_TRUE(IsNonEmptyList(input_descriptors));
RemoveDictKey(input_descriptors->GetList().front().GetDict(), "constraints");
EXPECT_EQ(ComputeInterstitialType(kOpenid4vpProtocol, std::move(request)),
InterstitialType::kLowRisk);
}
TEST_F(
DigitalIdentityRequestImplInterstitialTest,
Openid4VpProtocolPresentationDefinition_ComputeInterstitialType_EmptyInputDescriptorList) {
base::Value request =
GenerateOnlyAgeOpenid4VpRequestWithPresentationDefinition();
base::Value* input_descriptors =
FindValueWithKey(request, "input_descriptors");
ASSERT_TRUE(IsNonEmptyList(input_descriptors));
input_descriptors->GetList().resize(0);
EXPECT_EQ(ComputeInterstitialType(kOpenid4vpProtocol, std::move(request)),
InterstitialType::kLowRisk);
}
TEST_F(
DigitalIdentityRequestImplInterstitialTest,
Openid4VpProtocolPresentationDefinition_ComputeInterstitialType_RequestMultipleDocuments) {
base::Value request =
GenerateOnlyAgeOpenid4VpRequestWithPresentationDefinition();
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(ComputeInterstitialType(kOpenid4vpProtocol, std::move(request)),
InterstitialType::kLowRisk);
}
TEST_F(
DigitalIdentityRequestImplInterstitialTest,
Openid4VpProtocolPresentationDefinition_ComputeInterstitialType_NonMdlInputDescriptorId) {
base::Value request =
GenerateOnlyAgeOpenid4VpRequestWithPresentationDefinition();
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(ComputeInterstitialType(kOpenid4vpProtocol, std::move(request)),
InterstitialType::kLowRisk);
}
TEST_F(
DigitalIdentityRequestImplInterstitialTest,
Openid4VpProtocolPresentationDefinition_ComputeInterstitialType_NoPresentationDefinition) {
base::Value request =
GenerateOnlyAgeOpenid4VpRequestWithPresentationDefinition();
RemoveDictKey(request.GetDict(), "presentation_definition");
EXPECT_EQ(ComputeInterstitialType(kOpenid4vpProtocol, std::move(request)),
InterstitialType::kLowRisk);
}
TEST_F(DigitalIdentityRequestImplInterstitialTest,
PreviewProtocol_ComputeInterstitialType_OnlyAgeOver) {
EXPECT_EQ(ComputeInterstitialType(kPreviewProtocol,
GenerateOnlyAgePreviewRequest()),
std::nullopt);
}
TEST_F(DigitalIdentityRequestImplInterstitialTest,
PreviewProtocol_ComputeInterstitialType_OnlyAgeInYears) {
base::Value request = GenerateOnlyAgePreviewRequest();
ASSERT_TRUE(SetFieldNameValue(request, "age_in_years"));
EXPECT_EQ(ComputeInterstitialType(kPreviewProtocol, std::move(request)),
std::nullopt);
}
TEST_F(DigitalIdentityRequestImplInterstitialTest,
PreviewProtocol_ComputeIntersitialType_OnlyAgeBirthYear) {
base::Value request = GenerateOnlyAgePreviewRequest();
ASSERT_TRUE(SetFieldNameValue(request, "age_birth_year"));
EXPECT_EQ(ComputeInterstitialType(kPreviewProtocol, std::move(request)),
std::nullopt);
}
TEST_F(DigitalIdentityRequestImplInterstitialTest,
PreviewProtocol_ComputeIntersitialType_OnlyBirthDate) {
base::Value request = GenerateOnlyAgePreviewRequest();
ASSERT_TRUE(SetFieldNameValue(request, "birth_date"));
EXPECT_EQ(ComputeInterstitialType(kPreviewProtocol, std::move(request)),
std::nullopt);
}
TEST_F(DigitalIdentityRequestImplInterstitialTest,
PreviewProtocol_ComputeIntersitialType_GivenName) {
base::Value request = GenerateOnlyAgePreviewRequest();
ASSERT_TRUE(SetFieldNameValue(request, "given_name"));
EXPECT_EQ(ComputeInterstitialType(kPreviewProtocol, std::move(request)),
InterstitialType::kLowRisk);
}
TEST_F(DigitalIdentityRequestImplInterstitialTest,
Openid4VpProtocolDCQL_ComputeInterstitialType_OnlyAgeOver) {
EXPECT_EQ(ComputeInterstitialType(kOpenid4vpProtocol,
GenerateOnlyAgeOpenid4VpRequestWithDCQL()),
std::nullopt);
}
TEST_F(DigitalIdentityRequestImplInterstitialTest,
Openid4VpProtocolDCQL_ComputeIntersitialType_OnlyAgeBirthYear) {
base::Value request = GenerateOnlyAgeOpenid4VpRequestWithDCQL();
ASSERT_TRUE(SetDCQLPathItem(request, "age_birth_year"));
EXPECT_EQ(ComputeInterstitialType(kOpenid4vpProtocol, std::move(request)),
std::nullopt);
}
TEST_F(DigitalIdentityRequestImplInterstitialTest,
Openid4VpProtocolDCQL_ComputeIntersitialType_OnlyBirthDate) {
base::Value request = GenerateOnlyAgeOpenid4VpRequestWithDCQL();
ASSERT_TRUE(SetDCQLPathItem(request, "birth_date"));
EXPECT_EQ(ComputeInterstitialType(kOpenid4vpProtocol, std::move(request)),
std::nullopt);
}
TEST_F(DigitalIdentityRequestImplInterstitialTest,
Openid4VpProtocolDCQL_ComputeIntersitialType_GivenName) {
base::Value request = GenerateOnlyAgeOpenid4VpRequestWithDCQL();
ASSERT_TRUE(SetDCQLPathItem(request, "given_name"));
EXPECT_EQ(ComputeInterstitialType(kOpenid4vpProtocol, std::move(request)),
InterstitialType::kLowRisk);
}
TEST_F(DigitalIdentityRequestImplInterstitialTest,
Openid4VpProtocolDCQL_ComputeIntersitialType_GivenNameAndAgeOver) {
base::Value request = ParseJsonAndCheck(R"({
"response_type": "vp_token",
"response_mode": "dc_api",
"nonce": "EReTrXMsLOF7BTUnvmiuYqIbqc9zgEcHON9qalEKtP4",
"dcql_query": {
"credentials": [
{
"id": "cred1",
"format": "mso_mdoc",
"meta": {
"doctype_value": "org.iso.18013.5.1.mDL"
},
"claims": [
{
"path": [
"org.iso.18013.5.1",
"given_name"
]
},
{
"path": [
"org.iso.18013.5.1",
"age_over_21"
]
}
]
}
]
}
})");
EXPECT_EQ(ComputeInterstitialType(kOpenid4vpProtocol, std::move(request)),
InterstitialType::kLowRisk);
}
TEST_F(DigitalIdentityRequestImplInterstitialTest,
Openid4VpAndPreviewProtocol_ComputeIntersitialType_AgeOver) {
base::Value openid4vp_request = GenerateOnlyAgeOpenid4VpRequestWithDCQL();
base::Value preview_request = GenerateOnlyAgePreviewRequest();
std::vector<ProtocolAndParsedRequest> requests;
requests.emplace_back(kOpenid4vpProtocol, std::move(openid4vp_request));
requests.emplace_back(kPreviewProtocol, std::move(preview_request));
auto provider = std::make_unique<TestDigitalIdentityProviderWithCustomRisk>(
/*are_origins_low_risk=*/false);
EXPECT_EQ(DigitalIdentityRequestImpl::ComputeInterstitialType(
*main_rfh(), provider.get(), std::move(requests)),
std::nullopt);
}
TEST_F(DigitalIdentityRequestImplInterstitialTest,
Openid4VpAndPreviewProtocol_ComputeIntersitialType_AgeOverAndGivenName) {
base::Value openid4vp_request = GenerateOnlyAgeOpenid4VpRequestWithDCQL();
base::Value preview_request = GenerateOnlyAgePreviewRequest();
ASSERT_TRUE(SetFieldNameValue(preview_request, "given_name"));
std::vector<ProtocolAndParsedRequest> requests;
requests.emplace_back(kOpenid4vpProtocol, std::move(openid4vp_request));
requests.emplace_back(kPreviewProtocol, std::move(preview_request));
auto provider = std::make_unique<TestDigitalIdentityProviderWithCustomRisk>(
/*are_origins_low_risk=*/false);
EXPECT_EQ(DigitalIdentityRequestImpl::ComputeInterstitialType(
*main_rfh(), provider.get(), std::move(requests)),
InterstitialType::kLowRisk);
}
TEST_F(DigitalIdentityRequestImplInterstitialTest,
Openid4VpProtocolDCQL_ComputeIntersitialType_MalformedRequest) {
// Malformed request that's missing the claim_name entry.
base::Value malformed_request = ParseJsonAndCheck(R"({
"response_type": "vp_token",
"response_mode": "w3c_dc_api",
"client_id": "web-origin:https://www.digital-credentials.dev",
"nonce": "CL0BDiED_T5qDttEddJASo8Ft5yR9C0wmLy6WFtHsCQ",
"dcql_query": {
"credentials": [
{
"id": "cred1",
"format": "mso_mdoc",
"meta": {
"doctype_value": "org.iso.18013.5.1.mDL"
},
"claims": [
{
"namespace": "org.iso.18013.5.1",
},
]
}
]
}
})");
EXPECT_EQ(ComputeInterstitialType(kOpenid4vpProtocol,
std::move(malformed_request)),
InterstitialType::kLowRisk);
}
class DigitalIdentityRequestImplWithCreationEnabledTest
: public RenderViewHostTestHarness {
public:
void SetUp() override {
RenderViewHostTestHarness::SetUp();
digital_identity_request_impl_ = DigitalIdentityRequestImpl::CreateInstance(
*web_contents()->GetPrimaryMainFrame(),
request_remote_.BindNewPipeAndPassReceiver());
content::RenderFrameHostTester::For(web_contents()->GetPrimaryMainFrame())
->SimulateUserActivation();
command_line_.GetProcessCommandLine()->AppendSwitch(
switches::kUseFakeUIForDigitalIdentity);
}
void TearDown() override {
// Reset here to avoid dangling pointer upon the destruction of the rvh.
digital_identity_request_impl_ = nullptr;
RenderViewHostTestHarness::TearDown();
}
DigitalIdentityRequestImpl* digital_identity_request_impl() {
return digital_identity_request_impl_.get();
}
private:
base::test::ScopedFeatureList scoped_feature_list_{
features::kWebIdentityDigitalCredentialsCreation};
base::test::ScopedCommandLine command_line_;
mojo::Remote<blink::mojom::DigitalIdentityRequest> request_remote_;
base::WeakPtr<DigitalIdentityRequestImpl> digital_identity_request_impl_;
};
TEST_F(DigitalIdentityRequestImplWithCreationEnabledTest,
ShouldReturnErrorWhenNoRequest) {
base::MockCallback<DigitalIdentityRequestImpl::CreateCallback> callback;
EXPECT_CALL(callback,
Run(RequestDigitalIdentityStatus::kErrorNoRequests, _, _));
digital_identity_request_impl()->Create({}, callback.Get());
}
TEST_F(DigitalIdentityRequestImplWithCreationEnabledTest,
ShouldReturnErrorWhenAnotherRequestIsInFlight) {
DigitalCredentialCreateRequestPtr digital_credential_request1 =
DigitalCredentialCreateRequest::New();
digital_credential_request1->protocol = "protocol1";
base::Value::Dict request1_data;
request1_data.Set("data", "request data 1");
digital_credential_request1->data = base::Value(std::move(request1_data));
DigitalCredentialCreateRequestPtr digital_credential_request2 =
DigitalCredentialCreateRequest::New();
digital_credential_request2->protocol = "protocol2";
base::Value::Dict request2_data;
request2_data.Set("data", "request data 2");
digital_credential_request2->data = base::Value(std::move(request2_data));
std::vector<blink::mojom::DigitalCredentialCreateRequestPtr> requests1;
requests1.push_back(std::move(digital_credential_request1));
digital_identity_request_impl()->Create(std::move(requests1),
base::DoNothing());
base::MockCallback<DigitalIdentityRequestImpl::CreateCallback> callback;
EXPECT_CALL(callback,
Run(RequestDigitalIdentityStatus::kErrorTooManyRequests, _, _));
std::vector<blink::mojom::DigitalCredentialCreateRequestPtr> requests2;
requests2.push_back(std::move(digital_credential_request2));
digital_identity_request_impl()->Create(std::move(requests2), callback.Get());
}
TEST_F(DigitalIdentityRequestImplWithCreationEnabledTest,
ShouldSucceedWhenValidRequest) {
const std::string kProtocol = "protocol";
base::MockCallback<DigitalIdentityRequestImpl::CreateCallback> callback;
DigitalCredentialCreateRequestPtr digital_credential_request =
DigitalCredentialCreateRequest::New();
digital_credential_request->protocol = kProtocol;
base::Value::Dict request_data;
request_data.Set("data", "request data");
digital_credential_request->data = base::Value(std::move(request_data));
std::vector<blink::mojom::DigitalCredentialCreateRequestPtr> requests;
requests.push_back(std::move(digital_credential_request));
base::RunLoop run_loop;
EXPECT_CALL(callback, Run(RequestDigitalIdentityStatus::kSuccess,
testing::Optional(kProtocol), _))
.WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure()));
digital_identity_request_impl()->Create(std::move(requests), callback.Get());
run_loop.Run();
}
TEST_F(DigitalIdentityRequestImplWithCreationEnabledTest,
ShouldReturnErrorWhenAbort) {
const std::string kProtocol = "protocol";
base::MockCallback<DigitalIdentityRequestImpl::CreateCallback> callback;
DigitalCredentialCreateRequestPtr digital_credential_request =
DigitalCredentialCreateRequest::New();
digital_credential_request->protocol = kProtocol;
base::Value::Dict request_data;
request_data.Set("data", "request data");
digital_credential_request->data = base::Value(std::move(request_data));
std::vector<blink::mojom::DigitalCredentialCreateRequestPtr> requests;
requests.push_back(std::move(digital_credential_request));
EXPECT_CALL(callback,
Run(RequestDigitalIdentityStatus::kErrorCanceled, _, _));
digital_identity_request_impl()->Create(std::move(requests), callback.Get());
digital_identity_request_impl()->Abort();
}
class ContentBrowserClientWithMockDigitalIdentityProvider
: public ContentBrowserClient {
public:
ContentBrowserClientWithMockDigitalIdentityProvider() = default;
~ContentBrowserClientWithMockDigitalIdentityProvider() override = default;
// ContentBrowserClient overrides:
std::unique_ptr<DigitalIdentityProvider> CreateDigitalIdentityProvider()
override {
return std::move(provider_);
}
void SetDigitalIdentityProvider(
std::unique_ptr<DigitalIdentityProvider> provider) {
provider_ = std::move(provider);
}
private:
std::unique_ptr<DigitalIdentityProvider> provider_;
};
class DigitalIdentityRequestImplTest : public RenderViewHostTestHarness {
public:
void SetUp() override {
RenderViewHostTestHarness::SetUp();
auto mock_digital_identity_provider =
std::make_unique<MockDigitalIdentityProvider>();
mock_digital_identity_provider_ = mock_digital_identity_provider.get();
content_browser_client_.SetDigitalIdentityProvider(
std::move(mock_digital_identity_provider));
content::SetBrowserClientForTesting(&content_browser_client_);
digital_identity_request_impl_ = DigitalIdentityRequestImpl::CreateInstance(
*web_contents()->GetPrimaryMainFrame(),
request_remote_.BindNewPipeAndPassReceiver());
content::RenderFrameHostTester::For(web_contents()->GetPrimaryMainFrame())
->SimulateUserActivation();
// Tests in this fixture don't test the dialog behaviour.
scoped_feature_list_.InitAndEnableFeatureWithParameters(
features::kWebIdentityDigitalCredentials, {{"dialog", "no_dialog"}});
}
void TearDown() override {
// Reset here to avoid dangling pointer upon the destruction of the rvh.
digital_identity_request_impl_ = nullptr;
mock_digital_identity_provider_ = nullptr;
RenderViewHostTestHarness::TearDown();
}
DigitalIdentityRequestImpl* digital_identity_request_impl() {
return digital_identity_request_impl_.get();
}
MockDigitalIdentityProvider* mock_digital_identity_provider() {
return mock_digital_identity_provider_;
}
void reset_provider_pointer() { mock_digital_identity_provider_ = nullptr; }
private:
base::test::ScopedFeatureList scoped_feature_list_;
// base::test::ScopedCommandLine command_line_;
raw_ptr<MockDigitalIdentityProvider> mock_digital_identity_provider_;
ContentBrowserClientWithMockDigitalIdentityProvider content_browser_client_;
mojo::Remote<blink::mojom::DigitalIdentityRequest> request_remote_;
base::WeakPtr<DigitalIdentityRequestImpl> digital_identity_request_impl_;
};
TEST_F(DigitalIdentityRequestImplTest, ShouldGetUsingLegacyFormat) {
const std::string kProtocol = "protocol";
DigitalCredentialGetRequestPtr digital_credential_request =
DigitalCredentialGetRequest::New();
digital_credential_request->protocol = kProtocol;
digital_credential_request->data =
RequestData::NewStr("{\"data\": \"request data\"}");
std::vector<DigitalCredentialGetRequestPtr> requests;
requests.push_back(std::move(digital_credential_request));
base::RunLoop run_loop;
// Intercept the `Get()` call and verify that the request is formatted
// properly.
EXPECT_CALL(*mock_digital_identity_provider(), Get)
.WillOnce(DoAll(WithArg<2>([](ValueView request) {
Value::Dict dict = request.ToValue().GetDict().Clone();
EXPECT_TRUE(dict.contains("providers"));
for (const Value& req : *dict.FindList("providers")) {
EXPECT_TRUE(req.GetDict().contains("protocol"));
EXPECT_TRUE(req.GetDict().contains("request"));
EXPECT_TRUE(
req.GetDict().Find("request")->is_string());
}
}),
base::test::RunOnceClosure(run_loop.QuitClosure())));
digital_identity_request_impl()->Get(std::move(requests),
blink::mojom::GetRequestFormat::kLegacy,
base::DoNothing());
run_loop.Run();
}
TEST_F(DigitalIdentityRequestImplTest, ShouldGetUsingModernFormat) {
const std::string kProtocol = "protocol";
DigitalCredentialGetRequestPtr digital_credential_request =
DigitalCredentialGetRequest::New();
digital_credential_request->protocol = kProtocol;
base::Value::Dict request_data;
request_data.Set("data", "request data");
digital_credential_request->data =
RequestData::NewValue(base::Value(std::move(request_data)));
std::vector<DigitalCredentialGetRequestPtr> requests;
requests.push_back(std::move(digital_credential_request));
base::RunLoop run_loop;
// Intercept the `Get()` call and verify that the request is formatted
// properly.
EXPECT_CALL(*mock_digital_identity_provider(), Get)
.WillOnce(DoAll(WithArg<2>([](ValueView request) {
Value::Dict dict = request.ToValue().GetDict().Clone();
EXPECT_TRUE(dict.contains("requests"));
for (const Value& req : *dict.FindList("requests")) {
EXPECT_TRUE(req.GetDict().contains("protocol"));
EXPECT_TRUE(req.GetDict().contains("data"));
EXPECT_TRUE(req.GetDict().Find("data")->is_dict());
}
}),
base::test::RunOnceClosure(run_loop.QuitClosure())));
digital_identity_request_impl()->Get(std::move(requests),
blink::mojom::GetRequestFormat::kModern,
base::DoNothing());
run_loop.Run();
}
TEST_F(DigitalIdentityRequestImplTest, ShouldGetAndReturnProtocolInRequest) {
const std::string kProtocol = "protocol";
const Value kResponseData(Value::Dict().Set("token", "token data"));
DigitalCredentialGetRequestPtr digital_credential_request =
DigitalCredentialGetRequest::New();
digital_credential_request->protocol = kProtocol;
base::Value::Dict request_data;
request_data.Set("data", "request data");
digital_credential_request->data =
RequestData::NewValue(base::Value(std::move(request_data)));
std::vector<DigitalCredentialGetRequestPtr> requests;
requests.push_back(std::move(digital_credential_request));
base::RunLoop run_loop;
// Simulate a provider that returns a response without a protocol.
EXPECT_CALL(*mock_digital_identity_provider(), Get)
.WillOnce(WithArg<3>([this,
&kResponseData](DigitalIdentityCallback callback) {
// Running the `callback` will destroy the provider, reset the pointer
// to avoid dangling pointers after invoking the callback.
reset_provider_pointer();
std::move(callback).Run(
DigitalCredential(std::nullopt, kResponseData.Clone()));
}));
base::MockCallback<GetCallback> mock_callback;
// The protocol in the request should be used when invoking the callback,
// since no protocol was available in the response.
EXPECT_CALL(mock_callback, Run(RequestDigitalIdentityStatus::kSuccess,
Optional(kProtocol), _))
.WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure()));
digital_identity_request_impl()->Get(std::move(requests),
blink::mojom::GetRequestFormat::kModern,
mock_callback.Get());
run_loop.Run();
}
TEST_F(DigitalIdentityRequestImplTest, ShouldGetAndReturnProtocolInResponse) {
const std::string kProtocolInRequest = "protocol_in_request";
const std::string kProtocolInResponse = "protocol_in_response";
const Value kResponseData(Value::Dict().Set("token", "token data"));
DigitalCredentialGetRequestPtr digital_credential_request =
DigitalCredentialGetRequest::New();
digital_credential_request->protocol = kProtocolInRequest;
base::Value::Dict request_data;
request_data.Set("data", "request data");
digital_credential_request->data =
RequestData::NewValue(base::Value(std::move(request_data)));
std::vector<DigitalCredentialGetRequestPtr> requests;
requests.push_back(std::move(digital_credential_request));
base::RunLoop run_loop;
// Simulate a provider that returns a response with a protocol.
EXPECT_CALL(*mock_digital_identity_provider(), Get)
.WillOnce(WithArg<3>([this, &kProtocolInResponse,
&kResponseData](DigitalIdentityCallback callback) {
// Running the `callback` will destroy the provider, reset the pointer
// to avoid dangling pointers after invoking the callback.
reset_provider_pointer();
std::move(callback).Run(
DigitalCredential(kProtocolInResponse, kResponseData.Clone()));
}));
base::MockCallback<GetCallback> mock_callback;
// The protocol in the response should be used when invoking the callback.
EXPECT_CALL(mock_callback, Run(RequestDigitalIdentityStatus::kSuccess,
Optional(kProtocolInResponse), _))
.WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure()));
digital_identity_request_impl()->Get(std::move(requests),
blink::mojom::GetRequestFormat::kModern,
mock_callback.Get());
run_loop.Run();
}
TEST_F(DigitalIdentityRequestImplTest,
ShouldErrorUsingModernFormatWithStringRequest) {
DigitalCredentialGetRequestPtr digital_credential_request =
DigitalCredentialGetRequest::New();
digital_credential_request->protocol = "protocol";
digital_credential_request->data =
RequestData::NewStr(R"({"data": "request data"})");
std::vector<DigitalCredentialGetRequestPtr> requests;
requests.push_back(std::move(digital_credential_request));
base::RunLoop run_loop;
base::MockCallback<GetCallback> mock_callback;
// The callback should be invoked with an error because of the malformed
// request.
EXPECT_CALL(mock_callback,
Run(RequestDigitalIdentityStatus::kErrorInvalidJson, _, _))
.WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure()));
digital_identity_request_impl()->Get(std::move(requests),
blink::mojom::GetRequestFormat::kModern,
mock_callback.Get());
run_loop.Run();
}
TEST_F(DigitalIdentityRequestImplTest,
ShouldErrorUsingLegacyFormatWithValueRequest) {
DigitalCredentialGetRequestPtr digital_credential_request =
DigitalCredentialGetRequest::New();
digital_credential_request->protocol = "protocol";
base::Value::Dict request_data;
request_data.Set("data", "request data");
digital_credential_request->data =
RequestData::NewValue(base::Value(std::move(request_data)));
std::vector<DigitalCredentialGetRequestPtr> requests;
requests.push_back(std::move(digital_credential_request));
base::RunLoop run_loop;
base::MockCallback<GetCallback> mock_callback;
// The callback should be invoked with an error because of the malformed
// request.
EXPECT_CALL(mock_callback,
Run(RequestDigitalIdentityStatus::kErrorInvalidJson, _, _))
.WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure()));
digital_identity_request_impl()->Get(std::move(requests),
blink::mojom::GetRequestFormat::kLegacy,
mock_callback.Get());
run_loop.Run();
}
TEST_F(DigitalIdentityRequestImplTest,
ShouldErrorWhenMultipleRequestsAndNoProtocolInResponse) {
const Value kResponseData(Value::Dict().Set("token", "token data"));
std::vector<DigitalCredentialGetRequestPtr> requests;
DigitalCredentialGetRequestPtr request1 = DigitalCredentialGetRequest::New();
request1->protocol = "protocol1";
base::Value::Dict request1_data;
request1_data.Set("data", "request1 data");
request1->data = RequestData::NewValue(base::Value(std::move(request1_data)));
DigitalCredentialGetRequestPtr request2 = DigitalCredentialGetRequest::New();
request2->protocol = "protocol2";
base::Value::Dict request2_data;
request2_data.Set("data", "request2 data");
request2->data = RequestData::NewValue(base::Value(std::move(request2_data)));
requests.push_back(std::move(request1));
requests.push_back(std::move(request2));
base::RunLoop run_loop;
// Simulate a provider that returns a response without a protocol.
EXPECT_CALL(*mock_digital_identity_provider(), Get)
.WillOnce(
WithArg<3>([this, &kResponseData](DigitalIdentityCallback callback) {
// Running the `callback` will destroy the provider, reset the
// pointer to avoid dangling pointers after invoking the callback.
reset_provider_pointer();
std::move(callback).Run(DigitalCredential(
/*protocol=*/std::nullopt, kResponseData.Clone()));
}));
base::MockCallback<GetCallback> mock_callback;
// The callback should be invoked with an error since the digital wallet
// response indicates that it doesn't support multiple requests.
EXPECT_CALL(mock_callback, Run(RequestDigitalIdentityStatus::kError, _, _))
.WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure()));
digital_identity_request_impl()->Get(std::move(requests),
blink::mojom::GetRequestFormat::kModern,
mock_callback.Get());
run_loop.Run();
}
} // namespace content
|