OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_SETTINGS_DEVICE_OAUTH2_TOKEN_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_OAUTH2_TOKEN_SERVICE_H_ |
6 #define CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_OAUTH2_TOKEN_SERVICE_H_ | 6 #define CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_OAUTH2_TOKEN_SERVICE_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
16 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
18 #include "chrome/browser/chromeos/settings/cros_settings.h" | 18 #include "chrome/browser/chromeos/settings/cros_settings.h" |
19 #include "google_apis/gaia/gaia_oauth_client.h" | 19 #include "google_apis/gaia/gaia_oauth_client.h" |
20 #include "google_apis/gaia/oauth2_token_service.h" | 20 #include "google_apis/gaia/oauth2_token_service.h" |
21 #include "google_apis/gaia/oauth2_token_service_delegate.h" | |
21 #include "net/url_request/url_request_context_getter.h" | 22 #include "net/url_request/url_request_context_getter.h" |
22 | 23 |
23 namespace gaia { | 24 namespace gaia { |
24 class GaiaOAuthClient; | 25 class GaiaOAuthClient; |
25 } | 26 } |
26 | 27 |
27 namespace net { | 28 namespace net { |
28 class URLRequestContextGetter; | 29 class URLRequestContextGetter; |
29 } | 30 } |
30 | 31 |
31 class PrefRegistrySimple; | 32 class PrefRegistrySimple; |
32 class PrefService; | 33 class PrefService; |
33 | 34 |
34 namespace chromeos { | 35 namespace chromeos { |
35 | 36 |
36 // DeviceOAuth2TokenService retrieves OAuth2 access tokens for a given | 37 // DeviceOAuth2TokenService retrieves OAuth2 access tokens for a given |
37 // set of scopes using the device-level OAuth2 any-api refresh token | 38 // set of scopes using the device-level OAuth2 any-api refresh token |
38 // obtained during enterprise device enrollment. | 39 // obtained during enterprise device enrollment. |
39 // | 40 // |
40 // See |OAuth2TokenService| for usage details. | 41 // See |OAuth2TokenService| for usage details. |
41 // | 42 // |
42 // When using DeviceOAuth2TokenService, a value of |GetRobotAccountId| should | 43 // When using DeviceOAuth2TokenService, a value of |GetRobotAccountId| should |
43 // be used in places where API expects |account_id|. | 44 // be used in places where API expects |account_id|. |
44 // | 45 // |
45 // Note that requests must be made from the UI thread. | 46 // Note that requests must be made from the UI thread. |
46 class DeviceOAuth2TokenService : public OAuth2TokenService, | 47 class DeviceOAuth2TokenService : public OAuth2TokenService, |
47 public gaia::GaiaOAuthClient::Delegate { | 48 public gaia::GaiaOAuthClient::Delegate { |
48 public: | 49 public: |
50 class DeviceOAuth2TokenServiceDelegate : public OAuth2TokenServiceDelegate { | |
xiyuan
2015/06/26 18:15:53
nit: We can probably move DeviceOAuth2TokenService
| |
51 public: | |
52 friend class DeviceOAuth2TokenService; | |
xiyuan
2015/06/26 18:15:53
Do we need this? Think inner class is friend of ou
| |
53 DeviceOAuth2TokenServiceDelegate( | |
54 DeviceOAuth2TokenService* device_token_service); | |
55 ~DeviceOAuth2TokenServiceDelegate() override; | |
56 | |
57 OAuth2AccessTokenFetcher* CreateAccessTokenFetcher( | |
xiyuan
2015/06/26 18:15:53
nit: // OAuth2TokenServiceDelegate
| |
58 const std::string& account_id, | |
59 net::URLRequestContextGetter* getter, | |
60 OAuth2AccessTokenConsumer* consumer) override; | |
61 | |
62 bool RefreshTokenIsAvailable(const std::string& account_id) const override; | |
63 void UpdateAuthError(const std::string& account_id, | |
64 const GoogleServiceAuthError& error) override; | |
65 | |
66 std::vector<std::string> GetAccounts() override; | |
67 void RevokeAllCredentials() override; | |
68 net::URLRequestContextGetter* GetRequestContext() const override; | |
69 | |
70 private: | |
71 DeviceOAuth2TokenService* device_token_service_; | |
xiyuan
2015/06/26 18:15:53
nit: DISALLOW_COPY_AND_ASSIGN(...);
| |
72 }; | |
73 | |
49 typedef base::Callback<void(bool)> StatusCallback; | 74 typedef base::Callback<void(bool)> StatusCallback; |
50 | 75 |
51 // Persist the given refresh token on the device. Overwrites any previous | 76 // Persist the given refresh token on the device. Overwrites any previous |
52 // value. Should only be called during initial device setup. Signals | 77 // value. Should only be called during initial device setup. Signals |
53 // completion via the given callback, passing true if the operation succeeded. | 78 // completion via the given callback, passing true if the operation succeeded. |
54 void SetAndSaveRefreshToken(const std::string& refresh_token, | 79 void SetAndSaveRefreshToken(const std::string& refresh_token, |
55 const StatusCallback& callback); | 80 const StatusCallback& callback); |
56 | 81 |
57 static void RegisterPrefs(PrefRegistrySimple* registry); | 82 static void RegisterPrefs(PrefRegistrySimple* registry); |
58 | 83 |
59 // Implementation of OAuth2TokenService. | |
60 bool RefreshTokenIsAvailable(const std::string& account_id) const override; | |
61 | |
62 // Pull the robot account ID from device policy. | 84 // Pull the robot account ID from device policy. |
63 virtual std::string GetRobotAccountId() const; | 85 virtual std::string GetRobotAccountId() const; |
64 | 86 |
65 // gaia::GaiaOAuthClient::Delegate implementation. | 87 // gaia::GaiaOAuthClient::Delegate implementation. |
66 void OnRefreshTokenResponse(const std::string& access_token, | 88 void OnRefreshTokenResponse(const std::string& access_token, |
67 int expires_in_seconds) override; | 89 int expires_in_seconds) override; |
68 void OnGetTokenInfoResponse( | 90 void OnGetTokenInfoResponse( |
69 scoped_ptr<base::DictionaryValue> token_info) override; | 91 scoped_ptr<base::DictionaryValue> token_info) override; |
70 void OnOAuthError() override; | 92 void OnOAuthError() override; |
71 void OnNetworkError(int response_code) override; | 93 void OnNetworkError(int response_code) override; |
72 | 94 |
73 protected: | 95 protected: |
74 // Implementation of OAuth2TokenService. | |
75 net::URLRequestContextGetter* GetRequestContext() override; | |
76 void FetchOAuth2Token(RequestImpl* request, | 96 void FetchOAuth2Token(RequestImpl* request, |
77 const std::string& account_id, | 97 const std::string& account_id, |
78 net::URLRequestContextGetter* getter, | 98 net::URLRequestContextGetter* getter, |
79 const std::string& client_id, | 99 const std::string& client_id, |
80 const std::string& client_secret, | 100 const std::string& client_secret, |
81 const ScopeSet& scopes) override; | 101 const ScopeSet& scopes) override; |
82 OAuth2AccessTokenFetcher* CreateAccessTokenFetcher( | |
83 const std::string& account_id, | |
84 net::URLRequestContextGetter* getter, | |
85 OAuth2AccessTokenConsumer* consumer) override; | |
86 | 102 |
87 private: | 103 private: |
88 struct PendingRequest; | 104 struct PendingRequest; |
89 friend class DeviceOAuth2TokenServiceFactory; | 105 friend class DeviceOAuth2TokenServiceFactory; |
90 friend class DeviceOAuth2TokenServiceTest; | 106 friend class DeviceOAuth2TokenServiceTest; |
91 | 107 |
92 // Describes the operational state of this object. | 108 // Describes the operational state of this object. |
93 enum State { | 109 enum State { |
94 // Pending system salt / refresh token load. | 110 // Pending system salt / refresh token load. |
95 STATE_LOADING, | 111 STATE_LOADING, |
96 // No token available. | 112 // No token available. |
97 STATE_NO_TOKEN, | 113 STATE_NO_TOKEN, |
98 // System salt loaded, validation not started yet. | 114 // System salt loaded, validation not started yet. |
99 STATE_VALIDATION_PENDING, | 115 STATE_VALIDATION_PENDING, |
100 // Refresh token validation underway. | 116 // Refresh token validation underway. |
101 STATE_VALIDATION_STARTED, | 117 STATE_VALIDATION_STARTED, |
102 // Token validation failed. | 118 // Token validation failed. |
103 STATE_TOKEN_INVALID, | 119 STATE_TOKEN_INVALID, |
104 // Refresh token is valid. | 120 // Refresh token is valid. |
105 STATE_TOKEN_VALID, | 121 STATE_TOKEN_VALID, |
106 }; | 122 }; |
107 | 123 |
124 // Concrete Implementation of GetRequestContext for | |
125 // DeviceOAuth2TokenServiceDelegate. | |
126 net::URLRequestContextGetter* GetRequestContextImpl(); | |
127 | |
128 // Concrete Implementation of RefreshTokenIsAvailableImpl for | |
129 // DeviceOAuth2TokenServiceDelegate. | |
130 bool RefreshTokenIsAvailableImpl(const std::string& account_id) const; | |
131 | |
132 // Concrete Implementation of CreateAccessTokenFetcher for | |
133 // DeviceOAuth2TokenServiceDelegate. | |
134 OAuth2AccessTokenFetcher* CreateAccessTokenFetcherImpl( | |
135 const std::string& account_id, | |
136 net::URLRequestContextGetter* getter, | |
137 OAuth2AccessTokenConsumer* consumer); | |
138 | |
108 // Invoked by CrosSettings when the robot account ID becomes available. | 139 // Invoked by CrosSettings when the robot account ID becomes available. |
109 void OnServiceAccountIdentityChanged(); | 140 void OnServiceAccountIdentityChanged(); |
110 | 141 |
111 // Use DeviceOAuth2TokenServiceFactory to get an instance of this class. | 142 // Use DeviceOAuth2TokenServiceFactory to get an instance of this class. |
112 // Ownership of |token_encryptor| will be taken. | 143 // Ownership of |token_encryptor| will be taken. |
113 explicit DeviceOAuth2TokenService(net::URLRequestContextGetter* getter, | 144 explicit DeviceOAuth2TokenService(net::URLRequestContextGetter* getter, |
114 PrefService* local_state); | 145 PrefService* local_state); |
115 ~DeviceOAuth2TokenService() override; | 146 ~DeviceOAuth2TokenService() override; |
116 | 147 |
117 // Returns the refresh token for account_id. | 148 // Returns the refresh token for account_id. |
(...skipping 20 matching lines...) Expand all Loading... | |
138 // Flushes |token_save_callbacks_|, indicating the specified result. | 169 // Flushes |token_save_callbacks_|, indicating the specified result. |
139 void FlushTokenSaveCallbacks(bool result); | 170 void FlushTokenSaveCallbacks(bool result); |
140 | 171 |
141 // Signals failure on the specified request, passing |error| as the reason. | 172 // Signals failure on the specified request, passing |error| as the reason. |
142 void FailRequest(RequestImpl* request, GoogleServiceAuthError::State error); | 173 void FailRequest(RequestImpl* request, GoogleServiceAuthError::State error); |
143 | 174 |
144 // Dependencies. | 175 // Dependencies. |
145 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 176 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
146 PrefService* local_state_; | 177 PrefService* local_state_; |
147 | 178 |
179 DeviceOAuth2TokenServiceDelegate* device_oauth2_token_service_delegate_; | |
180 | |
148 // Current operational state. | 181 // Current operational state. |
149 State state_; | 182 State state_; |
150 | 183 |
151 // Token save callbacks waiting to be completed. | 184 // Token save callbacks waiting to be completed. |
152 std::vector<StatusCallback> token_save_callbacks_; | 185 std::vector<StatusCallback> token_save_callbacks_; |
153 | 186 |
154 // Currently open requests that are waiting while loading the system salt or | 187 // Currently open requests that are waiting while loading the system salt or |
155 // validating the token. | 188 // validating the token. |
156 std::vector<PendingRequest*> pending_requests_; | 189 std::vector<PendingRequest*> pending_requests_; |
157 | 190 |
(...skipping 11 matching lines...) Expand all Loading... | |
169 service_account_identity_subscription_; | 202 service_account_identity_subscription_; |
170 | 203 |
171 base::WeakPtrFactory<DeviceOAuth2TokenService> weak_ptr_factory_; | 204 base::WeakPtrFactory<DeviceOAuth2TokenService> weak_ptr_factory_; |
172 | 205 |
173 DISALLOW_COPY_AND_ASSIGN(DeviceOAuth2TokenService); | 206 DISALLOW_COPY_AND_ASSIGN(DeviceOAuth2TokenService); |
174 }; | 207 }; |
175 | 208 |
176 } // namespace chromeos | 209 } // namespace chromeos |
177 | 210 |
178 #endif // CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_OAUTH2_TOKEN_SERVICE_H_ | 211 #endif // CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_OAUTH2_TOKEN_SERVICE_H_ |
OLD | NEW |