Vitaly Buka | f08caeb | 2015-12-02 13:47:48 -0800 | [diff] [blame] | 1 | // Copyright 2015 The Weave 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 | |
| 5 | #ifndef LIBWEAVE_SRC_PRIVET_AUTH_MANAGER_H_ |
| 6 | #define LIBWEAVE_SRC_PRIVET_AUTH_MANAGER_H_ |
| 7 | |
Vitaly Buka | cc77fad | 2015-12-13 21:04:46 -0800 | [diff] [blame] | 8 | #include <deque> |
Vitaly Buka | f08caeb | 2015-12-02 13:47:48 -0800 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
Vitaly Buka | a821f2e | 2016-01-27 11:44:02 -0800 | [diff] [blame] | 12 | #include <base/gtest_prod_util.h> |
Vitaly Buka | b612e3c | 2016-02-22 17:43:15 -0800 | [diff] [blame] | 13 | #include <base/memory/weak_ptr.h> |
Vitaly Buka | 41aa809 | 2015-12-09 20:04:34 -0800 | [diff] [blame] | 14 | #include <base/time/default_clock.h> |
Vitaly Buka | f08caeb | 2015-12-02 13:47:48 -0800 | [diff] [blame] | 15 | #include <base/time/time.h> |
| 16 | #include <weave/error.h> |
| 17 | |
| 18 | #include "src/privet/privet_types.h" |
| 19 | |
| 20 | namespace weave { |
Vitaly Buka | 229113e | 2015-12-13 23:12:42 -0800 | [diff] [blame] | 21 | |
Vitaly Buka | ebde3c1 | 2016-02-23 18:50:42 -0800 | [diff] [blame] | 22 | class AccessRevocationManager; |
Vitaly Buka | 229113e | 2015-12-13 23:12:42 -0800 | [diff] [blame] | 23 | class Config; |
Vitaly Buka | a10ab1c | 2015-12-14 16:28:47 -0800 | [diff] [blame] | 24 | enum class RootClientTokenOwner; |
Vitaly Buka | 229113e | 2015-12-13 23:12:42 -0800 | [diff] [blame] | 25 | |
Vitaly Buka | f08caeb | 2015-12-02 13:47:48 -0800 | [diff] [blame] | 26 | namespace privet { |
| 27 | |
| 28 | class AuthManager { |
| 29 | public: |
Vitaly Buka | 229113e | 2015-12-13 23:12:42 -0800 | [diff] [blame] | 30 | AuthManager(Config* config, |
Vitaly Buka | ebde3c1 | 2016-02-23 18:50:42 -0800 | [diff] [blame] | 31 | AccessRevocationManager* black_list, |
Vitaly Buka | 229113e | 2015-12-13 23:12:42 -0800 | [diff] [blame] | 32 | const std::vector<uint8_t>& certificate_fingerprint); |
| 33 | |
| 34 | // Constructor for tests. |
Vitaly Buka | 0bc02ed | 2015-12-18 14:13:12 -0800 | [diff] [blame] | 35 | AuthManager(const std::vector<uint8_t>& auth_secret, |
Vitaly Buka | 41aa809 | 2015-12-09 20:04:34 -0800 | [diff] [blame] | 36 | const std::vector<uint8_t>& certificate_fingerprint, |
Vitaly Buka | 0bc02ed | 2015-12-18 14:13:12 -0800 | [diff] [blame] | 37 | const std::vector<uint8_t>& access_secret, |
Vitaly Buka | b612e3c | 2016-02-22 17:43:15 -0800 | [diff] [blame] | 38 | base::Clock* clock = nullptr, |
Vitaly Buka | ebde3c1 | 2016-02-23 18:50:42 -0800 | [diff] [blame] | 39 | AccessRevocationManager* black_list = nullptr); |
Vitaly Buka | f08caeb | 2015-12-02 13:47:48 -0800 | [diff] [blame] | 40 | ~AuthManager(); |
| 41 | |
Vitaly Buka | a0a8134 | 2015-12-17 13:42:13 -0800 | [diff] [blame] | 42 | std::vector<uint8_t> CreateAccessToken(const UserInfo& user_info, |
| 43 | base::TimeDelta ttl) const; |
| 44 | |
| 45 | bool ParseAccessToken(const std::vector<uint8_t>& token, |
| 46 | UserInfo* user_info, |
| 47 | ErrorPtr* error) const; |
Vitaly Buka | f08caeb | 2015-12-02 13:47:48 -0800 | [diff] [blame] | 48 | |
Vitaly Buka | 0bc02ed | 2015-12-18 14:13:12 -0800 | [diff] [blame] | 49 | const std::vector<uint8_t>& GetAuthSecret() const { return auth_secret_; } |
| 50 | const std::vector<uint8_t>& GetAccessSecret() const { return access_secret_; } |
Vitaly Buka | f08caeb | 2015-12-02 13:47:48 -0800 | [diff] [blame] | 51 | const std::vector<uint8_t>& GetCertificateFingerprint() const { |
| 52 | return certificate_fingerprint_; |
| 53 | } |
Vitaly Buka | 41aa809 | 2015-12-09 20:04:34 -0800 | [diff] [blame] | 54 | |
| 55 | base::Time Now() const; |
Vitaly Buka | f08caeb | 2015-12-02 13:47:48 -0800 | [diff] [blame] | 56 | |
Vitaly Buka | 4ab5002 | 2015-12-14 22:32:24 -0800 | [diff] [blame] | 57 | std::vector<uint8_t> ClaimRootClientAuthToken(RootClientTokenOwner owner, |
| 58 | ErrorPtr* error); |
Vitaly Buka | 305ab61 | 2015-12-15 12:02:59 -0800 | [diff] [blame] | 59 | bool ConfirmClientAuthToken(const std::vector<uint8_t>& token, |
| 60 | ErrorPtr* error); |
Vitaly Buka | 29bc593 | 2015-12-13 22:56:52 -0800 | [diff] [blame] | 61 | |
Vitaly Buka | a821f2e | 2016-01-27 11:44:02 -0800 | [diff] [blame] | 62 | std::vector<uint8_t> GetRootClientAuthToken(RootClientTokenOwner owner) const; |
Vitaly Buka | 66a01e0 | 2015-12-20 18:37:14 -0800 | [diff] [blame] | 63 | bool IsValidAuthToken(const std::vector<uint8_t>& token, |
| 64 | ErrorPtr* error) const; |
| 65 | bool CreateAccessTokenFromAuth(const std::vector<uint8_t>& auth_token, |
| 66 | base::TimeDelta ttl, |
| 67 | std::vector<uint8_t>* access_token, |
| 68 | AuthScope* access_token_scope, |
| 69 | base::TimeDelta* access_token_ttl, |
| 70 | ErrorPtr* error) const; |
Vitaly Buka | cc77fad | 2015-12-13 21:04:46 -0800 | [diff] [blame] | 71 | |
Vitaly Buka | 0bc02ed | 2015-12-18 14:13:12 -0800 | [diff] [blame] | 72 | void SetAuthSecret(const std::vector<uint8_t>& secret, |
| 73 | RootClientTokenOwner owner); |
Vitaly Buka | 229113e | 2015-12-13 23:12:42 -0800 | [diff] [blame] | 74 | |
Vitaly Buka | d5f7aab | 2016-01-27 18:25:14 -0800 | [diff] [blame] | 75 | std::string CreateSessionId() const; |
| 76 | bool IsValidSessionId(const std::string& session_id) const; |
Vitaly Buka | 483d597 | 2015-12-16 13:45:35 -0800 | [diff] [blame] | 77 | |
Vitaly Buka | 4ab5002 | 2015-12-14 22:32:24 -0800 | [diff] [blame] | 78 | private: |
Vitaly Buka | d5f7aab | 2016-01-27 18:25:14 -0800 | [diff] [blame] | 79 | friend class AuthManagerTest; |
Vitaly Buka | a821f2e | 2016-01-27 11:44:02 -0800 | [diff] [blame] | 80 | |
Vitaly Buka | b612e3c | 2016-02-22 17:43:15 -0800 | [diff] [blame] | 81 | void ResetAccessSecret(); |
| 82 | |
Vitaly Buka | d5f7aab | 2016-01-27 18:25:14 -0800 | [diff] [blame] | 83 | // Test helpers. Device does not need to implement delegation. |
Vitaly Buka | a821f2e | 2016-01-27 11:44:02 -0800 | [diff] [blame] | 84 | std::vector<uint8_t> DelegateToUser(const std::vector<uint8_t>& token, |
Vitaly Buka | d5f7aab | 2016-01-27 18:25:14 -0800 | [diff] [blame] | 85 | base::TimeDelta ttl, |
Vitaly Buka | a821f2e | 2016-01-27 11:44:02 -0800 | [diff] [blame] | 86 | const UserInfo& user_info) const; |
| 87 | |
Vitaly Buka | ee7322f | 2015-12-18 16:54:05 -0800 | [diff] [blame] | 88 | Config* config_{nullptr}; // Can be nullptr for tests. |
Vitaly Buka | ebde3c1 | 2016-02-23 18:50:42 -0800 | [diff] [blame] | 89 | AccessRevocationManager* black_list_{nullptr}; |
Vitaly Buka | 41aa809 | 2015-12-09 20:04:34 -0800 | [diff] [blame] | 90 | base::DefaultClock default_clock_; |
Vitaly Buka | 229113e | 2015-12-13 23:12:42 -0800 | [diff] [blame] | 91 | base::Clock* clock_{&default_clock_}; |
Vitaly Buka | d5f7aab | 2016-01-27 18:25:14 -0800 | [diff] [blame] | 92 | mutable uint32_t session_counter_{0}; |
Vitaly Buka | 41aa809 | 2015-12-09 20:04:34 -0800 | [diff] [blame] | 93 | |
Vitaly Buka | 0bc02ed | 2015-12-18 14:13:12 -0800 | [diff] [blame] | 94 | std::vector<uint8_t> auth_secret_; // Persistent. |
Vitaly Buka | f08caeb | 2015-12-02 13:47:48 -0800 | [diff] [blame] | 95 | std::vector<uint8_t> certificate_fingerprint_; |
Vitaly Buka | 0bc02ed | 2015-12-18 14:13:12 -0800 | [diff] [blame] | 96 | std::vector<uint8_t> access_secret_; // New on every reboot. |
Vitaly Buka | f08caeb | 2015-12-02 13:47:48 -0800 | [diff] [blame] | 97 | |
Vitaly Buka | a10ab1c | 2015-12-14 16:28:47 -0800 | [diff] [blame] | 98 | std::deque<std::pair<std::unique_ptr<AuthManager>, RootClientTokenOwner>> |
| 99 | pending_claims_; |
Vitaly Buka | cc77fad | 2015-12-13 21:04:46 -0800 | [diff] [blame] | 100 | |
Vitaly Buka | b612e3c | 2016-02-22 17:43:15 -0800 | [diff] [blame] | 101 | base::WeakPtrFactory<AuthManager> weak_ptr_factory_{this}; |
Vitaly Buka | f08caeb | 2015-12-02 13:47:48 -0800 | [diff] [blame] | 102 | DISALLOW_COPY_AND_ASSIGN(AuthManager); |
| 103 | }; |
| 104 | |
| 105 | } // namespace privet |
| 106 | } // namespace weave |
| 107 | |
| 108 | #endif // LIBWEAVE_SRC_PRIVET_AUTH_MANAGER_H_ |