Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(372)

Side by Side Diff: content/common/experiments/api_key.cc

Issue 1521063003: Add API Key parsing for experimental APIs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Nits Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/common/experiments/api_key.h ('k') | content/common/experiments/api_key_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 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 "content/common/experiments/api_key.h"
6
7 #include <vector>
8
9 #include "base/base64.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_split.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/time/time.h"
15 #include "url/origin.h"
16
17 namespace content {
18
19 namespace {
20
21 const char* kApiKeyFieldSeparator = "|";
22 }
23
24 ApiKey::~ApiKey() {}
25
26 scoped_ptr<ApiKey> ApiKey::Parse(const std::string& key_text) {
27 if (key_text.empty()) {
28 return nullptr;
29 }
30
31 // API Key should resemble:
32 // signature|origin|api_name|expiry_timestamp
33 std::vector<std::string> parts =
34 SplitString(key_text, kApiKeyFieldSeparator, base::KEEP_WHITESPACE,
35 base::SPLIT_WANT_ALL);
36 if (parts.size() != 4) {
37 return nullptr;
38 }
39
40 const std::string& signature = parts[0];
41 const std::string& origin_string = parts[1];
42 const std::string& api_name = parts[2];
43 const std::string& expiry_string = parts[3];
44
45 uint64_t expiry_timestamp;
46 if (!base::StringToUint64(expiry_string, &expiry_timestamp)) {
47 return nullptr;
48 }
49
50 // Ensure that the origin is a valid (non-unique) origin URL
51 GURL origin_url(origin_string);
52 if (url::Origin(origin_url).unique()) {
53 return nullptr;
54 }
55
56 // signed data is (origin + "|" + api_name + "|" + expiry).
57 std::string data = key_text.substr(signature.length() + 1);
58
59 return make_scoped_ptr(
60 new ApiKey(signature, data, origin_url, api_name, expiry_timestamp));
61 }
62
63 ApiKey::ApiKey(const std::string& signature,
64 const std::string& data,
65 const GURL& origin,
66 const std::string& api_name,
67 uint64_t expiry_timestamp)
68 : signature_(signature),
69 data_(data),
70 origin_(origin),
71 api_name_(api_name),
72 expiry_timestamp_(expiry_timestamp) {}
73
74 bool ApiKey::IsAppropriate(const std::string& origin,
75 const std::string& api_name) const {
76 return ValidateOrigin(origin) && ValidateApiName(api_name);
77 }
78
79 bool ApiKey::IsValid(const base::Time& now) const {
80 // TODO(iclelland): Validate signature on key data here as well.
81 // https://crbug.com/543215
82 return ValidateDate(now);
83 }
84
85 bool ApiKey::ValidateOrigin(const std::string& origin) const {
86 return GURL(origin) == origin_;
87 }
88
89 bool ApiKey::ValidateApiName(const std::string& api_name) const {
90 return base::EqualsCaseInsensitiveASCII(api_name, api_name_);
91 }
92
93 bool ApiKey::ValidateDate(const base::Time& now) const {
94 base::Time expiry_time = base::Time::FromDoubleT((double)expiry_timestamp_);
95 return expiry_time > now;
96 }
97
98 } // namespace content
OLDNEW
« no previous file with comments | « content/common/experiments/api_key.h ('k') | content/common/experiments/api_key_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698