1 // Copyright 2016 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_UNGUESSABLE_TOKEN_H_
6 #define BASE_UNGUESSABLE_TOKEN_H_
13 #include "base/base_export.h"
14 #include "base/check.h"
15 #include "base/containers/span.h"
16 #include "base/token.h"
20 struct UnguessableTokenHash;
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). Also unlike Token, the ==
26 // and != operators are constant time. It can be used as part of a larger
27 // aggregate type, or as an ID in and of itself.
29 // An UnguessableToken is a strong *bearer token*. Bearer tokens are like HTTP
30 // cookies: if a caller has the token, the callee thereby considers the caller
31 // authorized to request the operation the callee performs.
33 // UnguessableToken can be used when the resource associated with the ID needs
34 // to be protected against manipulation by other untrusted agents in the system,
35 // and there is no other convenient way to verify the authority of the agent to
36 // do so (because the resource is part of a table shared across processes, for
37 // instance). In such a scheme, knowledge of the token value in and of itself is
38 // sufficient proof of authority to carry out an operation on the associated
41 // Use Create() for creating new UnguessableTokens.
43 // NOTE: It is illegal to send empty UnguessableTokens across processes, and
44 // sending/receiving empty tokens should be treated as a security issue. If
45 // there is a valid scenario for sending "no token" across processes, use
46 // absl::optional instead of an empty token.
48 class BASE_EXPORT UnguessableToken {
50 // Create a unique UnguessableToken. It's guaranteed to be nonempty.
51 static UnguessableToken Create();
53 // Returns a reference to a global null UnguessableToken. This should only be
54 // used for functions that need to return a reference to an UnguessableToken,
55 // and should not be used as a general-purpose substitute for invoking the
56 // default constructor.
57 static const UnguessableToken& Null();
59 // Return an UnguessableToken built from the high/low bytes provided.
60 // It should only be used in deserialization scenarios.
62 // NOTE: If the returned `absl::optional` does not have a value, it means that
63 // `high` and `low` correspond to an `UnguesssableToken` that was never
64 // initialized via Create(). This is a security issue, and should be handled.
65 static absl::optional<UnguessableToken> Deserialize(uint64_t high,
68 // Creates an empty UnguessableToken.
69 // Assign to it with Create() before using it.
70 constexpr UnguessableToken() = default;
72 constexpr UnguessableToken(const UnguessableToken&) = default;
73 constexpr UnguessableToken& operator=(const UnguessableToken&) = default;
74 constexpr UnguessableToken(UnguessableToken&&) noexcept = default;
75 constexpr UnguessableToken& operator=(UnguessableToken&&) = default;
77 // NOTE: Serializing an empty UnguessableToken is an illegal operation.
78 uint64_t GetHighForSerialization() const {
83 // NOTE: Serializing an empty UnguessableToken is an illegal operation.
84 uint64_t GetLowForSerialization() const {
89 constexpr bool is_empty() const { return token_.is_zero(); }
91 // Hex representation of the unguessable token.
92 std::string ToString() const { return token_.ToString(); }
94 explicit constexpr operator bool() const { return !is_empty(); }
96 span<const uint8_t, 16> AsBytes() const { return token_.AsBytes(); }
98 constexpr bool operator<(const UnguessableToken& other) const {
99 return token_ < other.token_;
102 bool operator==(const UnguessableToken& other) const;
104 bool operator!=(const UnguessableToken& other) const {
105 return !(*this == other);
108 #if defined(UNIT_TEST)
109 static UnguessableToken CreateForTesting(uint64_t high, uint64_t low) {
110 absl::optional<UnguessableToken> token = Deserialize(high, low);
111 DCHECK(token.has_value());
112 return token.value();
117 friend struct UnguessableTokenHash;
118 explicit UnguessableToken(const Token& token);
123 BASE_EXPORT std::ostream& operator<<(std::ostream& out,
124 const UnguessableToken& token);
126 // For use in std::unordered_map.
127 struct UnguessableTokenHash {
128 size_t operator()(const base::UnguessableToken& token) const {
130 return TokenHash()(token.token_);
136 #endif // BASE_UNGUESSABLE_TOKEN_H_