Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1347)

Side by Side Diff: content/renderer/origin_trials/trial_token.h

Issue 1752463002: Cleanup TrialToken in preparation of moving it to content/common. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_RENDERER_ORIGIN_TRIALS_TRIAL_TOKEN_H_ 5 #ifndef CONTENT_RENDERER_ORIGIN_TRIALS_TRIAL_TOKEN_H_
6 #define CONTENT_RENDERER_ORIGIN_TRIALS_TRIAL_TOKEN_H_ 6 #define CONTENT_RENDERER_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"
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/gurl.h" 14 #include "url/gurl.h"
iclelland 2016/03/02 22:36:29 Is this header needed anymore?
Marijn Kruisselbrink 2016/03/02 22:43:12 No, removed.
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 well-formed token, or 36 // 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 // nullptr otherwise. (This does not mean that the token is valid, just that
37 // it can be parsed.) 38 // it can be parsed.)
38 static scoped_ptr<TrialToken> Parse(const std::string& token_text); 39 static scoped_ptr<TrialToken> Parse(const std::string& token_text);
39 40
41 // Returns true if this token has a valid signature, has not expired and is
42 // appropriate for use by the given origin for the given feature name.
43 bool Verify(const url::Origin& origin, base::StringPiece feature_name);
44
40 // Returns true if this feature is appropriate for use by the given origin, 45 // 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 46 // for the given feature name. This does not check whether the signature is
42 // valid, or whether the token itself has expired. 47 // valid, or whether the token itself has expired.
43 bool IsAppropriate(const std::string& origin, 48 bool IsAppropriate(const url::Origin& origin,
44 const std::string& featureName) const; 49 base::StringPiece feature_name) const;
45 50
46 // Returns true if this token has a valid signature, and has not expired. 51 // Returns true if this token has a valid signature, and has not expired.
47 bool IsValid(const base::Time& now, 52 bool IsValid(const base::Time& now, base::StringPiece public_key) const;
48 const base::StringPiece& public_key) const;
49 53
50 uint8_t version() { return version_; } 54 uint8_t version() { return version_; }
51 std::string signature() { return signature_; } 55 std::string signature() { return signature_; }
52 std::string data() { return data_; } 56 std::string data() { return data_; }
53 GURL origin() { return origin_; } 57 url::Origin origin() { return origin_; }
54 std::string feature_name() { return feature_name_; } 58 std::string feature_name() { return feature_name_; }
55 uint64_t expiry_timestamp() { return expiry_timestamp_; } 59 base::Time expiry_time() { return expiry_time_; }
56 60
57 protected: 61 protected:
58 friend class TrialTokenTest; 62 friend class TrialTokenTest;
59 63
60 bool ValidateOrigin(const std::string& origin) const; 64 bool ValidateOrigin(const url::Origin& origin) const;
61 bool ValidateFeatureName(const std::string& feature_name) const; 65 bool ValidateFeatureName(base::StringPiece feature_name) const;
62 bool ValidateDate(const base::Time& now) const; 66 bool ValidateDate(const base::Time& now) const;
63 bool ValidateSignature(const base::StringPiece& public_key) const; 67 bool ValidateSignature(base::StringPiece public_key) const;
64 68
65 static bool ValidateSignature(const std::string& signature_text, 69 static bool ValidateSignature(const std::string& signature_text,
66 const std::string& data, 70 const std::string& data,
67 const base::StringPiece& public_key); 71 base::StringPiece public_key);
68 72
69 private: 73 private:
70 TrialToken(uint8_t version, 74 TrialToken(uint8_t version,
71 const std::string& signature, 75 const std::string& signature,
72 const std::string& data, 76 const std::string& data,
73 const GURL& origin, 77 const url::Origin& origin,
74 const std::string& feature_name, 78 const std::string& feature_name,
75 uint64_t expiry_timestamp); 79 uint64_t expiry_timestamp);
76 80
77 // The version number for this token. The version identifies the structure of 81 // The version number for this token. The version identifies the structure of
78 // the token, as well as the algorithm used to generate/validate the token. 82 // the token, as well as the algorithm used to generate/validate the token.
79 // The version number is only incremented when incompatible changes are made 83 // The version number is only incremented when incompatible changes are made
80 // to either the structure (e.g. adding a field), or the algorithm (e.g. 84 // to either the structure (e.g. adding a field), or the algorithm (e.g.
81 // changing the hash or signing algorithm). 85 // changing the hash or signing algorithm).
82 // NOTE: The version number is not part of the token signature and validation. 86 // NOTE: The version number is not part of the token signature and validation.
83 // Thus it is possible to modify a token to use a different version from 87 // Thus it is possible to modify a token to use a different version from
84 // that used to generate the signature. To mitigate cross-version 88 // that used to generate the signature. To mitigate cross-version
85 // attacks, the signing key(s) should be changed whenever there are 89 // attacks, the signing key(s) should be changed whenever there are
86 // changes to the token structure or algorithms. 90 // changes to the token structure or algorithms.
87 uint8_t version_; 91 uint8_t version_;
88 92
89 // The base64-encoded-signature portion of the token. For the token to be 93 // The base64-encoded-signature portion of the token. For the token to be
90 // valid, this must be a valid signature for the data portion of the token, as 94 // valid, this must be a valid signature for the data portion of the token, as
91 // verified by the public key in trial_token.cc. 95 // verified by the public key in trial_token.cc.
92 std::string signature_; 96 std::string signature_;
93 97
94 // The portion of the token string which is signed, and whose signature is 98 // The portion of the token string which is signed, and whose signature is
95 // contained in the signature_ member. 99 // contained in the signature_ member.
96 std::string data_; 100 std::string data_;
97 101
98 // The origin for which this token is valid. Must be a secure origin. 102 // The origin for which this token is valid. Must be a secure origin.
99 GURL origin_; 103 url::Origin origin_;
100 104
101 // The name of the experimental feature which this token enables. 105 // The name of the experimental feature which this token enables.
102 std::string feature_name_; 106 std::string feature_name_;
103 107
104 // The time until which this token should be considered valid, in UTC, as 108 // The time until which this token should be considered valid.
105 // seconds since the Unix epoch. 109 base::Time expiry_time_;
106 uint64_t expiry_timestamp_;
107 }; 110 };
108 111
109 } // namespace content 112 } // namespace content
110 113
111 #endif // CONTENT_RENDERER_ORIGIN_TRIALS_TRIAL_TOKEN_H_ 114 #endif // CONTENT_RENDERER_ORIGIN_TRIALS_TRIAL_TOKEN_H_
OLDNEW
« no previous file with comments | « no previous file | content/renderer/origin_trials/trial_token.cc » ('j') | content/renderer/origin_trials/trial_token.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698