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