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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_SIGNIN_TOKEN_HANDLE_UTIL_H_
6 #define CHROME_BROWSER_CHROMEOS_LOGIN_SIGNIN_TOKEN_HANDLE_UTIL_H_
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/containers/scoped_ptr_hash_map.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/time/time.h"
15 #include "components/user_manager/user_id.h"
16 #include "google_apis/gaia/gaia_oauth_client.h"
19 class DictionaryValue
;
22 namespace user_manager
{
26 // This class is responsible for operations with External Token Handle.
27 // Handle is an extra token associated with OAuth refresh token that have
28 // exactly same lifetime. It is not secure, and it's only purpose is checking
29 // validity of corresponding refresh token in the insecure environment.
30 class TokenHandleUtil
{
32 explicit TokenHandleUtil(user_manager::UserManager
* user_manager
);
35 enum TokenHandleStatus
{ VALID
, INVALID
, UNKNOWN
};
37 typedef base::Callback
<void(const user_manager::UserID
&, TokenHandleStatus
)>
38 TokenValidationCallback
;
40 // Returns true if UserManager has token handle associated with |user_id|.
41 bool HasToken(const user_manager::UserID
& user_id
);
43 // Removes token handle for |user_id| from UserManager storage.
44 void DeleteHandle(const user_manager::UserID
& user_id
);
46 // Marks current handle as invalid, new one should be obtained at next sign
48 void MarkHandleInvalid(const user_manager::UserID
& user_id
);
50 // Indicates if token handle for |user_id| is missing or marked as invalid.
51 bool ShouldObtainHandle(const user_manager::UserID
& user_id
);
53 // Performs token handle check for |user_id|. Will call |callback| with
54 // corresponding result.
55 void CheckToken(const user_manager::UserID
& user_id
,
56 const TokenValidationCallback
& callback
);
58 // Given the token |handle| store it for |user_id|.
59 void StoreTokenHandle(const user_manager::UserID
& user_id
,
60 const std::string
& handle
);
63 // Associates GaiaOAuthClient::Delegate with User ID and Token.
64 class TokenDelegate
: public gaia::GaiaOAuthClient::Delegate
{
66 TokenDelegate(const base::WeakPtr
<TokenHandleUtil
>& owner
,
67 const user_manager::UserID
& user_id
,
68 const std::string
& token
,
69 const TokenValidationCallback
& callback
);
70 ~TokenDelegate() override
;
71 void OnOAuthError() override
;
72 void OnNetworkError(int response_code
) override
;
73 void OnGetTokenInfoResponse(
74 scoped_ptr
<base::DictionaryValue
> token_info
) override
;
78 base::WeakPtr
<TokenHandleUtil
> owner_
;
79 user_manager::UserID user_id_
;
81 base::TimeTicks tokeninfo_response_start_time_
;
82 TokenValidationCallback callback_
;
84 DISALLOW_COPY_AND_ASSIGN(TokenDelegate
);
87 void OnValidationComplete(const std::string
& token
);
88 void OnObtainTokenComplete(const user_manager::UserID
& id
);
90 // UserManager that stores corresponding user data.
91 user_manager::UserManager
* user_manager_
;
93 // Map of pending check operations.
94 base::ScopedPtrHashMap
<std::string
, scoped_ptr
<TokenDelegate
>>
95 validation_delegates_
;
97 // Map of pending obtain operations.
98 base::ScopedPtrHashMap
<user_manager::UserID
, scoped_ptr
<TokenDelegate
>>
101 // Instance of GAIA Client.
102 scoped_ptr
<gaia::GaiaOAuthClient
> gaia_client_
;
104 base::WeakPtrFactory
<TokenHandleUtil
> weak_factory_
;
106 DISALLOW_COPY_AND_ASSIGN(TokenHandleUtil
);
109 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_SIGNIN_TOKEN_HANDLE_UTIL_H_