unguessable_token.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/hash/hash.h"
  12. #include "base/logging.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. // NOTE: Serializing an empty UnguessableToken is an illegal operation.
  59. uint64_t GetHighForSerialization() const {
  60. DCHECK(!is_empty());
  61. return token_.high();
  62. }
  63. // NOTE: Serializing an empty UnguessableToken is an illegal operation.
  64. uint64_t GetLowForSerialization() const {
  65. DCHECK(!is_empty());
  66. return token_.low();
  67. }
  68. bool is_empty() const { return token_.is_zero(); }
  69. // Hex representation of the unguessable token.
  70. std::string ToString() const { return token_.ToString(); }
  71. explicit operator bool() const { return !is_empty(); }
  72. bool operator<(const UnguessableToken& other) const {
  73. return token_ < other.token_;
  74. }
  75. bool operator==(const UnguessableToken& other) const {
  76. return token_ == other.token_;
  77. }
  78. bool operator!=(const UnguessableToken& other) const {
  79. return !(*this == other);
  80. }
  81. private:
  82. friend struct UnguessableTokenHash;
  83. explicit UnguessableToken(const Token& token);
  84. base::Token token_;
  85. };
  86. BASE_EXPORT std::ostream& operator<<(std::ostream& out,
  87. const UnguessableToken& token);
  88. // For use in std::unordered_map.
  89. struct UnguessableTokenHash {
  90. size_t operator()(const base::UnguessableToken& token) const {
  91. DCHECK(token);
  92. return TokenHash()(token.token_);
  93. }
  94. };
  95. } // namespace base
  96. #endif // BASE_UNGUESSABLE_TOKEN_H_