unguessable_token.h 4.4 KB

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