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 | 10 |
11 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "chrome/common/net/gaia/gaia_auth_consumer.h" | 13 #include "chrome/common/net/gaia/gaia_auth_consumer.h" |
14 #include "chrome/common/net/url_fetcher.h" | 14 #include "chrome/common/net/gaia/authentication_fetcher.h" |
15 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
16 | 16 |
17 // Authenticate a user against the Google Accounts ClientLogin API | 17 // Authenticate a user against the Google Accounts ClientLogin API |
18 // with various capabilities and return results to a GaiaAuthConsumer. | 18 // with various capabilities and return results to a GaiaAuthConsumer. |
19 // | 19 // |
20 // 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. |
21 // 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 |
22 // that you like. | 22 // that you like. |
23 // | 23 // |
24 // 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, |
25 // create multiple GaiaAuthFetcher's. | 25 // create multiple GaiaAuthFetcher's. |
26 | 26 |
27 class GaiaAuthFetcherTest; | 27 class GaiaAuthFetcherTest; |
28 | 28 |
29 class GaiaAuthFetcher : public URLFetcher::Delegate { | 29 class GaiaAuthFetcher : public AuthenticationFetcher { |
30 public: | 30 public: |
31 enum HostedAccountsSetting { | |
32 HostedAccountsAllowed, | |
33 HostedAccountsNotAllowed | |
34 }; | |
35 | |
36 // The URLs for different calls in the Google Accounts programmatic login API. | 31 // The URLs for different calls in the Google Accounts programmatic login API. |
37 static const char kClientLoginUrl[]; | 32 static const char kClientLoginUrl[]; |
38 static const char kIssueAuthTokenUrl[]; | 33 static const char kIssueAuthTokenUrl[]; |
39 static const char kGetUserInfoUrl[]; | 34 static const char kGetUserInfoUrl[]; |
40 | 35 |
41 // Magic string indicating that, while a second factor is still | 36 // Magic string indicating that, while a second factor is still |
42 // needed to complete authentication, the user provided the right password. | 37 // needed to complete authentication, the user provided the right password. |
43 static const char kSecondFactor[]; | 38 static const char kSecondFactor[]; |
44 | 39 |
45 // This will later be hidden behind an auth service which caches | 40 // This will later be hidden behind an auth service which caches |
46 // tokens. | 41 // tokens. |
47 GaiaAuthFetcher(GaiaAuthConsumer* consumer, | 42 GaiaAuthFetcher(GaiaAuthConsumer* consumer, |
48 const std::string& source, | 43 const std::string& source, |
49 net::URLRequestContextGetter* getter); | 44 net::URLRequestContextGetter* getter); |
50 virtual ~GaiaAuthFetcher(); | 45 virtual ~GaiaAuthFetcher(); |
51 | 46 |
| 47 virtual void StartAuthentication(const std::string& username, |
| 48 const std::string& password, |
| 49 const char* const service, |
| 50 const std::string& login_token, |
| 51 const std::string& login_captcha, |
| 52 HostedAccountsSetting allow_hosted_accounts); |
| 53 |
52 // GaiaAuthConsumer will be called on the original thread | 54 // GaiaAuthConsumer will be called on the original thread |
53 // after results come back. This class is thread agnostic. | 55 // after results come back. This class is thread agnostic. |
54 // You can't make more than request at a time. | 56 // You can't make more than request at a time. |
55 void StartClientLogin(const std::string& username, | 57 void StartClientLogin(const std::string& username, |
56 const std::string& password, | 58 const std::string& password, |
57 const char* const service, | 59 const char* const service, |
58 const std::string& login_token, | 60 const std::string& login_token, |
59 const std::string& login_captcha, | 61 const std::string& login_captcha, |
60 HostedAccountsSetting allow_hosted_accounts); | 62 HostedAccountsSetting allow_hosted_accounts); |
61 | 63 |
62 // GaiaAuthConsumer will be called on the original thread | 64 // GaiaAuthConsumer will be called on the original thread |
63 // after results come back. This class is thread agnostic. | 65 // after results come back. This class is thread agnostic. |
64 // You can't make more than one request at a time. | 66 // You can't make more than one request at a time. |
65 void StartIssueAuthToken(const std::string& sid, | 67 virtual void StartIssueAuthToken( |
66 const std::string& lsid, | 68 const AuthenticationConsumer::AuthenticationResult& credentials, |
67 const char* const service); | 69 const char* const service); |
68 | 70 |
69 // Start a request to get a particular key from user info. | 71 // Start a request to get a particular key from user info. |
70 // GaiaAuthConsumer will be called back on the same thread when | 72 // GaiaAuthConsumer will be called back on the same thread when |
71 // results come back. | 73 // results come back. |
72 // You can't make more than one request at a time. | 74 // You can't make more than one request at a time. |
73 void StartGetUserInfo(const std::string& lsid, | 75 virtual void StartGetUserInfo(const std::string& lsid, |
74 const std::string& info_key); | 76 const std::string& info_key); |
75 | 77 |
76 // Implementation of URLFetcher::Delegate | 78 // Implementation of URLFetcher::Delegate |
77 virtual void OnURLFetchComplete(const URLFetcher* source, | 79 virtual void OnURLFetchComplete(const URLFetcher* source, |
78 const GURL& url, | 80 const GURL& url, |
79 const net::URLRequestStatus& status, | 81 const net::URLRequestStatus& status, |
80 int response_code, | 82 int response_code, |
81 const ResponseCookies& cookies, | 83 const ResponseCookies& cookies, |
82 const std::string& data); | 84 const std::string& data); |
83 | 85 |
84 // StartClientLogin been called && results not back yet? | |
85 bool HasPendingFetch(); | |
86 | |
87 // Stop any URL fetches in progress. | |
88 void CancelRequest(); | |
89 | |
90 private: | 86 private: |
91 // ClientLogin body constants that don't change | 87 // ClientLogin body constants that don't change |
92 static const char kCookiePersistence[]; | 88 static const char kCookiePersistence[]; |
93 static const char kAccountTypeHostedOrGoogle[]; | 89 static const char kAccountTypeHostedOrGoogle[]; |
94 static const char kAccountTypeGoogle[]; | 90 static const char kAccountTypeGoogle[]; |
95 | 91 |
96 // The format of the POST body for ClientLogin. | 92 // The format of the POST body for ClientLogin. |
97 static const char kClientLoginFormat[]; | 93 static const char kClientLoginFormat[]; |
98 // The format of said POST body when CAPTCHA token & answer are specified. | 94 // The format of said POST body when CAPTCHA token & answer are specified. |
99 static const char kClientLoginCaptchaFormat[]; | 95 static const char kClientLoginCaptchaFormat[]; |
100 // The format of the POST body for IssueAuthToken. | 96 // The format of the POST body for IssueAuthToken. |
101 static const char kIssueAuthTokenFormat[]; | 97 static const char kIssueAuthTokenFormat[]; |
102 // The format of the POSt body for GetUserInfo. | 98 // The format of the POSt body for GetUserInfo. |
103 static const char kGetUserInfoFormat[]; | 99 static const char kGetUserInfoFormat[]; |
104 | 100 |
105 // Constants for parsing ClientLogin errors. | 101 // Constants for parsing ClientLogin errors. |
106 static const char kAccountDeletedError[]; | 102 static const char kAccountDeletedError[]; |
107 static const char kAccountDisabledError[]; | 103 static const char kAccountDisabledError[]; |
108 static const char kBadAuthenticationError[]; | 104 static const char kBadAuthenticationError[]; |
109 static const char kCaptchaError[]; | 105 static const char kCaptchaError[]; |
110 static const char kServiceUnavailableError[]; | 106 static const char kServiceUnavailableError[]; |
111 static const char kErrorParam[]; | 107 static const char kErrorParam[]; |
112 static const char kErrorUrlParam[]; | 108 static const char kErrorUrlParam[]; |
113 static const char kCaptchaUrlParam[]; | 109 static const char kCaptchaUrlParam[]; |
114 static const char kCaptchaTokenParam[]; | 110 static const char kCaptchaTokenParam[]; |
115 static const char kCaptchaUrlPrefix[]; | 111 static const char kCaptchaUrlPrefix[]; |
116 | 112 |
| 113 // Process the results of a Authentication fetch. |
| 114 virtual void OnAuthenticationFetched(const std::string& data, |
| 115 const net::URLRequestStatus& status, |
| 116 int response_code); |
| 117 |
117 // Process the results of a ClientLogin fetch. | 118 // Process the results of a ClientLogin fetch. |
118 void OnClientLoginFetched(const std::string& data, | 119 void OnClientLoginFetched(const std::string& data, |
119 const net::URLRequestStatus& status, | 120 const net::URLRequestStatus& status, |
120 int response_code); | 121 int response_code); |
121 | 122 |
122 void OnIssueAuthTokenFetched(const std::string& data, | 123 void OnIssueAuthTokenFetched(const std::string& data, |
123 const net::URLRequestStatus& status, | 124 const net::URLRequestStatus& status, |
124 int response_code); | 125 int response_code); |
125 | 126 |
| 127 void OnIssueTokenFetched(const std::string& data, |
| 128 const net::URLRequestStatus& status, |
| 129 int response_code); |
| 130 |
126 void OnGetUserInfoFetched(const std::string& data, | 131 void OnGetUserInfoFetched(const std::string& data, |
127 const net::URLRequestStatus& status, | 132 const net::URLRequestStatus& status, |
128 int response_code); | 133 int response_code); |
129 | 134 |
130 // Tokenize the results of a ClientLogin fetch. | 135 // Tokenize the results of a ClientLogin fetch. |
131 static void ParseClientLoginResponse(const std::string& data, | 136 static void ParseClientLoginResponse(const std::string& data, |
132 std::string* sid, | 137 std::string* sid, |
133 std::string* lsid, | 138 std::string* lsid, |
134 std::string* token); | 139 std::string* token); |
135 | 140 |
(...skipping 17 matching lines...) Expand all Loading... |
153 static std::string MakeClientLoginBody( | 158 static std::string MakeClientLoginBody( |
154 const std::string& username, | 159 const std::string& username, |
155 const std::string& password, | 160 const std::string& password, |
156 const std::string& source, | 161 const std::string& source, |
157 const char* const service, | 162 const char* const service, |
158 const std::string& login_token, | 163 const std::string& login_token, |
159 const std::string& login_captcha, | 164 const std::string& login_captcha, |
160 HostedAccountsSetting allow_hosted_accounts); | 165 HostedAccountsSetting allow_hosted_accounts); |
161 // Supply the sid / lsid returned from ClientLogin in order to | 166 // Supply the sid / lsid returned from ClientLogin in order to |
162 // request a long lived auth token for a service. | 167 // request a long lived auth token for a service. |
163 static std::string MakeIssueAuthTokenBody(const std::string& sid, | 168 static std::string MakeIssueAuthTokenBody( |
164 const std::string& lsid, | 169 const AuthenticationConsumer::AuthenticationResult& credentials, |
165 const char* const service); | 170 const char* const service); |
166 // Supply the lsid returned from ClientLogin in order to fetch | 171 // Supply the lsid returned from ClientLogin in order to fetch |
167 // user information. | 172 // user information. |
168 static std::string MakeGetUserInfoBody(const std::string& lsid); | 173 static std::string MakeGetUserInfoBody(const std::string& lsid); |
169 | 174 |
170 // Create a fetcher useable for making any Gaia request. | 175 // Create a fetcher useable for making any Gaia request. |
171 static URLFetcher* CreateGaiaFetcher(net::URLRequestContextGetter* getter, | 176 static URLFetcher* CreateGaiaFetcher(net::URLRequestContextGetter* getter, |
172 const std::string& body, | 177 const std::string& body, |
173 const GURL& gaia_gurl_, | 178 const GURL& gaia_gurl_, |
174 URLFetcher::Delegate* delegate); | 179 URLFetcher::Delegate* delegate); |
175 | |
176 | |
177 // These fields are common to GaiaAuthFetcher, same every request | 180 // These fields are common to GaiaAuthFetcher, same every request |
178 GaiaAuthConsumer* const consumer_; | |
179 net::URLRequestContextGetter* const getter_; | |
180 std::string source_; | |
181 const GURL client_login_gurl_; | 181 const GURL client_login_gurl_; |
182 const GURL issue_auth_token_gurl_; | 182 const GURL issue_auth_token_gurl_; |
183 const GURL get_user_info_gurl_; | 183 const GURL get_user_info_gurl_; |
184 | 184 |
185 // While a fetch is going on: | 185 // While a fetch is going on: |
186 scoped_ptr<URLFetcher> fetcher_; | 186 scoped_ptr<URLFetcher> fetcher_; |
187 std::string request_body_; | 187 std::string request_body_; |
188 std::string requested_service_; // Currently tracked for IssueAuthToken only | 188 std::string requested_service_; // Currently tracked for IssueAuthToken only |
189 std::string requested_info_key_; // Currently tracked for GetUserInfo only | 189 std::string requested_info_key_; // Currently tracked for GetUserInfo only |
190 bool fetch_pending_; | |
191 | 190 |
192 friend class GaiaAuthFetcherTest; | 191 friend class GaiaAuthFetcherTest; |
193 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, CaptchaParse); | 192 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, CaptchaParse); |
194 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, AccountDeletedError); | 193 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, AccountDeletedError); |
195 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, AccountDisabledError); | 194 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, AccountDisabledError); |
196 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, BadAuthenticationError); | 195 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, BadAuthenticationError); |
197 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, IncomprehensibleError); | 196 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, IncomprehensibleError); |
198 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, ServiceUnavailableError); | 197 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, ServiceUnavailableError); |
199 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, CheckNormalErrorCode); | 198 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, CheckNormalErrorCode); |
200 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, CheckTwoFactorResponse); | 199 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, CheckTwoFactorResponse); |
201 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, LoginNetFailure); | 200 FRIEND_TEST_ALL_PREFIXES(GaiaAuthFetcherTest, LoginNetFailure); |
202 | 201 |
203 DISALLOW_COPY_AND_ASSIGN(GaiaAuthFetcher); | 202 DISALLOW_COPY_AND_ASSIGN(GaiaAuthFetcher); |
204 }; | 203 }; |
205 | 204 |
206 #endif // CHROME_COMMON_NET_GAIA_GAIA_AUTH_FETCHER_H_ | 205 #endif // CHROME_COMMON_NET_GAIA_GAIA_AUTH_FETCHER_H_ |
OLD | NEW |