| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_SIGNIN_TOKEN_HANDLE_UTIL_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_SIGNIN_TOKEN_HANDLE_UTIL_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_LOGIN_SIGNIN_TOKEN_HANDLE_UTIL_H_ | 6 #define CHROME_BROWSER_CHROMEOS_LOGIN_SIGNIN_TOKEN_HANDLE_UTIL_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/containers/scoped_ptr_hash_map.h" | 12 #include "base/containers/scoped_ptr_hash_map.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "components/signin/core/account_id/account_id.h" | 15 #include "components/user_manager/user_id.h" |
| 16 #include "google_apis/gaia/gaia_oauth_client.h" | 16 #include "google_apis/gaia/gaia_oauth_client.h" |
| 17 | 17 |
| 18 class AccountId; | |
| 19 | |
| 20 namespace base { | 18 namespace base { |
| 21 class DictionaryValue; | 19 class DictionaryValue; |
| 22 } | 20 } |
| 23 | 21 |
| 24 namespace user_manager { | 22 namespace user_manager { |
| 25 class UserManager; | 23 class UserManager; |
| 26 } | 24 } |
| 27 | 25 |
| 28 // This class is responsible for operations with External Token Handle. | 26 // This class is responsible for operations with External Token Handle. |
| 29 // Handle is an extra token associated with OAuth refresh token that have | 27 // Handle is an extra token associated with OAuth refresh token that have |
| 30 // exactly same lifetime. It is not secure, and it's only purpose is checking | 28 // exactly same lifetime. It is not secure, and it's only purpose is checking |
| 31 // validity of corresponding refresh token in the insecure environment. | 29 // validity of corresponding refresh token in the insecure environment. |
| 32 class TokenHandleUtil { | 30 class TokenHandleUtil { |
| 33 public: | 31 public: |
| 34 explicit TokenHandleUtil(user_manager::UserManager* user_manager); | 32 explicit TokenHandleUtil(user_manager::UserManager* user_manager); |
| 35 ~TokenHandleUtil(); | 33 ~TokenHandleUtil(); |
| 36 | 34 |
| 37 enum TokenHandleStatus { VALID, INVALID, UNKNOWN }; | 35 enum TokenHandleStatus { VALID, INVALID, UNKNOWN }; |
| 38 | 36 |
| 39 using TokenValidationCallback = | 37 typedef base::Callback<void(const user_manager::UserID&, TokenHandleStatus)> |
| 40 base::Callback<void(const AccountId&, TokenHandleStatus)>; | 38 TokenValidationCallback; |
| 41 | 39 |
| 42 // Returns true if UserManager has token handle associated with |account_id|. | 40 // Returns true if UserManager has token handle associated with |user_id|. |
| 43 bool HasToken(const AccountId& account_id); | 41 bool HasToken(const user_manager::UserID& user_id); |
| 44 | 42 |
| 45 // Removes token handle for |account_id| from UserManager storage. | 43 // Removes token handle for |user_id| from UserManager storage. |
| 46 void DeleteHandle(const AccountId& account_id); | 44 void DeleteHandle(const user_manager::UserID& user_id); |
| 47 | 45 |
| 48 // Marks current handle as invalid, new one should be obtained at next sign | 46 // Marks current handle as invalid, new one should be obtained at next sign |
| 49 // in. | 47 // in. |
| 50 void MarkHandleInvalid(const AccountId& account_id); | 48 void MarkHandleInvalid(const user_manager::UserID& user_id); |
| 51 | 49 |
| 52 // Indicates if token handle for |account_id| is missing or marked as invalid. | 50 // Indicates if token handle for |user_id| is missing or marked as invalid. |
| 53 bool ShouldObtainHandle(const AccountId& account_id); | 51 bool ShouldObtainHandle(const user_manager::UserID& user_id); |
| 54 | 52 |
| 55 // Performs token handle check for |account_id|. Will call |callback| with | 53 // Performs token handle check for |user_id|. Will call |callback| with |
| 56 // corresponding result. | 54 // corresponding result. |
| 57 void CheckToken(const AccountId& account_id, | 55 void CheckToken(const user_manager::UserID& user_id, |
| 58 const TokenValidationCallback& callback); | 56 const TokenValidationCallback& callback); |
| 59 | 57 |
| 60 // Given the token |handle| store it for |account_id|. | 58 // Given the token |handle| store it for |user_id|. |
| 61 void StoreTokenHandle(const AccountId& account_id, const std::string& handle); | 59 void StoreTokenHandle(const user_manager::UserID& user_id, |
| 60 const std::string& handle); |
| 62 | 61 |
| 63 private: | 62 private: |
| 64 // Associates GaiaOAuthClient::Delegate with User ID and Token. | 63 // Associates GaiaOAuthClient::Delegate with User ID and Token. |
| 65 class TokenDelegate : public gaia::GaiaOAuthClient::Delegate { | 64 class TokenDelegate : public gaia::GaiaOAuthClient::Delegate { |
| 66 public: | 65 public: |
| 67 TokenDelegate(const base::WeakPtr<TokenHandleUtil>& owner, | 66 TokenDelegate(const base::WeakPtr<TokenHandleUtil>& owner, |
| 68 const AccountId& account_id, | 67 const user_manager::UserID& user_id, |
| 69 const std::string& token, | 68 const std::string& token, |
| 70 const TokenValidationCallback& callback); | 69 const TokenValidationCallback& callback); |
| 71 ~TokenDelegate() override; | 70 ~TokenDelegate() override; |
| 72 void OnOAuthError() override; | 71 void OnOAuthError() override; |
| 73 void OnNetworkError(int response_code) override; | 72 void OnNetworkError(int response_code) override; |
| 74 void OnGetTokenInfoResponse( | 73 void OnGetTokenInfoResponse( |
| 75 scoped_ptr<base::DictionaryValue> token_info) override; | 74 scoped_ptr<base::DictionaryValue> token_info) override; |
| 76 void NotifyDone(); | 75 void NotifyDone(); |
| 77 | 76 |
| 78 private: | 77 private: |
| 79 base::WeakPtr<TokenHandleUtil> owner_; | 78 base::WeakPtr<TokenHandleUtil> owner_; |
| 80 AccountId account_id_; | 79 user_manager::UserID user_id_; |
| 81 std::string token_; | 80 std::string token_; |
| 82 base::TimeTicks tokeninfo_response_start_time_; | 81 base::TimeTicks tokeninfo_response_start_time_; |
| 83 TokenValidationCallback callback_; | 82 TokenValidationCallback callback_; |
| 84 | 83 |
| 85 DISALLOW_COPY_AND_ASSIGN(TokenDelegate); | 84 DISALLOW_COPY_AND_ASSIGN(TokenDelegate); |
| 86 }; | 85 }; |
| 87 | 86 |
| 88 void OnValidationComplete(const std::string& token); | 87 void OnValidationComplete(const std::string& token); |
| 89 void OnObtainTokenComplete(const AccountId& account_id); | 88 void OnObtainTokenComplete(const user_manager::UserID& id); |
| 90 | 89 |
| 91 // UserManager that stores corresponding user data. | 90 // UserManager that stores corresponding user data. |
| 92 user_manager::UserManager* user_manager_; | 91 user_manager::UserManager* user_manager_; |
| 93 | 92 |
| 94 // Map of pending check operations. | 93 // Map of pending check operations. |
| 95 base::ScopedPtrHashMap<std::string, scoped_ptr<TokenDelegate>> | 94 base::ScopedPtrHashMap<std::string, scoped_ptr<TokenDelegate>> |
| 96 validation_delegates_; | 95 validation_delegates_; |
| 97 | 96 |
| 98 // Map of pending obtain operations. | 97 // Map of pending obtain operations. |
| 99 base::ScopedPtrHashMap<AccountId, scoped_ptr<TokenDelegate>> | 98 base::ScopedPtrHashMap<user_manager::UserID, scoped_ptr<TokenDelegate>> |
| 100 obtain_delegates_; | 99 obtain_delegates_; |
| 101 | 100 |
| 102 // Instance of GAIA Client. | 101 // Instance of GAIA Client. |
| 103 scoped_ptr<gaia::GaiaOAuthClient> gaia_client_; | 102 scoped_ptr<gaia::GaiaOAuthClient> gaia_client_; |
| 104 | 103 |
| 105 base::WeakPtrFactory<TokenHandleUtil> weak_factory_; | 104 base::WeakPtrFactory<TokenHandleUtil> weak_factory_; |
| 106 | 105 |
| 107 DISALLOW_COPY_AND_ASSIGN(TokenHandleUtil); | 106 DISALLOW_COPY_AND_ASSIGN(TokenHandleUtil); |
| 108 }; | 107 }; |
| 109 | 108 |
| 110 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_SIGNIN_TOKEN_HANDLE_UTIL_H_ | 109 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_SIGNIN_TOKEN_HANDLE_UTIL_H_ |
| OLD | NEW |