OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
11 #include <utility> | 11 #include <utility> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
16 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
17 #include "base/observer_list.h" | 17 #include "base/observer_list.h" |
18 #include "build/build_config.h" | 18 #include "build/build_config.h" |
19 #include "chrome/browser/extensions/api/identity/extension_token_key.h" | 19 #include "chrome/browser/extensions/api/identity/extension_token_key.h" |
20 #include "chrome/browser/extensions/api/identity/gaia_web_auth_flow.h" | 20 #include "chrome/browser/extensions/api/identity/gaia_web_auth_flow.h" |
| 21 #include "chrome/browser/extensions/api/identity/identity_get_auth_token_functio
n.h" |
21 #include "chrome/browser/extensions/api/identity/identity_get_profile_user_info_
function.h" | 22 #include "chrome/browser/extensions/api/identity/identity_get_profile_user_info_
function.h" |
22 #include "chrome/browser/extensions/api/identity/identity_launch_web_auth_flow_f
unction.h" | 23 #include "chrome/browser/extensions/api/identity/identity_launch_web_auth_flow_f
unction.h" |
23 #include "chrome/browser/extensions/api/identity/identity_mint_queue.h" | 24 #include "chrome/browser/extensions/api/identity/identity_mint_queue.h" |
24 #include "chrome/browser/extensions/api/identity/identity_remove_cached_auth_tok
en_function.h" | 25 #include "chrome/browser/extensions/api/identity/identity_remove_cached_auth_tok
en_function.h" |
25 #include "chrome/browser/extensions/api/identity/identity_signin_flow.h" | 26 #include "chrome/browser/extensions/api/identity/identity_signin_flow.h" |
26 #include "chrome/browser/extensions/api/identity/web_auth_flow.h" | 27 #include "chrome/browser/extensions/api/identity/web_auth_flow.h" |
27 #include "chrome/browser/extensions/chrome_extension_function.h" | 28 #include "chrome/browser/extensions/chrome_extension_function.h" |
28 #include "components/signin/core/browser/profile_identity_provider.h" | 29 #include "components/signin/core/browser/profile_identity_provider.h" |
29 #include "extensions/browser/browser_context_keyed_api_factory.h" | 30 #include "extensions/browser/browser_context_keyed_api_factory.h" |
30 #include "google_apis/gaia/account_tracker.h" | 31 #include "google_apis/gaia/account_tracker.h" |
31 #include "google_apis/gaia/oauth2_mint_token_flow.h" | 32 #include "google_apis/gaia/oauth2_mint_token_flow.h" |
32 #include "google_apis/gaia/oauth2_token_service.h" | 33 #include "google_apis/gaia/oauth2_token_service.h" |
33 | 34 |
34 class GoogleServiceAuthError; | |
35 class MockGetAuthTokenFunction; | |
36 | |
37 namespace content { | 35 namespace content { |
38 class BrowserContext; | 36 class BrowserContext; |
39 } | 37 } |
40 | 38 |
41 namespace extensions { | 39 namespace extensions { |
42 | 40 |
43 class GetAuthTokenFunctionTest; | |
44 class IdentityGetAuthTokenFunction; | 41 class IdentityGetAuthTokenFunction; |
45 class MockGetAuthTokenFunction; | |
46 | 42 |
47 class IdentityTokenCacheValue { | 43 class IdentityTokenCacheValue { |
48 public: | 44 public: |
49 IdentityTokenCacheValue(); | 45 IdentityTokenCacheValue(); |
50 explicit IdentityTokenCacheValue(const IssueAdviceInfo& issue_advice); | 46 explicit IdentityTokenCacheValue(const IssueAdviceInfo& issue_advice); |
51 IdentityTokenCacheValue(const std::string& token, | 47 IdentityTokenCacheValue(const std::string& token, |
52 base::TimeDelta time_to_live); | 48 base::TimeDelta time_to_live); |
53 IdentityTokenCacheValue(const IdentityTokenCacheValue& other); | 49 IdentityTokenCacheValue(const IdentityTokenCacheValue& other); |
54 ~IdentityTokenCacheValue(); | 50 ~IdentityTokenCacheValue(); |
55 | 51 |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 | 140 |
145 IdentityGetAccountsFunction(); | 141 IdentityGetAccountsFunction(); |
146 | 142 |
147 private: | 143 private: |
148 ~IdentityGetAccountsFunction() override; | 144 ~IdentityGetAccountsFunction() override; |
149 | 145 |
150 // UIThreadExtensionFunction implementation. | 146 // UIThreadExtensionFunction implementation. |
151 ExtensionFunction::ResponseAction Run() override; | 147 ExtensionFunction::ResponseAction Run() override; |
152 }; | 148 }; |
153 | 149 |
154 // identity.getAuthToken fetches an OAuth 2 function for the | |
155 // caller. The request has three sub-flows: non-interactive, | |
156 // interactive, and sign-in. | |
157 // | |
158 // In the non-interactive flow, getAuthToken requests a token from | |
159 // GAIA. GAIA may respond with a token, an error, or "consent | |
160 // required". In the consent required cases, getAuthToken proceeds to | |
161 // the second, interactive phase. | |
162 // | |
163 // The interactive flow presents a scope approval dialog to the | |
164 // user. If the user approves the request, a grant will be recorded on | |
165 // the server, and an access token will be returned to the caller. | |
166 // | |
167 // In some cases we need to display a sign-in dialog. Normally the | |
168 // profile will be signed in already, but if it turns out we need a | |
169 // new login token, there is a sign-in flow. If that flow completes | |
170 // successfully, getAuthToken proceeds to the non-interactive flow. | |
171 class IdentityGetAuthTokenFunction : public ChromeAsyncExtensionFunction, | |
172 public GaiaWebAuthFlow::Delegate, | |
173 public IdentityMintRequestQueue::Request, | |
174 public OAuth2MintTokenFlow::Delegate, | |
175 public IdentitySigninFlow::Delegate, | |
176 public OAuth2TokenService::Consumer { | |
177 public: | |
178 DECLARE_EXTENSION_FUNCTION("identity.getAuthToken", | |
179 EXPERIMENTAL_IDENTITY_GETAUTHTOKEN); | |
180 | |
181 IdentityGetAuthTokenFunction(); | |
182 | |
183 const ExtensionTokenKey* GetExtensionTokenKeyForTest() { | |
184 return token_key_.get(); | |
185 } | |
186 | |
187 void Shutdown(); | |
188 | |
189 protected: | |
190 ~IdentityGetAuthTokenFunction() override; | |
191 | |
192 // IdentitySigninFlow::Delegate implementation: | |
193 void SigninSuccess() override; | |
194 void SigninFailed() override; | |
195 | |
196 // GaiaWebAuthFlow::Delegate implementation: | |
197 void OnGaiaFlowFailure(GaiaWebAuthFlow::Failure failure, | |
198 GoogleServiceAuthError service_error, | |
199 const std::string& oauth_error) override; | |
200 void OnGaiaFlowCompleted(const std::string& access_token, | |
201 const std::string& expiration) override; | |
202 | |
203 // Starts a login access token request. | |
204 virtual void StartLoginAccessTokenRequest(); | |
205 | |
206 // OAuth2TokenService::Consumer implementation: | |
207 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | |
208 const std::string& access_token, | |
209 const base::Time& expiration_time) override; | |
210 void OnGetTokenFailure(const OAuth2TokenService::Request* request, | |
211 const GoogleServiceAuthError& error) override; | |
212 | |
213 // Starts a mint token request to GAIA. | |
214 // Exposed for testing. | |
215 virtual void StartGaiaRequest(const std::string& login_access_token); | |
216 | |
217 // Caller owns the returned instance. | |
218 // Exposed for testing. | |
219 virtual OAuth2MintTokenFlow* CreateMintTokenFlow(); | |
220 | |
221 std::unique_ptr<OAuth2TokenService::Request> login_token_request_; | |
222 | |
223 private: | |
224 FRIEND_TEST_ALL_PREFIXES(GetAuthTokenFunctionTest, | |
225 ComponentWithChromeClientId); | |
226 FRIEND_TEST_ALL_PREFIXES(GetAuthTokenFunctionTest, | |
227 ComponentWithNormalClientId); | |
228 FRIEND_TEST_ALL_PREFIXES(GetAuthTokenFunctionTest, InteractiveQueueShutdown); | |
229 FRIEND_TEST_ALL_PREFIXES(GetAuthTokenFunctionTest, NoninteractiveShutdown); | |
230 | |
231 // ExtensionFunction: | |
232 bool RunAsync() override; | |
233 | |
234 // Helpers to report async function results to the caller. | |
235 void StartAsyncRun(); | |
236 void CompleteAsyncRun(bool success); | |
237 void CompleteFunctionWithResult(const std::string& access_token); | |
238 void CompleteFunctionWithError(const std::string& error); | |
239 | |
240 // Initiate/complete the sub-flows. | |
241 void StartSigninFlow(); | |
242 void StartMintTokenFlow(IdentityMintRequestQueue::MintType type); | |
243 void CompleteMintTokenFlow(); | |
244 | |
245 // IdentityMintRequestQueue::Request implementation: | |
246 void StartMintToken(IdentityMintRequestQueue::MintType type) override; | |
247 | |
248 // OAuth2MintTokenFlow::Delegate implementation: | |
249 void OnMintTokenSuccess(const std::string& access_token, | |
250 int time_to_live) override; | |
251 void OnMintTokenFailure(const GoogleServiceAuthError& error) override; | |
252 void OnIssueAdviceSuccess(const IssueAdviceInfo& issue_advice) override; | |
253 | |
254 #if defined(OS_CHROMEOS) | |
255 // Starts a login access token request for device robot account. This method | |
256 // will be called only in Chrome OS for: | |
257 // 1. Enterprise kiosk mode. | |
258 // 2. Whitelisted first party apps in public session. | |
259 virtual void StartDeviceLoginAccessTokenRequest(); | |
260 | |
261 bool IsOriginWhitelistedInPublicSession(); | |
262 #endif | |
263 | |
264 // Methods for invoking UI. Overridable for testing. | |
265 virtual void ShowLoginPopup(); | |
266 virtual void ShowOAuthApprovalDialog(const IssueAdviceInfo& issue_advice); | |
267 | |
268 // Checks if there is a master login token to mint tokens for the extension. | |
269 bool HasLoginToken() const; | |
270 | |
271 // Maps OAuth2 protocol errors to an error message returned to the | |
272 // developer in chrome.runtime.lastError. | |
273 std::string MapOAuth2ErrorToDescription(const std::string& error); | |
274 | |
275 std::string GetOAuth2ClientId() const; | |
276 | |
277 bool interactive_; | |
278 bool should_prompt_for_scopes_; | |
279 IdentityMintRequestQueue::MintType mint_token_flow_type_; | |
280 std::unique_ptr<OAuth2MintTokenFlow> mint_token_flow_; | |
281 OAuth2MintTokenFlow::Mode gaia_mint_token_mode_; | |
282 bool should_prompt_for_signin_; | |
283 | |
284 std::unique_ptr<ExtensionTokenKey> token_key_; | |
285 std::string oauth2_client_id_; | |
286 // When launched in interactive mode, and if there is no existing grant, | |
287 // a permissions prompt will be popped up to the user. | |
288 IssueAdviceInfo issue_advice_; | |
289 std::unique_ptr<GaiaWebAuthFlow> gaia_web_auth_flow_; | |
290 std::unique_ptr<IdentitySigninFlow> signin_flow_; | |
291 }; | |
292 | |
293 } // namespace extensions | 150 } // namespace extensions |
294 | 151 |
295 #endif // CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_ | 152 #endif // CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_ |
OLD | NEW |