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_H_ |
| 6 #define CHROME_COMMON_NET_GAIA_AUTHENTICATION_FETCHER_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_consumer.h" |
| 14 #include "chrome/common/net/url_fetcher.h" |
| 15 #include "googleurl/src/gurl.h" |
| 16 |
| 17 // Authenticate a user against the Google Accounts Authentication API |
| 18 // with various capabilities and return results to a AuthenticationConsumer. |
| 19 // |
| 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 |
| 22 // that you like. |
| 23 // |
| 24 // This class can handle one request at a time. To parallelize requests, |
| 25 // create multiple AuthenticationFetcher's. |
| 26 |
| 27 class AuthenticationFetcherTest; |
| 28 |
| 29 class AuthenticationFetcher : public URLFetcher::Delegate { |
| 30 public: |
| 31 enum HostedAccountsSetting { |
| 32 HostedAccountsAllowed, |
| 33 HostedAccountsNotAllowed |
| 34 }; |
| 35 |
| 36 // The URLs for different calls in the Google Accounts programmatic login API. |
| 37 static const char kAuthenticationUrl[]; |
| 38 static const char kIssueAuthTokenUrl[]; |
| 39 static const char kGetUserInfoUrl[]; |
| 40 |
| 41 // Magic string indicating that, while a second factor is still |
| 42 // needed to complete authentication, the user provided the right password. |
| 43 static const char kSecondFactor[]; |
| 44 |
| 45 // Create a fetcher useable for making any Authentication request. |
| 46 static AuthenticationFetcher* CreateAuthenticationFetcher( |
| 47 const std::string& variant, |
| 48 AuthenticationConsumer* consumer, |
| 49 const std::string& source, |
| 50 net::URLRequestContextGetter* getter); |
| 51 |
| 52 virtual ~AuthenticationFetcher(); |
| 53 |
| 54 // AuthenticationConsumer will be called on the original thread |
| 55 // after results come back. This class is thread agnostic. |
| 56 // You can't make more than request at a time. |
| 57 virtual void StartAuthentication(const std::string& username, |
| 58 const std::string& password, |
| 59 const char* const service, |
| 60 const std::string& login_token, |
| 61 const std::string& login_captcha, |
| 62 HostedAccountsSetting allow_hosted_accounts) |
| 63 = 0; |
| 64 |
| 65 // AuthenticationConsumer will be called on the original thread |
| 66 // after results come back. This class is thread agnostic. |
| 67 // You can't make more than one request at a time. |
| 68 virtual void StartIssueAuthToken( |
| 69 const AuthenticationConsumer::AuthenticationResult& credentials, |
| 70 const char* const service) = 0; |
| 71 |
| 72 // Start a request to get a particular key from user info. |
| 73 // AuthenticationConsumer will be called back on the same thread when |
| 74 // results come back. |
| 75 // You can't make more than one request at a time. |
| 76 virtual void StartGetUserInfo(const std::string& lsid, |
| 77 const std::string& info_key) = 0; |
| 78 |
| 79 // Implementation of URLFetcher::Delegate |
| 80 virtual void OnURLFetchComplete(const URLFetcher* source, |
| 81 const GURL& url, |
| 82 const net::URLRequestStatus& status, |
| 83 int response_code, |
| 84 const ResponseCookies& cookies, |
| 85 const std::string& data) = 0; |
| 86 |
| 87 // StartAuthentication been called && results not back yet? |
| 88 bool HasPendingFetch() const; |
| 89 |
| 90 // Stop any URL fetches in progress. |
| 91 void CancelRequest(); |
| 92 |
| 93 protected: |
| 94 AuthenticationFetcher(AuthenticationConsumer* consumer, |
| 95 const std::string& source, |
| 96 net::URLRequestContextGetter* getter); |
| 97 |
| 98 // Authentication body constants that don't change |
| 99 static const char kCookiePersistence[]; |
| 100 static const char kAccountTypeHostedOrGoogle[]; |
| 101 static const char kAccountTypeGoogle[]; |
| 102 |
| 103 // The format of the POST body for Authentication. |
| 104 static const char kAuthenticationFormat[]; |
| 105 // The format of said POST body when CAPTCHA token & answer are specified. |
| 106 static const char kAuthenticationCaptchaFormat[]; |
| 107 // The format of the POST body for IssueAuthToken. |
| 108 static const char kIssueAuthTokenFormat[]; |
| 109 // The format of the POSt body for GetUserInfo. |
| 110 static const char kGetUserInfoFormat[]; |
| 111 |
| 112 // Constants for parsing Authentication errors. |
| 113 static const char kAccountDeletedError[]; |
| 114 static const char kAccountDisabledError[]; |
| 115 static const char kBadAuthenticationError[]; |
| 116 static const char kCaptchaError[]; |
| 117 static const char kServiceUnavailableError[]; |
| 118 static const char kErrorParam[]; |
| 119 static const char kErrorUrlParam[]; |
| 120 static const char kCaptchaUrlParam[]; |
| 121 static const char kCaptchaTokenParam[]; |
| 122 static const char kCaptchaUrlPrefix[]; |
| 123 |
| 124 // Indicate no pending fetch. |
| 125 void ClearPending(); |
| 126 |
| 127 // Indicate a pending fetch. |
| 128 void SetPending(); |
| 129 |
| 130 // Process the results of a Authentication fetch. |
| 131 virtual void OnAuthenticationFetched(const std::string& data, |
| 132 const net::URLRequestStatus& status, |
| 133 int response_code) = 0; |
| 134 |
| 135 virtual void OnIssueAuthTokenFetched(const std::string& data, |
| 136 const net::URLRequestStatus& status, |
| 137 int response_code) = 0; |
| 138 |
| 139 virtual void OnGetUserInfoFetched(const std::string& data, |
| 140 const net::URLRequestStatus& status, |
| 141 int response_code) = 0; |
| 142 |
| 143 // Create a fetcher useable for making any Gaia request. |
| 144 static URLFetcher* CreateAuthenticationFetcher( |
| 145 net::URLRequestContextGetter* getter, |
| 146 const std::string& body, |
| 147 const GURL& gaia_gurl_, |
| 148 URLFetcher::Delegate* delegate); |
| 149 |
| 150 // These fields are common to AuthenticationFetcher, same every request |
| 151 AuthenticationConsumer* const consumer_; |
| 152 net::URLRequestContextGetter* const getter_; |
| 153 std::string source_; |
| 154 |
| 155 // While a fetch is going on: |
| 156 scoped_ptr<AuthenticationFetcher> fetcher_; |
| 157 |
| 158 private: |
| 159 bool fetch_pending_; |
| 160 |
| 161 friend class AuthenticationFetcherTest; |
| 162 FRIEND_TEST_ALL_PREFIXES(AuthenticationFetcherTest, CaptchaParse); |
| 163 FRIEND_TEST_ALL_PREFIXES(AuthenticationFetcherTest, AccountDeletedError); |
| 164 FRIEND_TEST_ALL_PREFIXES(AuthenticationFetcherTest, AccountDisabledError); |
| 165 FRIEND_TEST_ALL_PREFIXES(AuthenticationFetcherTest, BadAuthenticationError); |
| 166 FRIEND_TEST_ALL_PREFIXES(AuthenticationFetcherTest, IncomprehensibleError); |
| 167 FRIEND_TEST_ALL_PREFIXES(AuthenticationFetcherTest, ServiceUnavailableError); |
| 168 FRIEND_TEST_ALL_PREFIXES(AuthenticationFetcherTest, CheckNormalErrorCode); |
| 169 FRIEND_TEST_ALL_PREFIXES(AuthenticationFetcherTest, CheckTwoFactorResponse); |
| 170 FRIEND_TEST_ALL_PREFIXES(AuthenticationFetcherTest, LoginNetFailure); |
| 171 |
| 172 DISALLOW_COPY_AND_ASSIGN(AuthenticationFetcher); |
| 173 }; |
| 174 |
| 175 #endif // CHROME_COMMON_NET_GAIA_AUTHENTICATION_FETCHER_H_ |
OLD | NEW |