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

Side by Side Diff: content/browser/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: Rebase Created 5 years 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/browser/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 ApiKey::~ApiKey() {}
19
20 scoped_ptr<ApiKey> ApiKey::Parse(const std::string& keyText) {
21 if (keyText.empty()) {
22 return nullptr;
23 }
24
25 // API Key should resemble:
26 // signature:origin:apiName:expiry
27 std::vector<std::string> parts =
28 SplitString(keyText, "|", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
29 if (parts.size() != 4) {
30 return nullptr;
31 }
32
33 const std::string& signature = parts[0];
34 const std::string& origin = parts[1];
35 const std::string& apiName = parts[2];
36 const std::string& expiry = parts[3];
37
38 uint64_t expiry_int;
39 if (!base::StringToUint64(expiry, &expiry_int)) {
40 return nullptr;
41 }
42
43 // signed data is (origin + "|" + api_name + "|" + expiry)
44 const std::string& data = keyText.substr(keyText.find('|') + 1);
45
46 return scoped_ptr<ApiKey>(
47 new ApiKey(signature, data, GURL(origin), apiName, expiry_int));
chasej 2015/12/15 19:43:41 There is no validation of the origin or api name (
iclelland 2015/12/15 21:22:24 Illegal use of the delimiter would result in more
chasej 2015/12/16 15:41:17 I agree with keeping parsing and validation separa
iclelland 2015/12/16 17:04:37 Cool -- if it's alright with you, I'll add the ext
48 }
49
50 ApiKey::ApiKey(const std::string& signature,
51 const std::string& data,
52 const GURL& origin,
53 const std::string& api_name,
54 uint64_t expiry)
55 : signature_(signature),
56 data_(data),
57 origin_(origin),
58 api_name_(api_name),
59 expiry_(expiry) {}
60
61 bool ApiKey::IsValidNow(const base::Time& now) const {
62 base::Time expiry_time =
63 base::Time::FromDoubleT((double)expiry_);
64 return expiry_time > now;
65 }
66
67 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698