OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_COMMON_NET_GAIA_GAIA_AUTH_FETCHER_H_ | 5 #ifndef CHROME_COMMON_NET_GAIA_GAIA_AUTH_FETCHER_H_ |
6 #define CHROME_COMMON_NET_GAIA_GAIA_AUTH_FETCHER_H_ | 6 #define CHROME_COMMON_NET_GAIA_GAIA_AUTH_FETCHER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <vector> | |
11 | 10 |
12 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
13 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
14 #include "chrome/common/net/gaia/gaia_auth_consumer.h" | 13 #include "chrome/common/net/gaia/gaia_auth_consumer.h" |
15 #include "content/public/common/url_fetcher_delegate.h" | 14 #include "content/public/common/url_fetcher_delegate.h" |
16 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
17 | 16 |
18 // Authenticate a user against the Google Accounts ClientLogin API | 17 // Authenticate a user against the Google Accounts ClientLogin API |
19 // with various capabilities and return results to a GaiaAuthConsumer. | 18 // with various capabilities and return results to a GaiaAuthConsumer. |
20 // | 19 // |
21 // In the future, we will also issue auth tokens from this class. | 20 // In the future, we will also issue auth tokens from this class. |
22 // This class should be used on a single thread, but it can be whichever thread | 21 // This class should be used on a single thread, but it can be whichever thread |
23 // that you like. | 22 // that you like. |
24 // | 23 // |
25 // This class can handle one request at a time. To parallelize requests, | 24 // This class can handle one request at a time. To parallelize requests, |
26 // create multiple GaiaAuthFetcher's. | 25 // create multiple GaiaAuthFetcher's. |
27 | 26 |
28 class GaiaAuthFetcherTest; | 27 class GaiaAuthFetcherTest; |
29 | 28 |
30 namespace net { | 29 namespace net { |
31 class URLRequestContextGetter; | 30 class URLRequestContextGetter; |
32 class URLRequestStatus; | 31 class URLRequestStatus; |
33 typedef std::vector<std::string> ResponseCookies; | |
34 } | 32 } |
35 | 33 |
36 class GaiaAuthFetcher : public content::URLFetcherDelegate { | 34 class GaiaAuthFetcher : public content::URLFetcherDelegate { |
37 public: | 35 public: |
38 enum HostedAccountsSetting { | 36 enum HostedAccountsSetting { |
39 HostedAccountsAllowed, | 37 HostedAccountsAllowed, |
40 HostedAccountsNotAllowed | 38 HostedAccountsNotAllowed |
41 }; | 39 }; |
42 | 40 |
43 // Magic string indicating that, while a second factor is still | 41 // Magic string indicating that, while a second factor is still |
(...skipping 17 matching lines...) Expand all Loading... |
61 const std::string& login_captcha, | 59 const std::string& login_captcha, |
62 HostedAccountsSetting allow_hosted_accounts); | 60 HostedAccountsSetting allow_hosted_accounts); |
63 | 61 |
64 // GaiaAuthConsumer will be called on the original thread | 62 // GaiaAuthConsumer will be called on the original thread |
65 // after results come back. This class is thread agnostic. | 63 // after results come back. This class is thread agnostic. |
66 // You can't make more than one request at a time. | 64 // You can't make more than one request at a time. |
67 void StartIssueAuthToken(const std::string& sid, | 65 void StartIssueAuthToken(const std::string& sid, |
68 const std::string& lsid, | 66 const std::string& lsid, |
69 const char* const service); | 67 const char* const service); |
70 | 68 |
71 // Start fetching OAuth login scoped token from the given ClientLogin token | |
72 // for "lso" service. | |
73 // Either OnOAuthLoginTokenSuccess or OnOAuthLoginTokenFailure method will be | |
74 // called on the consumer with results. | |
75 void StartOAuthLoginTokenFetch(const std::string& auth_token); | |
76 | |
77 // Start a request to get a particular key from user info. | 69 // Start a request to get a particular key from user info. |
78 // GaiaAuthConsumer will be called back on the same thread when | 70 // GaiaAuthConsumer will be called back on the same thread when |
79 // results come back. | 71 // results come back. |
80 // You can't make more than one request at a time. | 72 // You can't make more than one request at a time. |
81 void StartGetUserInfo(const std::string& lsid, | 73 void StartGetUserInfo(const std::string& lsid, |
82 const std::string& info_key); | 74 const std::string& info_key); |
83 | 75 |
84 // Start a TokenAuth request to pre-login the user with the given credentials. | 76 // Start a TokenAuth request to pre-login the user with the given credentials. |
85 void StartTokenAuth(const std::string& auth_token); | 77 void StartTokenAuth(const std::string& auth_token); |
86 | 78 |
(...skipping 23 matching lines...) Expand all Loading... |
110 static const char kCookiePersistence[]; | 102 static const char kCookiePersistence[]; |
111 static const char kAccountTypeHostedOrGoogle[]; | 103 static const char kAccountTypeHostedOrGoogle[]; |
112 static const char kAccountTypeGoogle[]; | 104 static const char kAccountTypeGoogle[]; |
113 | 105 |
114 // The format of the POST body for ClientLogin. | 106 // The format of the POST body for ClientLogin. |
115 static const char kClientLoginFormat[]; | 107 static const char kClientLoginFormat[]; |
116 // The format of said POST body when CAPTCHA token & answer are specified. | 108 // The format of said POST body when CAPTCHA token & answer are specified. |
117 static const char kClientLoginCaptchaFormat[]; | 109 static const char kClientLoginCaptchaFormat[]; |
118 // The format of the POST body for IssueAuthToken. | 110 // The format of the POST body for IssueAuthToken. |
119 static const char kIssueAuthTokenFormat[]; | 111 static const char kIssueAuthTokenFormat[]; |
120 // The format of the POST body to get OAuth2 auth code from auth token. | |
121 static const char kClientLoginToOAuth2BodyFormat[]; | |
122 // The format of the POST body to get OAuth2 token pair from auth code. | |
123 static const char kOAuth2CodeToTokenPairBodyFormat[]; | |
124 // The format of the POST body for GetUserInfo. | 112 // The format of the POST body for GetUserInfo. |
125 static const char kGetUserInfoFormat[]; | 113 static const char kGetUserInfoFormat[]; |
126 // The format of the POST body for TokenAuth. | 114 // The format of the POST body for TokenAuth. |
127 static const char kTokenAuthFormat[]; | 115 static const char kTokenAuthFormat[]; |
128 // The format of the POST body for MergeSession. | 116 // The format of the POST body for MergeSession. |
129 static const char kMergeSessionFormat[]; | 117 static const char kMergeSessionFormat[]; |
130 | 118 |
131 // Constants for parsing ClientLogin errors. | 119 // Constants for parsing ClientLogin errors. |
132 static const char kAccountDeletedError[]; | 120 static const char kAccountDeletedError[]; |
133 static const char kAccountDeletedErrorCode[]; | 121 static const char kAccountDeletedErrorCode[]; |
134 static const char kAccountDisabledError[]; | 122 static const char kAccountDisabledError[]; |
135 static const char kAccountDisabledErrorCode[]; | 123 static const char kAccountDisabledErrorCode[]; |
136 static const char kBadAuthenticationError[]; | 124 static const char kBadAuthenticationError[]; |
137 static const char kBadAuthenticationErrorCode[]; | 125 static const char kBadAuthenticationErrorCode[]; |
138 static const char kCaptchaError[]; | 126 static const char kCaptchaError[]; |
139 static const char kCaptchaErrorCode[]; | 127 static const char kCaptchaErrorCode[]; |
140 static const char kServiceUnavailableError[]; | 128 static const char kServiceUnavailableError[]; |
141 static const char kServiceUnavailableErrorCode[]; | 129 static const char kServiceUnavailableErrorCode[]; |
142 static const char kErrorParam[]; | 130 static const char kErrorParam[]; |
143 static const char kErrorUrlParam[]; | 131 static const char kErrorUrlParam[]; |
144 static const char kCaptchaUrlParam[]; | 132 static const char kCaptchaUrlParam[]; |
145 static const char kCaptchaTokenParam[]; | 133 static const char kCaptchaTokenParam[]; |
146 | 134 |
147 // Constants for request/response for OAuth2 requests. | |
148 static const char kAuthHeaderFormat[]; | |
149 static const char kClientLoginToOAuth2CookiePartSecure[]; | |
150 static const char kClientLoginToOAuth2CookiePartHttpOnly[]; | |
151 static const char kClientLoginToOAuth2CookiePartCodePrefix[]; | |
152 static const int kClientLoginToOAuth2CookiePartCodePrefixLength; | |
153 static const char kOAuth2RefreshTokenKey[]; | |
154 static const char kOAuth2AccessTokenKey[]; | |
155 static const char kOAuth2ExpiresInKey[]; | |
156 | |
157 // Process the results of a ClientLogin fetch. | 135 // Process the results of a ClientLogin fetch. |
158 void OnClientLoginFetched(const std::string& data, | 136 void OnClientLoginFetched(const std::string& data, |
159 const net::URLRequestStatus& status, | 137 const net::URLRequestStatus& status, |
160 int response_code); | 138 int response_code); |
161 | 139 |
162 void OnIssueAuthTokenFetched(const std::string& data, | 140 void OnIssueAuthTokenFetched(const std::string& data, |
163 const net::URLRequestStatus& status, | 141 const net::URLRequestStatus& status, |
164 int response_code); | 142 int response_code); |
165 | 143 |
166 void OnClientLoginToOAuth2Fetched(const std::string& data, | |
167 const net::ResponseCookies& cookies, | |
168 const net::URLRequestStatus& status, | |
169 int response_code); | |
170 | |
171 void OnOAuth2TokenPairFetched(const std::string& data, | |
172 const net::URLRequestStatus& status, | |
173 int response_code); | |
174 | |
175 void OnGetUserInfoFetched(const std::string& data, | 144 void OnGetUserInfoFetched(const std::string& data, |
176 const net::URLRequestStatus& status, | 145 const net::URLRequestStatus& status, |
177 int response_code); | 146 int response_code); |
178 | 147 |
179 void OnTokenAuthFetched(const std::string& data, | 148 void OnTokenAuthFetched(const std::string& data, |
180 const net::URLRequestStatus& status, | 149 const net::URLRequestStatus& status, |
181 int response_code); | 150 int response_code); |
182 | 151 |
183 void OnMergeSessionFetched(const std::string& data, | 152 void OnMergeSessionFetched(const std::string& data, |
184 const net::URLRequestStatus& status, | 153 const net::URLRequestStatus& status, |
185 int response_code); | 154 int response_code); |
186 | 155 |
187 // Tokenize the results of a ClientLogin fetch. | 156 // Tokenize the results of a ClientLogin fetch. |
188 static void ParseClientLoginResponse(const std::string& data, | 157 static void ParseClientLoginResponse(const std::string& data, |
189 std::string* sid, | 158 std::string* sid, |
190 std::string* lsid, | 159 std::string* lsid, |
191 std::string* token); | 160 std::string* token); |
192 | 161 |
193 static void ParseClientLoginFailure(const std::string& data, | 162 static void ParseClientLoginFailure(const std::string& data, |
194 std::string* error, | 163 std::string* error, |
195 std::string* error_url, | 164 std::string* error_url, |
196 std::string* captcha_url, | 165 std::string* captcha_url, |
197 std::string* captcha_token); | 166 std::string* captcha_token); |
198 | 167 |
199 // Parse ClientLogin to OAuth2 response. | |
200 static bool ParseClientLoginToOAuth2Response( | |
201 const net::ResponseCookies& cookies, | |
202 std::string* auth_code); | |
203 | |
204 // Parse OAuth2 token pairresponse. | |
205 static bool ParseOAuth2TokenPairResponse(const std::string& data, | |
206 std::string* refresh_token, | |
207 std::string* access_token, | |
208 int* expires_in_secs); | |
209 | |
210 static bool ParseClientLoginToOAuth2Cookie(const std::string& cookie, | |
211 std::string* auth_code); | |
212 | |
213 // Is this a special case Gaia error for TwoFactor auth? | 168 // Is this a special case Gaia error for TwoFactor auth? |
214 static bool IsSecondFactorSuccess(const std::string& alleged_error); | 169 static bool IsSecondFactorSuccess(const std::string& alleged_error); |
215 | 170 |
216 // Given parameters, create a ClientLogin request body. | 171 // Given parameters, create a ClientLogin request body. |
217 static std::string MakeClientLoginBody( | 172 static std::string MakeClientLoginBody( |
218 const std::string& username, | 173 const std::string& username, |
219 const std::string& password, | 174 const std::string& password, |
220 const std::string& source, | 175 const std::string& source, |
221 const char* const service, | 176 const char* const service, |
222 const std::string& login_token, | 177 const std::string& login_token, |
223 const std::string& login_captcha, | 178 const std::string& login_captcha, |
224 HostedAccountsSetting allow_hosted_accounts); | 179 HostedAccountsSetting allow_hosted_accounts); |
225 // Supply the sid / lsid returned from ClientLogin in order to | 180 // Supply the sid / lsid returned from ClientLogin in order to |
226 // request a long lived auth token for a service. | 181 // request a long lived auth token for a service. |
227 static std::string MakeIssueAuthTokenBody(const std::string& sid, | 182 static std::string MakeIssueAuthTokenBody(const std::string& sid, |
228 const std::string& lsid, | 183 const std::string& lsid, |
229 const char* const service); | 184 const char* const service); |
230 // Create body to get OAuth2 auth code. | |
231 static std::string MakeGetAuthCodeBody(); | |
232 // Given auth code, create body to get OAuth2 token pair. | |
233 static std::string MakeGetTokenPairBody(const std::string& auth_code); | |
234 // Supply the lsid returned from ClientLogin in order to fetch | 185 // Supply the lsid returned from ClientLogin in order to fetch |
235 // user information. | 186 // user information. |
236 static std::string MakeGetUserInfoBody(const std::string& lsid); | 187 static std::string MakeGetUserInfoBody(const std::string& lsid); |
237 | 188 |
238 // Supply the authentication token returned from StartIssueAuthToken. | 189 // Supply the authentication token returned from StartIssueAuthToken. |
239 static std::string MakeTokenAuthBody(const std::string& auth_token, | 190 static std::string MakeTokenAuthBody(const std::string& auth_token, |
240 const std::string& continue_url, | 191 const std::string& continue_url, |
241 const std::string& source); | 192 const std::string& source); |
242 | 193 |
243 // Supply the authentication token returned from StartIssueAuthToken. | 194 // Supply the authentication token returned from StartIssueAuthToken. |
244 static std::string MakeMergeSessionBody(const std::string& auth_token, | 195 static std::string MakeMergeSessionBody(const std::string& auth_token, |
245 const std::string& continue_url, | 196 const std::string& continue_url, |
246 const std::string& source); | 197 const std::string& source); |
247 | 198 |
248 static std::string MakeGetAuthCodeHeader(const std::string& auth_token); | |
249 | |
250 void StartOAuth2TokenPairFetch(const std::string& auth_code); | |
251 | |
252 // Create a fetcher useable for making any Gaia request. | 199 // Create a fetcher useable for making any Gaia request. |
253 static content::URLFetcher* CreateGaiaFetcher( | 200 static content::URLFetcher* CreateGaiaFetcher( |
254 net::URLRequestContextGetter* getter, | 201 net::URLRequestContextGetter* getter, |
255 const std::string& body, | 202 const std::string& body, |
256 const std::string& headers, | |
257 const GURL& gaia_gurl, | 203 const GURL& gaia_gurl, |
258 bool use_cookies, | 204 bool send_cookies, |
259 content::URLFetcherDelegate* delegate); | 205 content::URLFetcherDelegate* delegate); |
260 | 206 |
261 // From a URLFetcher result, generate an appropriate error. | 207 // From a URLFetcher result, generate an appropriate error. |
262 // From the API documentation, both IssueAuthToken and ClientLogin have | 208 // From the API documentation, both IssueAuthToken and ClientLogin have |
263 // the same error returns. | 209 // the same error returns. |
264 static GoogleServiceAuthError GenerateAuthError( | 210 static GoogleServiceAuthError GenerateAuthError( |
265 const std::string& data, | 211 const std::string& data, |
266 const net::URLRequestStatus& status); | 212 const net::URLRequestStatus& status); |
267 | 213 |
268 // These fields are common to GaiaAuthFetcher, same every request | 214 // These fields are common to GaiaAuthFetcher, same every request |
269 GaiaAuthConsumer* const consumer_; | 215 GaiaAuthConsumer* const consumer_; |
270 net::URLRequestContextGetter* const getter_; | 216 net::URLRequestContextGetter* const getter_; |
271 std::string source_; | 217 std::string source_; |
272 const GURL client_login_gurl_; | 218 const GURL client_login_gurl_; |
273 const GURL issue_auth_token_gurl_; | 219 const GURL issue_auth_token_gurl_; |
274 const GURL client_login_to_oauth2_gurl_; | |
275 const GURL oauth2_token_gurl_; | |
276 const GURL get_user_info_gurl_; | 220 const GURL get_user_info_gurl_; |
277 const GURL token_auth_gurl_; | 221 const GURL token_auth_gurl_; |
278 const GURL merge_session_gurl_; | 222 const GURL merge_session_gurl_; |
279 | 223 |
280 // While a fetch is going on: | 224 // While a fetch is going on: |
281 scoped_ptr<content::URLFetcher> fetcher_; | 225 scoped_ptr<content::URLFetcher> fetcher_; |
282 std::string request_body_; | 226 std::string request_body_; |
283 std::string requested_service_; // Currently tracked for IssueAuthToken only | 227 std::string requested_service_; // Currently tracked for IssueAuthToken only |
284 std::string requested_info_key_; // Currently tracked for GetUserInfo only | 228 std::string requested_info_key_; // Currently tracked for GetUserInfo only |
285 bool fetch_pending_; | 229 bool fetch_pending_; |
286 | 230 |
287 friend class GaiaAuthFetcherTest; | 231 friend class GaiaAuthFetcherTest; |
288 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, CaptchaParse); | 232 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, CaptchaParse); |
289 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, AccountDeletedError); | 233 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, AccountDeletedError); |
290 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, AccountDisabledError); | 234 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, AccountDisabledError); |
291 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, BadAuthenticationError); | 235 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, BadAuthenticationError); |
292 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, IncomprehensibleError); | 236 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, IncomprehensibleError); |
293 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, ServiceUnavailableError); | 237 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, ServiceUnavailableError); |
294 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, CheckNormalErrorCode); | 238 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, CheckNormalErrorCode); |
295 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, CheckTwoFactorResponse); | 239 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, CheckTwoFactorResponse); |
296 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, LoginNetFailure); | 240 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, LoginNetFailure); |
297 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, | |
298 ParseClientLoginToOAuth2Response); | |
299 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, ParseOAuth2TokenPairResponse); | |
300 | 241 |
301 DISALLOW_COPY_AND_ASSIGN(GaiaAuthFetcher); | 242 DISALLOW_COPY_AND_ASSIGN(GaiaAuthFetcher); |
302 }; | 243 }; |
303 | 244 |
304 #endif // CHROME_COMMON_NET_GAIA_GAIA_AUTH_FETCHER_H_ | 245 #endif // CHROME_COMMON_NET_GAIA_GAIA_AUTH_FETCHER_H_ |
OLD | NEW |