Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_COMMON_ORIGIN_TRIALS_TRIAL_TOKEN_H_ | 5 #ifndef CONTENT_COMMON_ORIGIN_TRIALS_TRIAL_TOKEN_H_ |
| 6 #define CONTENT_COMMON_ORIGIN_TRIALS_TRIAL_TOKEN_H_ | 6 #define CONTENT_COMMON_ORIGIN_TRIALS_TRIAL_TOKEN_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 // the EF validates the name provided by the feature implementation against any | 25 // the EF validates the name provided by the feature implementation against any |
| 26 // provided tokens. | 26 // provided tokens. |
| 27 // | 27 // |
| 28 // More documentation on the token format can be found at | 28 // More documentation on the token format can be found at |
| 29 // https://docs.google.com/document/d/1v5fi0EUV_QHckVHVF2K4P72iNywnrJtNhNZ6i2NPt 0M | 29 // https://docs.google.com/document/d/1v5fi0EUV_QHckVHVF2K4P72iNywnrJtNhNZ6i2NPt 0M |
| 30 | 30 |
| 31 class CONTENT_EXPORT TrialToken { | 31 class CONTENT_EXPORT TrialToken { |
| 32 public: | 32 public: |
| 33 ~TrialToken(); | 33 ~TrialToken(); |
| 34 | 34 |
| 35 // Returns a token object if the string represents a well-formed token, or | 35 // Returns a token object if the string represents a signed well-formed token, |
| 36 // nullptr otherwise. (This does not mean that the token is valid, just that | 36 // or nullptr otherwise. (This does not mean that the token is currently |
| 37 // it can be parsed.) | 37 // valid, or appropriate for a given origin / feature, just that it is |
| 38 // correctly formatted and signed, and can be parsed.) | |
| 39 static scoped_ptr<TrialToken> From(const std::string& token_text, | |
|
palmer
2016/04/07 00:03:53
Nit: The |From|, |Extract|, |Parse| suite of funct
iclelland
2016/04/07 14:55:13
From is intended to be the entry point -- it could
| |
| 40 base::StringPiece public_key); | |
| 41 | |
| 42 // Returns the payload of a signed token, or nullptr if the token is not | |
| 43 // properly signed, or is not well-formed. | |
| 44 static scoped_ptr<std::string> Extract(const std::string& token_text, | |
| 45 base::StringPiece public_key); | |
| 46 | |
| 47 // Returns a token object if the string represents a well-formed JSON token | |
| 48 // payload, or nullptr otherwise. | |
| 38 static scoped_ptr<TrialToken> Parse(const std::string& token_text); | 49 static scoped_ptr<TrialToken> Parse(const std::string& token_text); |
| 39 | 50 |
| 40 // Returns true if this feature is appropriate for use by the given origin, | 51 // Returns true if this token is appropriate for use by the given origin, |
| 41 // for the given feature name. This does not check whether the signature is | 52 // for the given feature name. This does not check whether the signature is |
| 42 // valid, or whether the token itself has expired. | 53 // valid, or whether the token itself has expired. |
| 43 bool IsAppropriate(const url::Origin& origin, | 54 bool IsAppropriate(const url::Origin& origin, |
|
palmer
2016/04/07 00:03:53
Nit: Similarly, I'd declare simply:
bool IsVali
iclelland
2016/04/07 14:55:13
Done.
| |
| 44 base::StringPiece feature_name) const; | 55 base::StringPiece feature_name) const; |
| 45 | 56 |
| 46 // Returns true if this token has a valid signature, and has not expired. | 57 // Returns true if this token has not yet expired. |
| 47 bool IsValid(const base::Time& now, base::StringPiece public_key) const; | 58 bool IsValid(const base::Time& now) const; |
| 48 | 59 |
| 49 uint8_t version() { return version_; } | |
| 50 std::string signature() { return signature_; } | |
| 51 std::string data() { return data_; } | |
| 52 url::Origin origin() { return origin_; } | 60 url::Origin origin() { return origin_; } |
| 53 std::string feature_name() { return feature_name_; } | 61 std::string feature_name() { return feature_name_; } |
| 54 base::Time expiry_time() { return expiry_time_; } | 62 base::Time expiry_time() { return expiry_time_; } |
| 55 | 63 |
| 56 protected: | 64 protected: |
| 57 friend class TrialTokenTest; | 65 friend class TrialTokenTest; |
| 58 | 66 |
| 59 bool ValidateOrigin(const url::Origin& origin) const; | 67 bool ValidateOrigin(const url::Origin& origin) const; |
| 60 bool ValidateFeatureName(base::StringPiece feature_name) const; | 68 bool ValidateFeatureName(base::StringPiece feature_name) const; |
| 61 bool ValidateDate(const base::Time& now) const; | 69 bool ValidateDate(const base::Time& now) const; |
| 62 bool ValidateSignature(base::StringPiece public_key) const; | 70 bool ValidateSignature(base::StringPiece public_key) const; |
| 63 | 71 |
| 64 static bool ValidateSignature(const std::string& signature_text, | 72 static bool ValidateSignature(const std::string& signature_text, |
| 65 const std::string& data, | 73 const std::string& data, |
| 66 base::StringPiece public_key); | 74 base::StringPiece public_key); |
| 67 | 75 |
| 68 private: | 76 private: |
| 69 TrialToken(uint8_t version, | 77 TrialToken(const url::Origin& origin, |
| 70 const std::string& signature, | |
| 71 const std::string& data, | |
| 72 const url::Origin& origin, | |
| 73 const std::string& feature_name, | 78 const std::string& feature_name, |
| 74 uint64_t expiry_timestamp); | 79 uint64_t expiry_timestamp); |
| 75 | 80 |
| 76 // The version number for this token. The version identifies the structure of | |
| 77 // the token, as well as the algorithm used to generate/validate the token. | |
| 78 // The version number is only incremented when incompatible changes are made | |
| 79 // to either the structure (e.g. adding a field), or the algorithm (e.g. | |
| 80 // changing the hash or signing algorithm). | |
| 81 // NOTE: The version number is not part of the token signature and validation. | |
| 82 // Thus it is possible to modify a token to use a different version from | |
| 83 // that used to generate the signature. To mitigate cross-version | |
| 84 // attacks, the signing key(s) should be changed whenever there are | |
| 85 // changes to the token structure or algorithms. | |
| 86 uint8_t version_; | |
| 87 | |
| 88 // The base64-encoded-signature portion of the token. For the token to be | |
| 89 // valid, this must be a valid signature for the data portion of the token, as | |
| 90 // verified by the public key in trial_token.cc. | |
| 91 std::string signature_; | |
| 92 | |
| 93 // The portion of the token string which is signed, and whose signature is | |
| 94 // contained in the signature_ member. | |
| 95 std::string data_; | |
| 96 | |
| 97 // The origin for which this token is valid. Must be a secure origin. | 81 // The origin for which this token is valid. Must be a secure origin. |
| 98 url::Origin origin_; | 82 url::Origin origin_; |
| 99 | 83 |
| 100 // The name of the experimental feature which this token enables. | 84 // The name of the experimental feature which this token enables. |
| 101 std::string feature_name_; | 85 std::string feature_name_; |
| 102 | 86 |
| 103 // The time until which this token should be considered valid. | 87 // The time until which this token should be considered valid. |
| 104 base::Time expiry_time_; | 88 base::Time expiry_time_; |
| 105 }; | 89 }; |
| 106 | 90 |
| 107 } // namespace content | 91 } // namespace content |
| 108 | 92 |
| 109 #endif // CONTENT_COMMON_ORIGIN_TRIALS_TRIAL_TOKEN_H_ | 93 #endif // CONTENT_COMMON_ORIGIN_TRIALS_TRIAL_TOKEN_H_ |
| OLD | NEW |