OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_AUTHENTICATOR2_H_ | 5 #ifndef CHROME_COMMON_NET_GAIA_GAIA_AUTHENTICATOR2_H_ |
6 #define CHROME_COMMON_NET_GAIA_GAIA_AUTHENTICATOR2_H_ | 6 #define CHROME_COMMON_NET_GAIA_GAIA_AUTHENTICATOR2_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/gtest_prod_util.h" | 10 #include "base/gtest_prod_util.h" |
| 11 #include "chrome/common/net/gaia/gaia_auth_consumer.h" |
11 #include "chrome/common/net/url_fetcher.h" | 12 #include "chrome/common/net/url_fetcher.h" |
12 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
13 | 14 |
14 // Authenticate a user against the Google Accounts ClientLogin API | 15 // Authenticate a user against the Google Accounts ClientLogin API |
15 // with various capabilities and return results to a GaiaAuthConsumer. | 16 // with various capabilities and return results to a GaiaAuthConsumer. |
16 // | 17 // |
17 // In the future, we will also issue auth tokens from this class. | 18 // In the future, we will also issue auth tokens from this class. |
18 // This class should be used on a single thread, but it can be whichever thread | 19 // This class should be used on a single thread, but it can be whichever thread |
19 // that you like. | 20 // that you like. |
| 21 // |
| 22 // This class can handle one request at a time. To parallelize requests, |
| 23 // create multiple GaiaAuthenticator2's. |
20 | 24 |
21 class GaiaAuthConsumer; | |
22 class GaiaAuthenticator2Test; | 25 class GaiaAuthenticator2Test; |
23 | 26 |
24 class GaiaAuthenticator2 : public URLFetcher::Delegate { | 27 class GaiaAuthenticator2 : public URLFetcher::Delegate { |
25 public: | 28 public: |
26 // Constants to use in the ClientLogin request POST body. | 29 // Constants to use in the ClientLogin request POST body. |
27 static const char kChromeOSSource[]; | 30 static const char kChromeOSSource[]; |
28 static const char kContactsService[]; | 31 static const char kContactsService[]; |
29 | 32 |
30 // The URLs for different calls in the Google Accounts programmatic login API. | 33 // The URLs for different calls in the Google Accounts programmatic login API. |
31 static const char kClientLoginUrl[]; | 34 static const char kClientLoginUrl[]; |
32 static const char kIssueAuthTokenUrl[]; | 35 static const char kIssueAuthTokenUrl[]; |
33 static const char kTokenAuthUrl[]; | |
34 | |
35 | 36 |
36 // Magic string indicating that, while a second factor is still | 37 // Magic string indicating that, while a second factor is still |
37 // needed to complete authentication, the user provided the right password. | 38 // needed to complete authentication, the user provided the right password. |
38 static const char kSecondFactor[]; | 39 static const char kSecondFactor[]; |
39 | 40 |
40 // This will later be hidden behind an auth service which caches | 41 // This will later be hidden behind an auth service which caches |
41 // tokens. | 42 // tokens. |
42 GaiaAuthenticator2(GaiaAuthConsumer* consumer, | 43 GaiaAuthenticator2(GaiaAuthConsumer* consumer, |
43 const std::string& source, | 44 const std::string& source, |
44 URLRequestContextGetter* getter); | 45 URLRequestContextGetter* getter); |
45 virtual ~GaiaAuthenticator2(); | 46 virtual ~GaiaAuthenticator2(); |
46 | 47 |
47 // GaiaAuthConsumer will be called on the original thread | 48 // GaiaAuthConsumer will be called on the original thread |
48 // after results come back. This class is thread agnostic. | 49 // after results come back. This class is thread agnostic. |
| 50 // You can't make more than request at a time. |
49 void StartClientLogin(const std::string& username, | 51 void StartClientLogin(const std::string& username, |
50 const std::string& password, | 52 const std::string& password, |
51 const char* const service, | 53 const char* const service, |
52 const std::string& login_token, | 54 const std::string& login_token, |
53 const std::string& login_captcha); | 55 const std::string& login_captcha); |
54 | 56 |
| 57 // GaiaAuthConsumer will be called on the original thread |
| 58 // after results come back. This class is thread agnostic. |
| 59 // You can't make more than one request at a time. |
| 60 void StartIssueAuthToken(const std::string& sid, |
| 61 const std::string& lsid, |
| 62 const char* const service); |
| 63 |
55 // Implementation of URLFetcher::Delegate | 64 // Implementation of URLFetcher::Delegate |
56 void OnURLFetchComplete(const URLFetcher* source, | 65 void OnURLFetchComplete(const URLFetcher* source, |
57 const GURL& url, | 66 const GURL& url, |
58 const URLRequestStatus& status, | 67 const URLRequestStatus& status, |
59 int response_code, | 68 int response_code, |
60 const ResponseCookies& cookies, | 69 const ResponseCookies& cookies, |
61 const std::string& data); | 70 const std::string& data); |
62 | 71 |
63 // StartClientLogin been called && results not back yet? | 72 // StartClientLogin been called && results not back yet? |
64 bool HasPendingFetch(); | 73 bool HasPendingFetch(); |
65 | 74 |
66 // Stop any URL fetches in progress. | 75 // Stop any URL fetches in progress. |
67 void CancelRequest(); | 76 void CancelRequest(); |
68 | 77 |
69 private: | 78 private: |
70 // ClientLogin body constants that don't change | 79 // ClientLogin body constants that don't change |
71 static const char kCookiePersistence[]; | 80 static const char kCookiePersistence[]; |
72 static const char kAccountType[]; | 81 static const char kAccountType[]; |
73 | 82 |
74 // The format of the POST body for ClientLogin. | 83 // The format of the POST body for ClientLogin. |
75 static const char kClientLoginFormat[]; | 84 static const char kClientLoginFormat[]; |
76 // The format of said POST body when CAPTCHA token & answer are specified. | 85 // The format of said POST body when CAPTCHA token & answer are specified. |
77 static const char kClientLoginCaptchaFormat[]; | 86 static const char kClientLoginCaptchaFormat[]; |
| 87 // The format of the POST body for IssueAuthToken. |
| 88 static const char kIssueAuthTokenFormat[]; |
78 | 89 |
79 // Process the results of a ClientLogin fetch. | 90 // Process the results of a ClientLogin fetch. |
80 void OnClientLoginFetched(const std::string& data, | 91 void OnClientLoginFetched(const std::string& data, |
81 const URLRequestStatus& status, | 92 const URLRequestStatus& status, |
82 int response_code); | 93 int response_code); |
83 | 94 |
| 95 void OnIssueAuthTokenFetched(const std::string& data, |
| 96 const URLRequestStatus& status, |
| 97 int response_code); |
| 98 |
84 // Tokenize the results of a ClientLogin fetch. | 99 // Tokenize the results of a ClientLogin fetch. |
85 static void ParseClientLoginResponse(const std::string& data, | 100 static void ParseClientLoginResponse(const std::string& data, |
86 std::string* sid, | 101 std::string* sid, |
87 std::string* lsid, | 102 std::string* lsid, |
88 std::string* token); | 103 std::string* token); |
89 | 104 |
| 105 // From a URLFetcher result, generate an appropriate GaiaAuthError. |
| 106 // From the API documentation, both IssueAuthToken and ClientLogin have |
| 107 // the same error returns. |
| 108 static GaiaAuthConsumer::GaiaAuthError GenerateAuthError( |
| 109 const std::string& data, |
| 110 const URLRequestStatus& status); |
| 111 |
90 // Is this a special case Gaia error for TwoFactor auth? | 112 // Is this a special case Gaia error for TwoFactor auth? |
91 static bool IsSecondFactorSuccess(const std::string& alleged_error); | 113 static bool IsSecondFactorSuccess(const std::string& alleged_error); |
92 | 114 |
93 // Given parameters, create a ClientLogin request body. | 115 // Given parameters, create a ClientLogin request body. |
94 static std::string GenerateRequestBody(const std::string& username, | 116 static std::string MakeClientLoginBody(const std::string& username, |
95 const std::string& password, | 117 const std::string& password, |
96 const std::string& source, | 118 const std::string& source, |
97 const char* service, | 119 const char* const service, |
98 const std::string& login_token, | 120 const std::string& login_token, |
99 const std::string& login_captcha); | 121 const std::string& login_captcha); |
100 | 122 |
101 // Create a fetcher useable for making a ClientLogin request. | 123 static std::string MakeIssueAuthTokenBody(const std::string& sid, |
102 static URLFetcher* CreateClientLoginFetcher(URLRequestContextGetter* getter, | 124 const std::string& lsid, |
103 const std::string& body, | 125 const char* const service); |
104 const GURL& client_login_gurl_, | |
105 URLFetcher::Delegate* delegate); | |
106 | 126 |
| 127 // Create a fetcher useable for making any Gaia request. |
| 128 static URLFetcher* CreateGaiaFetcher(URLRequestContextGetter* getter, |
| 129 const std::string& body, |
| 130 const GURL& gaia_gurl_, |
| 131 URLFetcher::Delegate* delegate); |
| 132 |
| 133 |
| 134 // Unique per GaiaAuthenticator2 |
107 GaiaAuthConsumer* const consumer_; | 135 GaiaAuthConsumer* const consumer_; |
108 scoped_ptr<URLFetcher> fetcher_; | |
109 URLRequestContextGetter* const getter_; | 136 URLRequestContextGetter* const getter_; |
110 std::string source_; | 137 std::string source_; |
111 const GURL client_login_gurl_; | 138 const GURL client_login_gurl_; |
| 139 const GURL issue_auth_token_gurl_; |
| 140 |
| 141 // While a fetch is going on: |
| 142 scoped_ptr<URLFetcher> fetcher_; |
112 std::string request_body_; | 143 std::string request_body_; |
| 144 std::string requested_service_; // Currently tracked for IssueAuthToken only |
113 bool fetch_pending_; | 145 bool fetch_pending_; |
114 | 146 |
115 friend class GaiaAuthenticator2Test; | 147 friend class GaiaAuthenticator2Test; |
116 FRIEND_TEST_ALL_PREFIXES(GaiaAuthenticator2Test, LoginNetFailure); | 148 FRIEND_TEST_ALL_PREFIXES(GaiaAuthenticator2Test, LoginNetFailure); |
117 FRIEND_TEST_ALL_PREFIXES(GaiaAuthenticator2Test, CheckNormalErrorCode); | 149 FRIEND_TEST_ALL_PREFIXES(GaiaAuthenticator2Test, CheckNormalErrorCode); |
118 FRIEND_TEST_ALL_PREFIXES(GaiaAuthenticator2Test, CheckTwoFactorResponse); | 150 FRIEND_TEST_ALL_PREFIXES(GaiaAuthenticator2Test, CheckTwoFactorResponse); |
119 | 151 |
120 DISALLOW_COPY_AND_ASSIGN(GaiaAuthenticator2); | 152 DISALLOW_COPY_AND_ASSIGN(GaiaAuthenticator2); |
121 }; | 153 }; |
122 | 154 |
123 #endif // CHROME_COMMON_NET_GAIA_GAIA_AUTHENTICATOR2_H_ | 155 #endif // CHROME_COMMON_NET_GAIA_GAIA_AUTHENTICATOR2_H_ |
OLD | NEW |