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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: content/browser/experiments/api_key.cc
diff --git a/content/browser/experiments/api_key.cc b/content/browser/experiments/api_key.cc
new file mode 100644
index 0000000000000000000000000000000000000000..ce69791059e4b861e050991731fab7fdb2a31b15
--- /dev/null
+++ b/content/browser/experiments/api_key.cc
@@ -0,0 +1,67 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/browser/experiments/api_key.h"
+
+#include <vector>
+
+#include "base/base64.h"
+#include "base/strings/string_number_conversions.h"
+#include "base/strings/string_split.h"
+#include "base/strings/string_util.h"
+#include "base/strings/utf_string_conversions.h"
+#include "base/time/time.h"
+
+namespace content {
+
+ApiKey::~ApiKey() {}
+
+scoped_ptr<ApiKey> ApiKey::Parse(const std::string& keyText) {
+ if (keyText.empty()) {
+ return nullptr;
+ }
+
+ // API Key should resemble:
+ // signature:origin:apiName:expiry
+ std::vector<std::string> parts =
+ SplitString(keyText, "|", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
+ if (parts.size() != 4) {
+ return nullptr;
+ }
+
+ const std::string& signature = parts[0];
+ const std::string& origin = parts[1];
+ const std::string& apiName = parts[2];
+ const std::string& expiry = parts[3];
+
+ uint64_t expiry_int;
+ if (!base::StringToUint64(expiry, &expiry_int)) {
+ return nullptr;
+ }
+
+ // signed data is (origin + "|" + api_name + "|" + expiry)
+ const std::string& data = keyText.substr(keyText.find('|') + 1);
+
+ return scoped_ptr<ApiKey>(
+ 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
+}
+
+ApiKey::ApiKey(const std::string& signature,
+ const std::string& data,
+ const GURL& origin,
+ const std::string& api_name,
+ uint64_t expiry)
+ : signature_(signature),
+ data_(data),
+ origin_(origin),
+ api_name_(api_name),
+ expiry_(expiry) {}
+
+bool ApiKey::IsValidNow(const base::Time& now) const {
+ base::Time expiry_time =
+ base::Time::FromDoubleT((double)expiry_);
+ return expiry_time > now;
+}
+
+} // namespace

Powered by Google App Engine
This is Rietveld 408576698