| 6 | #define BASE_UNGUESSABLE_TOKEN_H_ |
| 7 |
| 8 | #include <stdint.h> |
| 9 | #include <string.h> |
| 10 | #include <iosfwd> |
| 11 | #include <tuple> |
| 12 |
| 13 | #include "base/base_export.h" |
| 14 | #include "base/hash/hash.h" |
| 15 | #include "base/logging.h" |
| 16 | #include "base/token.h" |
| 17 |
| 18 | namespace base { |
| 19 |
| 20 | struct UnguessableTokenHash; |
| 21 |
| 22 | // UnguessableToken is, like Token, a randomly chosen 128-bit value. Unlike |
| 23 | // Token, a new UnguessableToken is always generated at runtime from a |
| 24 | // cryptographically strong random source (or copied or serialized and |
| 25 | // deserialized from another such UnguessableToken). It can be used as part of a |
| 26 | // larger aggregate type, or as an ID in and of itself. |
| 27 | // |
| 28 | // An UnguessableToken is a strong *bearer token*. Bearer tokens are like HTTP |
| 29 | // cookies: if a caller has the token, the callee thereby considers the caller |
| 30 | // authorized to request the operation the callee performs. |
| 31 | // |
| 32 | // UnguessableToken can be used when the resource associated with the ID needs |
| 33 | // to be protected against manipulation by other untrusted agents in the system, |
| 34 | // and there is no other convenient way to verify the authority of the agent to |
| 35 | // do so (because the resource is part of a table shared across processes, for |
| 36 | // instance). In such a scheme, knowledge of the token value in and of itself is |
| 37 | // sufficient proof of authority to carry out an operation on the associated |
| 38 | // resource. |
| 39 | // |
| 40 | // Use Create() for creating new UnguessableTokens. |
| 41 | // |
| 42 | // NOTE: It is illegal to send empty UnguessableTokens across processes, and |
| 43 | // sending/receiving empty tokens should be treated as a security issue. If |
| 44 | // there is a valid scenario for sending "no token" across processes, use |
| 45 | // base::Optional instead of an empty token. |
| 46 |
| 47 | class BASE_EXPORT UnguessableToken { |
| 48 | public: |
| 49 | // Create a unique UnguessableToken. |
| 50 | static UnguessableToken Create(); |
| 51 |
| 52 | // Returns a reference to a global null UnguessableToken. This should only be |
| 53 | // used for functions that need to return a reference to an UnguessableToken, |
| 54 | // and should not be used as a general-purpose substitute for invoking the |
| 55 | // default constructor. |
| 56 | static const UnguessableToken& Null(); |
| 57 |
| 58 | // Return a UnguessableToken built from the high/low bytes provided. |
| 59 | // It should only be used in deserialization scenarios. |
| 60 | // |
| 61 | // NOTE: If the deserialized token is empty, it means that it was never |
| 62 | // initialized via Create(). This is a security issue, and should be handled. |
| 63 | static UnguessableToken Deserialize(uint64_t high, uint64_t low); |
| 64 |
| 65 | // Creates an empty UnguessableToken. |
| 66 | // Assign to it with Create() before using it. |
| 67 | constexpr UnguessableToken() = default; |
| 68 |
| 69 | // NOTE: Serializing an empty UnguessableToken is an illegal operation. |
| 70 | uint64_t GetHighForSerialization() const { |
| 71 | DCHECK(!is_empty()); |
| 72 | return token_.high(); |
| 73 | } |
| 74 |
| 75 | // NOTE: Serializing an empty UnguessableToken is an illegal operation. |
| 76 | uint64_t GetLowForSerialization() const { |
| 77 | DCHECK(!is_empty()); |
| 78 | return token_.low(); |
| 79 | } |
| 80 |
| 81 | bool is_empty() const { return token_.is_zero(); } |
| 82 |
| 83 | // Hex representation of the unguessable token. |
| 84 | std::string ToString() const { return token_.ToString(); } |
| 85 |
| 86 | explicit operator bool() const { return !is_empty(); } |
| 87 |
| 88 | bool operator<(const UnguessableToken& other) const { |
| 89 | return token_ < other.token_; |
| 90 | } |
| 91 |
| 92 | bool operator==(const UnguessableToken& other) const { |
| 93 | return token_ == other.token_; |
| 94 | } |
| 95 |
| 96 | bool operator!=(const UnguessableToken& other) const { |
| 97 | return !(*this == other); |
| 98 | } |
| 99 |
| 100 | private: |
| 101 | friend struct UnguessableTokenHash; |
| 102 | explicit UnguessableToken(const Token& token); |
| 103 |
| 104 | base::Token token_; |
| 105 | }; |
| 106 |
| 107 | BASE_EXPORT std::ostream& operator<<(std::ostream& out, |
| 108 | const UnguessableToken& token); |
| 109 |
| 110 | // For use in std::unordered_map. |
| 111 | struct UnguessableTokenHash { |
| 112 | size_t operator()(const base::UnguessableToken& token) const { |
| 113 | DCHECK(token); |
| 114 | return TokenHash()(token.token_); |
| 115 | } |
| 116 | }; |
| 117 |
| 118 | } // namespace base |
| 119 |
| 120 | #endif // BASE_UNGUESSABLE_TOKEN_H_ |
| 121 |