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