| OLD | NEW |
| (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 #ifndef CONTENT_COMMON_ORIGIN_TRIALS_TRIAL_TOKEN_H_ |
| 6 #define CONTENT_COMMON_ORIGIN_TRIALS_TRIAL_TOKEN_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/strings/string_piece.h" |
| 12 #include "base/time/time.h" |
| 13 #include "content/common/content_export.h" |
| 14 #include "url/gurl.h" |
| 15 |
| 16 namespace content { |
| 17 |
| 18 // The Experimental Framework (EF) provides limited access to experimental |
| 19 // 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 |
| 21 // feature. |
| 22 // |
| 23 // 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, |
| 25 // the EF validates the name provided by the feature implementation against any |
| 26 // provided tokens. |
| 27 // |
| 28 // More documentation on the token format can be found at |
| 29 // https://docs.google.com/document/d/1v5fi0EUV_QHckVHVF2K4P72iNywnrJtNhNZ6i2NPt
0M |
| 30 |
| 31 class CONTENT_EXPORT TrialToken { |
| 32 public: |
| 33 ~TrialToken(); |
| 34 |
| 35 // Returns a token object if the string represents a well-formed token, or |
| 36 // nullptr otherwise. (This does not mean that the token is valid, just that |
| 37 // it can be parsed.) |
| 38 static scoped_ptr<TrialToken> Parse(const std::string& token_text); |
| 39 |
| 40 // Returns true if this feature is appropriate for use by the given origin, |
| 41 // for the given feature name. This does not check whether the signature is |
| 42 // valid, or whether the token itself has expired. |
| 43 bool IsAppropriate(const std::string& origin, |
| 44 const std::string& featureName) const; |
| 45 |
| 46 // Returns true if this token has a valid signature, and has not expired. |
| 47 bool IsValid(const base::Time& now) const; |
| 48 |
| 49 std::string signature() { return signature_; } |
| 50 std::string data() { return data_; } |
| 51 GURL origin() { return origin_; } |
| 52 std::string feature_name() { return feature_name_; } |
| 53 uint64_t expiry_timestamp() { return expiry_timestamp_; } |
| 54 |
| 55 protected: |
| 56 friend class TrialTokenTest; |
| 57 |
| 58 bool ValidateOrigin(const std::string& origin) const; |
| 59 bool ValidateFeatureName(const std::string& feature_name) const; |
| 60 bool ValidateDate(const base::Time& now) const; |
| 61 bool ValidateSignature(const base::StringPiece& public_key) const; |
| 62 |
| 63 static bool ValidateSignature(const std::string& signature_text, |
| 64 const std::string& data, |
| 65 const base::StringPiece& public_key); |
| 66 |
| 67 private: |
| 68 TrialToken(const std::string& signature, |
| 69 const std::string& data, |
| 70 const GURL& origin, |
| 71 const std::string& feature_name, |
| 72 uint64_t expiry_timestamp); |
| 73 |
| 74 // The base64-encoded-signature portion of the token. For the token to be |
| 75 // valid, this must be a valid signature for the data portion of the token, as |
| 76 // verified by the public key in trial_token.cc. |
| 77 std::string signature_; |
| 78 |
| 79 // The portion of the token string which is signed, and whose signature is |
| 80 // contained in the signature_ member. |
| 81 std::string data_; |
| 82 |
| 83 // The origin for which this token is valid. Must be a secure origin. |
| 84 GURL origin_; |
| 85 |
| 86 // The name of the experimental feature which this token enables. |
| 87 std::string feature_name_; |
| 88 |
| 89 // The time until which this token should be considered valid, in UTC, as |
| 90 // seconds since the Unix epoch. |
| 91 uint64_t expiry_timestamp_; |
| 92 }; |
| 93 |
| 94 } // namespace content |
| 95 |
| 96 #endif // CONTENT_COMMON_ORIGIN_TRIALS_TRIAL_TOKEN_H_ |
| OLD | NEW |