1 // Copyright (c) 2012 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.
5 #ifndef CHROME_BROWSER_NET_GAIA_GAIA_OAUTH_FETCHER_H_
6 #define CHROME_BROWSER_NET_GAIA_GAIA_OAUTH_FETCHER_H_
11 #include "base/memory/scoped_ptr.h"
12 #include "google_apis/gaia/google_service_auth_error.h"
13 #include "net/url_request/url_fetcher_delegate.h"
16 class GaiaOAuthConsumer;
20 class URLRequestContextGetter;
21 class URLRequestStatus;
22 typedef std::vector<std::string> ResponseCookies;
25 // Authenticate a user using Gaia's OAuth1 and OAuth2 support.
27 // Users of this class typically desire an OAuth2 Access token scoped for a
28 // specific service. This will typically start with either an interactive
29 // login, using StartOAuthLogin, or with a long-lived OAuth1 all-scope
30 // token obtained through a previous login or other means, using
31 // StartOAuthGetAccessToken. In fact, one can start with any of these
34 // StartGetOAuthTokenRequest()
35 // StartOAuthGetAccessToken()
36 // StartOAuthWrapBridge()
38 // with the expectation that each of these calls the next Start* routine in
39 // the sequence, except for StartUserInfo as it's the last one.
41 // This class can handle one request at a time, and all calls through an
42 // instance should be serialized.
43 class GaiaOAuthFetcher : public net::URLFetcherDelegate {
45 // Defines steps of OAuth process performed by this class.
49 OAUTH1_ALL_ACCESS_TOKEN,
50 OAUTH2_SERVICE_ACCESS_TOKEN,
55 GaiaOAuthFetcher(GaiaOAuthConsumer* consumer,
56 net::URLRequestContextGetter* getter,
57 const std::string& service_scope);
59 virtual ~GaiaOAuthFetcher();
61 // Sets the mask of which OAuth fetch steps should be automatically kicked
62 // of upon successful completition of the previous steps. By default,
63 // this class will chain all steps in OAuth proccess.
64 void SetAutoFetchLimit(RequestType limit) { auto_fetch_limit_ = limit; }
66 // Non-UI version of the method above. Initiates Gaia OAuth request token
68 void StartGetOAuthTokenRequest();
70 // Performs account login based on OAuth1 access token and its secret.
71 void StartOAuthLogin(const char* source,
73 const std::string& oauth1_access_token,
74 const std::string& oauth1_access_token_secret);
76 // Obtains an OAuth1 access token and secret
78 // oauth1_request_token is from GetOAuthToken's result.
79 virtual void StartOAuthGetAccessToken(
80 const std::string& oauth1_request_token);
82 // Obtains an OAuth2 access token using Gaia's OAuth1-to-OAuth2 bridge.
84 // oauth1_access_token and oauth1_access_token_secret are from
85 // OAuthGetAccessToken's result.
87 // wrap_token_duration is typically one hour,
88 // which is also the max -- you can only decrease it.
90 // service_scope will be used as a service name. For example, Chromium Sync
91 // uses https://www.googleapis.com/auth/chromesync for its OAuth2 service
92 // scope here as well as for its service name in TokenService.
93 virtual void StartOAuthWrapBridge(
94 const std::string& oauth1_access_token,
95 const std::string& oauth1_access_token_secret,
96 const std::string& wrap_token_duration,
97 const std::string& service_scope);
99 // Obtains user information related to an OAuth2 access token
101 // oauth2_access_token is from OAuthWrapBridge's result.
102 virtual void StartUserInfo(const std::string& oauth2_access_token);
104 // Starts a request for revoking the given OAuth access token (as requested by
105 // StartOAuthGetAccessToken).
106 virtual void StartOAuthRevokeAccessToken(const std::string& token,
107 const std::string& secret);
109 // Starts a request for revoking the given OAuth Bearer token (as requested by
110 // StartOAuthWrapBridge).
111 virtual void StartOAuthRevokeWrapToken(const std::string& token);
113 // Implementation of net::URLFetcherDelegate
114 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
116 // StartGetOAuthToken (or other Start* routine) been called, but results
118 virtual bool HasPendingFetch() const;
120 // Stop any URL fetches in progress.
121 virtual void CancelRequest();
124 // Stores the type of the current request in flight.
125 RequestType request_type_;
128 // Process the results of a GetOAuthToken fetch for non-UI driven path.
129 virtual void OnGetOAuthTokenUrlFetched(const net::ResponseCookies& cookies,
130 const net::URLRequestStatus& status,
133 // Process the results of a OAuthLogin fetch.
134 virtual void OnOAuthLoginFetched(const std::string& data,
135 const net::URLRequestStatus& status,
138 // Process the results of a OAuthGetAccessToken fetch.
139 virtual void OnOAuthGetAccessTokenFetched(const std::string& data,
140 const net::URLRequestStatus& status,
143 // Process the results of a OAuthWrapBridge fetch.
144 virtual void OnOAuthWrapBridgeFetched(const std::string& data,
145 const net::URLRequestStatus& status,
148 // Process the results of a token revocation fetch.
149 virtual void OnOAuthRevokeTokenFetched(const std::string& data,
150 const net::URLRequestStatus& status,
153 // Process the results of a userinfo fetch.
154 virtual void OnUserInfoFetched(const std::string& data,
155 const net::URLRequestStatus& status,
158 // Tokenize the results of a OAuthLogin fetch.
159 static void ParseOAuthLoginResponse(const std::string& data,
164 // Tokenize the results of a OAuthGetAccessToken fetch.
165 static void ParseOAuthGetAccessTokenResponse(const std::string& data,
167 std::string* secret);
169 // Tokenize the results of a OAuthWrapBridge fetch.
170 static void ParseOAuthWrapBridgeResponse(const std::string& data,
172 std::string* expires_in);
174 // Tokenize the results of a userinfo fetch.
175 static void ParseUserInfoResponse(const std::string& data,
178 // From a URLFetcher result, generate an appropriate error.
179 static GoogleServiceAuthError GenerateAuthError(
180 const std::string& data,
181 const net::URLRequestStatus& status,
184 // Given parameters, create a OAuth v1 request URL.
185 static GURL MakeGetOAuthTokenUrl(const std::string& oauth1_login_scope,
186 const std::string& product_name);
188 // Given parameters, create a OAuthGetAccessToken request body.
189 static std::string MakeOAuthGetAccessTokenBody(
190 const std::string& oauth1_request_token);
192 // Given parameters, create a OAuthLogin request body.
193 static std::string MakeOAuthLoginBody(
196 const std::string& oauth1_access_token,
197 const std::string& oauth1_access_token_secret);
199 // Given parameters, create a OAuthWrapBridge request body.
200 static std::string MakeOAuthWrapBridgeBody(
201 const std::string& oauth1_access_token,
202 const std::string& oauth1_access_token_secret,
203 const std::string& wrap_token_duration,
204 const std::string& oauth2_service_scope);
206 // Create a fetcher useable for making any Gaia OAuth request.
207 static net::URLFetcher* CreateGaiaFetcher(
208 net::URLRequestContextGetter* getter,
209 const GURL& gaia_gurl_,
210 const std::string& body,
211 const std::string& headers,
213 net::URLFetcherDelegate* delegate);
215 bool ShouldAutoFetch(RequestType fetch_step);
217 // These fields are common to GaiaOAuthFetcher, same every request
218 GaiaOAuthConsumer* const consumer_;
219 net::URLRequestContextGetter* const getter_;
221 // While a fetch is going on:
222 scoped_ptr<net::URLFetcher> fetcher_;
223 std::string request_body_;
224 std::string request_headers_;
225 std::string service_scope_;
227 RequestType auto_fetch_limit_;
229 DISALLOW_COPY_AND_ASSIGN(GaiaOAuthFetcher);
232 #endif // CHROME_BROWSER_NET_GAIA_GAIA_OAUTH_FETCHER_H_