1 // Copyright 2016 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.
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/hash/hash.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). It can be used as part of a
26 // larger aggregate type, or as an ID in and of itself.
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.
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
40 // Use Create() for creating new UnguessableTokens.
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.
47 class BASE_EXPORT UnguessableToken {
49 // Create a unique UnguessableToken.
50 static UnguessableToken Create();
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();
58 // Return a UnguessableToken built from the high/low bytes provided.
59 // It should only be used in deserialization scenarios.
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);
65 // Creates an empty UnguessableToken.
66 // Assign to it with Create() before using it.
67 constexpr UnguessableToken() = default;
69 // NOTE: Serializing an empty UnguessableToken is an illegal operation.
70 uint64_t GetHighForSerialization() const {
75 // NOTE: Serializing an empty UnguessableToken is an illegal operation.
76 uint64_t GetLowForSerialization() const {
81 bool is_empty() const { return token_.is_zero(); }
83 // Hex representation of the unguessable token.
84 std::string ToString() const { return token_.ToString(); }
86 explicit operator bool() const { return !is_empty(); }
88 bool operator<(const UnguessableToken& other) const {
89 return token_ < other.token_;
92 bool operator==(const UnguessableToken& other) const {
93 return token_ == other.token_;
96 bool operator!=(const UnguessableToken& other) const {
97 return !(*this == other);
101 friend struct UnguessableTokenHash;
102 explicit UnguessableToken(const Token& token);
107 BASE_EXPORT std::ostream& operator<<(std::ostream& out,
108 const UnguessableToken& token);
110 // For use in std::unordered_map.
111 struct UnguessableTokenHash {
112 size_t operator()(const base::UnguessableToken& token) const {
114 return TokenHash()(token.token_);
120 #endif // BASE_UNGUESSABLE_TOKEN_H_