OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/android/digital_asset_links/digital_asset_links_handler
.h" |
| 6 |
| 7 #include "base/json/json_reader.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/strings/stringprintf.h" |
| 10 #include "base/values.h" |
| 11 #include "components/safe_json/safe_json_parser.h" |
| 12 #include "net/base/load_flags.h" |
| 13 #include "net/base/url_util.h" |
| 14 #include "net/http/http_response_headers.h" |
| 15 #include "net/http/http_status_code.h" |
| 16 #include "net/http/http_util.h" |
| 17 #include "net/traffic_annotation/network_traffic_annotation.h" |
| 18 #include "net/url_request/url_request_status.h" |
| 19 |
| 20 namespace { |
| 21 const char kDigitalAssetLinksBaseURL[] = |
| 22 "https://digitalassetlinks.googleapis.com"; |
| 23 const char kDigitalAssetLinksCheckAPI[] = "/v1/assetlinks:check?"; |
| 24 const char kTargetOriginParam[] = "source.web.site"; |
| 25 const char kSourcePackageNameParam[] = "target.androidApp.packageName"; |
| 26 const char kSourceFingerprintParam[] = |
| 27 "target.androidApp.certificate.sha256Fingerprint"; |
| 28 const char kRelationshipParam[] = "relation"; |
| 29 |
| 30 GURL GetUrlForCheckingRelationship(const std::string& web_domain, |
| 31 const std::string& package_name, |
| 32 const std::string& fingerprint, |
| 33 const std::string& relationship) { |
| 34 GURL request_url = |
| 35 GURL(kDigitalAssetLinksBaseURL).Resolve(kDigitalAssetLinksCheckAPI); |
| 36 request_url = |
| 37 net::AppendQueryParameter(request_url, kTargetOriginParam, web_domain); |
| 38 request_url = net::AppendQueryParameter(request_url, kSourcePackageNameParam, |
| 39 package_name); |
| 40 request_url = net::AppendQueryParameter(request_url, kSourceFingerprintParam, |
| 41 fingerprint); |
| 42 request_url = |
| 43 net::AppendQueryParameter(request_url, kRelationshipParam, relationship); |
| 44 DCHECK(request_url.is_valid()); |
| 45 return request_url; |
| 46 } |
| 47 } // namespace |
| 48 |
| 49 namespace digital_asset_links { |
| 50 |
| 51 const char kDigitalAssetLinksCheckResponseKeyLinked[] = "linked"; |
| 52 |
| 53 DigitalAssetLinksHandler::DigitalAssetLinksHandler( |
| 54 const scoped_refptr<net::URLRequestContextGetter>& request_context) |
| 55 : request_context_(request_context), weak_ptr_factory_(this) {} |
| 56 |
| 57 DigitalAssetLinksHandler::~DigitalAssetLinksHandler() {} |
| 58 |
| 59 void DigitalAssetLinksHandler::OnURLFetchComplete( |
| 60 const net::URLFetcher* source) { |
| 61 if (!source->GetStatus().is_success() || |
| 62 source->GetResponseCode() != net::HTTP_OK) { |
| 63 LOG(WARNING) << base::StringPrintf( |
| 64 "Digital Asset Links endpoint responded with code %d.", |
| 65 source->GetResponseCode()); |
| 66 callback_.Run(nullptr); |
| 67 return; |
| 68 } |
| 69 |
| 70 std::string response_body; |
| 71 source->GetResponseAsString(&response_body); |
| 72 |
| 73 safe_json::SafeJsonParser::Parse( |
| 74 response_body, |
| 75 base::Bind(&DigitalAssetLinksHandler::OnJSONParseSucceeded, |
| 76 weak_ptr_factory_.GetWeakPtr()), |
| 77 base::Bind(&DigitalAssetLinksHandler::OnJSONParseFailed, |
| 78 weak_ptr_factory_.GetWeakPtr())); |
| 79 |
| 80 url_fetcher_.reset(nullptr); |
| 81 } |
| 82 |
| 83 void DigitalAssetLinksHandler::OnJSONParseSucceeded( |
| 84 std::unique_ptr<base::Value> result) { |
| 85 callback_.Run(base::DictionaryValue::From(std::move(result))); |
| 86 } |
| 87 |
| 88 void DigitalAssetLinksHandler::OnJSONParseFailed( |
| 89 const std::string& error_message) { |
| 90 LOG(WARNING) |
| 91 << base::StringPrintf( |
| 92 "Digital Asset Links response parsing failed with message:") |
| 93 << error_message; |
| 94 callback_.Run(nullptr); |
| 95 } |
| 96 |
| 97 bool DigitalAssetLinksHandler::CheckDigitalAssetLinkRelationship( |
| 98 RelationshipCheckResultCallback callback, |
| 99 const std::string& web_domain, |
| 100 const std::string& package_name, |
| 101 const std::string& fingerprint, |
| 102 const std::string& relationship) { |
| 103 GURL request_url = GetUrlForCheckingRelationship(web_domain, package_name, |
| 104 fingerprint, relationship); |
| 105 |
| 106 if (!request_url.is_valid()) |
| 107 return false; |
| 108 |
| 109 // Resetting both the callback and URLFetcher here to ensure that any previous |
| 110 // requests will never get a OnUrlFetchComplete. This effectively cancels |
| 111 // any checks that was done over this handler. |
| 112 callback_ = callback; |
| 113 |
| 114 net::NetworkTrafficAnnotationTag traffic_annotation = |
| 115 net::DefineNetworkTrafficAnnotation("digital_asset_links", R"( |
| 116 semantics { |
| 117 sender: "Digital Asset Links Handler" |
| 118 description: |
| 119 "Digital Asset Links APIs allows any caller to check pre declared" |
| 120 "relationships between two assets which can be either web domains" |
| 121 "or native applications. This requests checks for a specific " |
| 122 "relationship declared by a web site with an Android application" |
| 123 trigger: |
| 124 "When the related application makes a claim to have the queried" |
| 125 "relationship with the web domain" |
| 126 destination: WEBSITE |
| 127 } |
| 128 policy { |
| 129 cookies_allowed: true |
| 130 cookies_store: "user" |
| 131 setting: "Not user controlled. But the verification is a trusted API" |
| 132 "that doesn't use user data" |
| 133 policy_exception_justification: |
| 134 "Not implemented, considered not useful as no content is being " |
| 135 "uploaded; this request merely downloads the resources on the web." |
| 136 })"); |
| 137 url_fetcher_ = net::URLFetcher::Create(0, request_url, net::URLFetcher::GET, |
| 138 this, traffic_annotation); |
| 139 url_fetcher_->SetAutomaticallyRetryOn5xx(false); |
| 140 url_fetcher_->SetRequestContext(request_context_.get()); |
| 141 url_fetcher_->Start(); |
| 142 return true; |
| 143 } |
| 144 |
| 145 } // namespace digital_asset_links |
OLD | NEW |