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

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: Merge blink and chromium code; fixing issues from review Created 4 years, 12 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
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
16 namespace content {
17
18 namespace {
19
20 const char* kApiKeyFieldSeparator = "|";
21 }
22
23 ApiKey::~ApiKey() {}
24
25 scoped_ptr<ApiKey> ApiKey::Parse(const std::string& key_text) {
26 if (key_text.empty()) {
27 return nullptr;
28 }
29
30 // API Key should resemble:
31 // signature|origin|api_name|expiry_timestamp
32 std::vector<std::string> parts =
33 SplitString(key_text, kApiKeyFieldSeparator, base::KEEP_WHITESPACE,
34 base::SPLIT_WANT_ALL);
35 if (parts.size() != 4) {
36 return nullptr;
37 }
38
39 const std::string& signature = parts[0];
40 const std::string& origin_string = parts[1];
41 const std::string& api_name = parts[2];
42 const std::string& expiry_string = parts[3];
43
44 uint64_t expiry_timestamp;
45 if (!base::StringToUint64(expiry_string, &expiry_timestamp)) {
46 return nullptr;
47 }
48
49 // signed data is (origin + "|" + api_name + "|" + expiry).
50 const std::string& data = key_text.substr(key_text.find('|') + 1);
Marijn Kruisselbrink 2016/01/05 00:59:14 '|' should probably be the same kApiKeyFieldSepara
iclelland 2016/01/05 20:26:31 Yes, missed that one, thanks...
51
52 return scoped_ptr<ApiKey>(new ApiKey(signature, data, GURL(origin_string),
Marijn Kruisselbrink 2016/01/05 00:59:14 nit: return make_scoped_ptr(new ApiKey(..)) is I t
iclelland 2016/01/05 20:26:31 Done.
53 api_name, expiry_timestamp));
54 }
55
56 ApiKey::ApiKey(const std::string& signature,
57 const std::string& data,
58 const GURL& origin,
59 const std::string& api_name,
60 uint64_t expiry_timestamp)
61 : signature_(signature),
62 data_(data),
63 origin_(origin),
64 api_name_(api_name),
65 expiry_timestamp_(expiry_timestamp) {}
66
67 bool ApiKey::IsAppropriate(const std::string& origin,
68 const std::string& api_name) const {
69 return ValidateOrigin(origin) && ValidateApiName(api_name);
70 }
71
72 bool ApiKey::IsValid(const base::Time& now) const {
73 // TODO(iclelland): Validate signature on key data here as well.
74 // https://crbug.com/543215
75 return ValidateDate(now);
76 }
77
78 bool ApiKey::ValidateOrigin(const std::string& origin) const {
79 return GURL(origin) == origin_;
80 }
81
82 bool ApiKey::ValidateApiName(const std::string& api_name) const {
83 return api_name == api_name_;
chasej 2015/12/21 20:09:10 I think the API name comparison should be case-ins
iclelland 2016/01/05 20:26:31 Done.
84 }
85
86 bool ApiKey::ValidateDate(const base::Time& now) const {
87 base::Time expiry_time = base::Time::FromDoubleT((double)expiry_timestamp_);
88 return expiry_time > now;
89 }
90
91 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698