Chromium Code Reviews| 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..5ab842b68e0325e7bb40cac9adb2418f75e1d0fc |
| --- /dev/null |
| +++ b/content/browser/experiments/api_key.cc |
| @@ -0,0 +1,66 @@ |
| +// 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) { |
|
davidben
2015/12/18 19:48:36
key_text
iclelland
2015/12/18 21:10:22
Done.
|
| + if (keyText.empty()) { |
| + return nullptr; |
| + } |
| + |
| + // API Key should resemble: |
| + // signature|origin|api_name|expiry_timestamp |
| + 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_string = parts[1]; |
| + const std::string& api_name = parts[2]; |
| + const std::string& expiry_string = parts[3]; |
| + |
| + uint64_t expiry_timestamp; |
| + if (!base::StringToUint64(expiry_string, &expiry_timestamp)) { |
| + return nullptr; |
| + } |
| + |
| + // 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.
|
| + const std::string& data = keyText.substr(keyText.find('|') + 1); |
| + |
| + return scoped_ptr<ApiKey>(new ApiKey(signature, data, GURL(origin_string), |
| + api_name, expiry_timestamp)); |
| +} |
| + |
| +ApiKey::ApiKey(const std::string& signature, |
| + const std::string& data, |
| + const GURL& origin, |
| + const std::string& api_name, |
| + uint64_t expiry_timestamp) |
| + : signature_(signature), |
| + data_(data), |
| + origin_(origin), |
| + api_name_(api_name), |
| + expiry_timestamp_(expiry_timestamp) {} |
| + |
| +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.
|
| + base::Time expiry_time = base::Time::FromDoubleT((double)expiry_timestamp_); |
| + return expiry_time > now; |
| +} |
| + |
| +} // namespace |