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

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: Remove forward declaration to wrong namespace 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) {
davidben 2015/12/18 19:48:36 key_text
iclelland 2015/12/18 21:10:22 Done.
21 if (keyText.empty()) {
22 return nullptr;
23 }
24
25 // API Key should resemble:
26 // signature|origin|api_name|expiry_timestamp
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_string = parts[1];
35 const std::string& api_name = parts[2];
36 const std::string& expiry_string = parts[3];
37
38 uint64_t expiry_timestamp;
39 if (!base::StringToUint64(expiry_string, &expiry_timestamp)) {
40 return nullptr;
41 }
42
43 // signed data is (origin + "|" + api_name + "|" + expiry)
davidben 2015/12/18 19:48:36 Nit: period at the end
iclelland 2015/12/18 21:10:22 Done.
44 const std::string& data = keyText.substr(keyText.find('|') + 1);
45
46 return scoped_ptr<ApiKey>(new ApiKey(signature, data, GURL(origin_string),
47 api_name, expiry_timestamp));
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_timestamp)
55 : signature_(signature),
56 data_(data),
57 origin_(origin),
58 api_name_(api_name),
59 expiry_timestamp_(expiry_timestamp) {}
60
61 bool ApiKey::IsValidNow(const base::Time& now) const {
davidben 2015/12/18 19:48:36 Nit: I realize you're going to be adding that imme
iclelland 2015/12/18 21:10:22 Done.
62 base::Time expiry_time = base::Time::FromDoubleT((double)expiry_timestamp_);
63 return expiry_time > now;
64 }
65
66 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698