| 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 <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/strings/string_piece.h" | 11 #include "base/strings/string_piece.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "content/common/content_export.h" | 13 #include "content/common/content_export.h" |
| 14 #include "url/origin.h" | 14 #include "url/origin.h" |
| 15 | 15 |
| 16 namespace blink { |
| 17 enum class WebOriginTrialTokenStatus; |
| 18 } |
| 19 |
| 16 namespace content { | 20 namespace content { |
| 17 | 21 |
| 18 // The Experimental Framework (EF) provides limited access to experimental | 22 // The Experimental Framework (EF) provides limited access to experimental |
| 19 // features, on a per-origin basis (origin trials). This class defines the trial | 23 // features, on a per-origin basis (origin trials). This class defines the trial |
| 20 // token data structure, used to securely provide access to an experimental | 24 // token data structure, used to securely provide access to an experimental |
| 21 // feature. | 25 // feature. |
| 22 // | 26 // |
| 23 // Features are defined by string names, provided by the implementers. The EF | 27 // Features are defined by string names, provided by the implementers. The EF |
| 24 // code does not maintain an enum or constant list for feature names. Instead, | 28 // code does not maintain an enum or constant list for feature names. Instead, |
| 25 // the EF validates the name provided by the feature implementation against any | 29 // the EF validates the name provided by the feature implementation against any |
| 26 // provided tokens. | 30 // provided tokens. |
| 27 // | 31 // |
| 28 // More documentation on the token format can be found at | 32 // More documentation on the token format can be found at |
| 29 // https://docs.google.com/document/d/1v5fi0EUV_QHckVHVF2K4P72iNywnrJtNhNZ6i2NPt
0M | 33 // https://docs.google.com/document/d/1v5fi0EUV_QHckVHVF2K4P72iNywnrJtNhNZ6i2NPt
0M |
| 30 | 34 |
| 31 class CONTENT_EXPORT TrialToken { | 35 class CONTENT_EXPORT TrialToken { |
| 32 public: | 36 public: |
| 33 ~TrialToken(); | 37 ~TrialToken(); |
| 34 | 38 |
| 35 // Returns a token object if the string represents a signed well-formed token, | 39 // If the string represents a signed well-formed token, a token object is |
| 36 // or nullptr otherwise. (This does not mean that the token is currently | 40 // returned, and success is returned in the |out_status| parameter. Otherwise, |
| 37 // valid, or appropriate for a given origin / feature, just that it is | 41 // the |out_status| parameter indicates what was wrong with the string, and |
| 42 // nullptr is returned. |
| 43 // Note that success does not mean that the token is currently valid, or |
| 44 // appropriate for a given origin / feature. It only means that it is |
| 38 // correctly formatted and signed by the supplied public key, and can be | 45 // correctly formatted and signed by the supplied public key, and can be |
| 39 // parsed.) | 46 // parsed. |
| 40 static std::unique_ptr<TrialToken> From(const std::string& token_text, | 47 static std::unique_ptr<TrialToken> From( |
| 41 base::StringPiece public_key); | 48 const std::string& token_text, |
| 49 base::StringPiece public_key, |
| 50 blink::WebOriginTrialTokenStatus* out_status); |
| 42 | 51 |
| 43 // Returns true if this token is appropriate for use by the given origin, | 52 // Returns success if this token is appropriate for use by the given origin |
| 44 // for the given feature name, and has not yet expired. | 53 // and feature name, and has not yet expired. Otherwise, the return value |
| 45 bool IsValidForFeature(const url::Origin& origin, | 54 // indicates why the token is not valid. |
| 46 base::StringPiece feature_name, | 55 blink::WebOriginTrialTokenStatus IsValidForFeature( |
| 47 const base::Time& now) const; | 56 const url::Origin& origin, |
| 57 base::StringPiece feature_name, |
| 58 const base::Time& now) const; |
| 48 | 59 |
| 49 url::Origin origin() { return origin_; } | 60 url::Origin origin() { return origin_; } |
| 50 std::string feature_name() { return feature_name_; } | 61 std::string feature_name() { return feature_name_; } |
| 51 base::Time expiry_time() { return expiry_time_; } | 62 base::Time expiry_time() { return expiry_time_; } |
| 52 | 63 |
| 53 protected: | 64 protected: |
| 54 friend class TrialTokenTest; | 65 friend class TrialTokenTest; |
| 55 | 66 |
| 56 // Returns the payload of a signed token, or nullptr if the token is not | 67 // If the string represents a properly signed and well-formed token, the token |
| 57 // properly signed, or is not well-formed. | 68 // payload is returned in the |out_token_payload| parameter and success is |
| 58 static std::unique_ptr<std::string> Extract(const std::string& token_text, | 69 // returned. Otherwise,the return code indicates what was wrong with the |
| 59 base::StringPiece public_key); | 70 // string, and |out_token_payload| is unchanged. |
| 71 static blink::WebOriginTrialTokenStatus Extract( |
| 72 const std::string& token_text, |
| 73 base::StringPiece public_key, |
| 74 std::string* out_token_payload); |
| 60 | 75 |
| 61 // Returns a token object if the string represents a well-formed JSON token | 76 // Returns a token object if the string represents a well-formed JSON token |
| 62 // payload, or nullptr otherwise. | 77 // payload, or nullptr otherwise. |
| 63 static std::unique_ptr<TrialToken> Parse(const std::string& token_json); | 78 static std::unique_ptr<TrialToken> Parse(const std::string& token_payload); |
| 64 | 79 |
| 65 bool ValidateOrigin(const url::Origin& origin) const; | 80 bool ValidateOrigin(const url::Origin& origin) const; |
| 66 bool ValidateFeatureName(base::StringPiece feature_name) const; | 81 bool ValidateFeatureName(base::StringPiece feature_name) const; |
| 67 bool ValidateDate(const base::Time& now) const; | 82 bool ValidateDate(const base::Time& now) const; |
| 68 | 83 |
| 69 static bool ValidateSignature(base::StringPiece signature_text, | 84 static bool ValidateSignature(base::StringPiece signature_text, |
| 70 const std::string& data, | 85 const std::string& data, |
| 71 base::StringPiece public_key); | 86 base::StringPiece public_key); |
| 72 | 87 |
| 73 private: | 88 private: |
| 74 TrialToken(const url::Origin& origin, | 89 TrialToken(const url::Origin& origin, |
| 75 const std::string& feature_name, | 90 const std::string& feature_name, |
| 76 uint64_t expiry_timestamp); | 91 uint64_t expiry_timestamp); |
| 77 | 92 |
| 78 // The origin for which this token is valid. Must be a secure origin. | 93 // The origin for which this token is valid. Must be a secure origin. |
| 79 url::Origin origin_; | 94 url::Origin origin_; |
| 80 | 95 |
| 81 // The name of the experimental feature which this token enables. | 96 // The name of the experimental feature which this token enables. |
| 82 std::string feature_name_; | 97 std::string feature_name_; |
| 83 | 98 |
| 84 // The time until which this token should be considered valid. | 99 // The time until which this token should be considered valid. |
| 85 base::Time expiry_time_; | 100 base::Time expiry_time_; |
| 86 }; | 101 }; |
| 87 | 102 |
| 88 } // namespace content | 103 } // namespace content |
| 89 | 104 |
| 90 #endif // CONTENT_COMMON_ORIGIN_TRIALS_TRIAL_TOKEN_H_ | 105 #endif // CONTENT_COMMON_ORIGIN_TRIALS_TRIAL_TOKEN_H_ |
| OLD | NEW |