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 <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "chrome/browser/extensions/api/identity/gaia_web_auth_flow.h" |
14 #include "chrome/browser/extensions/api/identity/identity_mint_queue.h" | 15 #include "chrome/browser/extensions/api/identity/identity_mint_queue.h" |
15 #include "chrome/browser/extensions/api/identity/identity_signin_flow.h" | 16 #include "chrome/browser/extensions/api/identity/identity_signin_flow.h" |
16 #include "chrome/browser/extensions/api/identity/web_auth_flow.h" | 17 #include "chrome/browser/extensions/api/identity/web_auth_flow.h" |
17 #include "chrome/browser/extensions/api/profile_keyed_api_factory.h" | 18 #include "chrome/browser/extensions/api/profile_keyed_api_factory.h" |
18 #include "chrome/browser/extensions/extension_function.h" | 19 #include "chrome/browser/extensions/extension_function.h" |
19 #include "chrome/browser/extensions/extension_install_prompt.h" | |
20 #include "chrome/browser/signin/signin_global_error.h" | 20 #include "chrome/browser/signin/signin_global_error.h" |
21 #include "google_apis/gaia/oauth2_mint_token_flow.h" | 21 #include "google_apis/gaia/oauth2_mint_token_flow.h" |
22 | 22 |
23 class GetAuthTokenFunctionTest; | |
24 class MockGetAuthTokenFunction; | |
25 class GoogleServiceAuthError; | 23 class GoogleServiceAuthError; |
26 class Profile; | 24 class Profile; |
27 class SigninManagerBase; | 25 class SigninManagerBase; |
28 | 26 |
29 namespace extensions { | 27 namespace extensions { |
30 | 28 |
| 29 class GetAuthTokenFunctionTest; |
| 30 class MockGetAuthTokenFunction; |
| 31 |
31 namespace identity_constants { | 32 namespace identity_constants { |
32 extern const char kInvalidClientId[]; | 33 extern const char kInvalidClientId[]; |
33 extern const char kInvalidScopes[]; | 34 extern const char kInvalidScopes[]; |
34 extern const char kAuthFailure[]; | 35 extern const char kAuthFailure[]; |
35 extern const char kNoGrant[]; | 36 extern const char kNoGrant[]; |
36 extern const char kUserRejected[]; | 37 extern const char kUserRejected[]; |
37 extern const char kUserNotSignedIn[]; | 38 extern const char kUserNotSignedIn[]; |
38 extern const char kInteractionRequired[]; | 39 extern const char kInteractionRequired[]; |
39 extern const char kInvalidRedirect[]; | 40 extern const char kInvalidRedirect[]; |
40 extern const char kOffTheRecord[]; | 41 extern const char kOffTheRecord[]; |
(...skipping 10 matching lines...) Expand all Loading... |
51 // | 52 // |
52 // The interactive flow presents a scope approval dialog to the | 53 // The interactive flow presents a scope approval dialog to the |
53 // user. If the user approves the request, a grant will be recorded on | 54 // user. If the user approves the request, a grant will be recorded on |
54 // the server, and an access token will be returned to the caller. | 55 // the server, and an access token will be returned to the caller. |
55 // | 56 // |
56 // In some cases we need to display a sign-in dialog. Normally the | 57 // In some cases we need to display a sign-in dialog. Normally the |
57 // profile will be signed in already, but if it turns out we need a | 58 // profile will be signed in already, but if it turns out we need a |
58 // new login token, there is a sign-in flow. If that flow completes | 59 // new login token, there is a sign-in flow. If that flow completes |
59 // successfully, getAuthToken proceeds to the non-interactive flow. | 60 // successfully, getAuthToken proceeds to the non-interactive flow. |
60 class IdentityGetAuthTokenFunction : public AsyncExtensionFunction, | 61 class IdentityGetAuthTokenFunction : public AsyncExtensionFunction, |
61 public ExtensionInstallPrompt::Delegate, | 62 public GaiaWebAuthFlow::Delegate, |
62 public IdentityMintRequestQueue::Request, | 63 public IdentityMintRequestQueue::Request, |
63 public OAuth2MintTokenFlow::Delegate, | 64 public OAuth2MintTokenFlow::Delegate, |
64 public IdentitySigninFlow::Delegate { | 65 public IdentitySigninFlow::Delegate { |
65 public: | 66 public: |
66 DECLARE_EXTENSION_FUNCTION("identity.getAuthToken", | 67 DECLARE_EXTENSION_FUNCTION("identity.getAuthToken", |
67 EXPERIMENTAL_IDENTITY_GETAUTHTOKEN); | 68 EXPERIMENTAL_IDENTITY_GETAUTHTOKEN); |
68 | 69 |
69 IdentityGetAuthTokenFunction(); | 70 IdentityGetAuthTokenFunction(); |
70 | 71 |
71 protected: | 72 protected: |
(...skipping 23 matching lines...) Expand all Loading... |
95 int time_to_live) OVERRIDE; | 96 int time_to_live) OVERRIDE; |
96 virtual void OnMintTokenFailure( | 97 virtual void OnMintTokenFailure( |
97 const GoogleServiceAuthError& error) OVERRIDE; | 98 const GoogleServiceAuthError& error) OVERRIDE; |
98 virtual void OnIssueAdviceSuccess( | 99 virtual void OnIssueAdviceSuccess( |
99 const IssueAdviceInfo& issue_advice) OVERRIDE; | 100 const IssueAdviceInfo& issue_advice) OVERRIDE; |
100 | 101 |
101 // IdentitySigninFlow::Delegate implementation: | 102 // IdentitySigninFlow::Delegate implementation: |
102 virtual void SigninSuccess(const std::string& token) OVERRIDE; | 103 virtual void SigninSuccess(const std::string& token) OVERRIDE; |
103 virtual void SigninFailed() OVERRIDE; | 104 virtual void SigninFailed() OVERRIDE; |
104 | 105 |
105 // ExtensionInstallPrompt::Delegate implementation: | 106 // GaiaWebAuthFlow::Delegate implementation: |
106 virtual void InstallUIProceed() OVERRIDE; | 107 virtual void OnGaiaFlowFailure(GaiaWebAuthFlow::Failure failure, |
107 virtual void InstallUIAbort(bool user_initiated) OVERRIDE; | 108 GoogleServiceAuthError service_error, |
| 109 const std::string& oauth_error) OVERRIDE; |
| 110 virtual void OnGaiaFlowCompleted(const std::string& access_token, |
| 111 const std::string& expiration) OVERRIDE; |
108 | 112 |
109 // Starts a mint token request to GAIA. | 113 // Starts a mint token request to GAIA. |
110 void StartGaiaRequest(OAuth2MintTokenFlow::Mode mode); | 114 void StartGaiaRequest(OAuth2MintTokenFlow::Mode mode); |
111 | 115 |
112 // Methods for invoking UI. Overridable for testing. | 116 // Methods for invoking UI. Overridable for testing. |
113 virtual void ShowLoginPopup(); | 117 virtual void ShowLoginPopup(); |
114 virtual void ShowOAuthApprovalDialog(const IssueAdviceInfo& issue_advice); | 118 virtual void ShowOAuthApprovalDialog(const IssueAdviceInfo& issue_advice); |
115 // Caller owns the returned instance. | 119 // Caller owns the returned instance. |
116 virtual OAuth2MintTokenFlow* CreateMintTokenFlow( | 120 virtual OAuth2MintTokenFlow* CreateMintTokenFlow( |
117 OAuth2MintTokenFlow::Mode mode); | 121 OAuth2MintTokenFlow::Mode mode); |
118 | 122 |
119 // Checks if there is a master login token to mint tokens for the extension. | 123 // Checks if there is a master login token to mint tokens for the extension. |
120 virtual bool HasLoginToken() const; | 124 virtual bool HasLoginToken() const; |
121 | 125 |
| 126 // Maps OAuth2 protocol errors to an error message returned to the |
| 127 // developer in chrome.runtime.lastError. |
| 128 std::string MapOAuth2ErrorToDescription(const std::string& error); |
| 129 |
122 bool should_prompt_for_scopes_; | 130 bool should_prompt_for_scopes_; |
123 IdentityMintRequestQueue::MintType mint_token_flow_type_; | 131 IdentityMintRequestQueue::MintType mint_token_flow_type_; |
124 scoped_ptr<OAuth2MintTokenFlow> mint_token_flow_; | 132 scoped_ptr<OAuth2MintTokenFlow> mint_token_flow_; |
125 std::string refresh_token_; | 133 std::string refresh_token_; |
126 bool should_prompt_for_signin_; | 134 bool should_prompt_for_signin_; |
127 | 135 |
128 // When launched in interactive mode, and if there is no existing grant, | 136 // When launched in interactive mode, and if there is no existing grant, |
129 // a permissions prompt will be popped up to the user. | 137 // a permissions prompt will be popped up to the user. |
130 IssueAdviceInfo issue_advice_; | 138 IssueAdviceInfo issue_advice_; |
131 scoped_ptr<ExtensionInstallPrompt> install_ui_; | 139 scoped_ptr<GaiaWebAuthFlow> gaia_web_auth_flow_; |
132 scoped_ptr<IdentitySigninFlow> signin_flow_; | 140 scoped_ptr<IdentitySigninFlow> signin_flow_; |
133 }; | 141 }; |
134 | 142 |
135 class IdentityRemoveCachedAuthTokenFunction : public SyncExtensionFunction { | 143 class IdentityRemoveCachedAuthTokenFunction : public SyncExtensionFunction { |
136 public: | 144 public: |
137 DECLARE_EXTENSION_FUNCTION("identity.removeCachedAuthToken", | 145 DECLARE_EXTENSION_FUNCTION("identity.removeCachedAuthToken", |
138 EXPERIMENTAL_IDENTITY_REMOVECACHEDAUTHTOKEN) | 146 EXPERIMENTAL_IDENTITY_REMOVECACHEDAUTHTOKEN) |
139 IdentityRemoveCachedAuthTokenFunction(); | 147 IdentityRemoveCachedAuthTokenFunction(); |
140 | 148 |
141 protected: | 149 protected: |
(...skipping 14 matching lines...) Expand all Loading... |
156 // Tests may override extension_id. | 164 // Tests may override extension_id. |
157 void InitFinalRedirectURLPrefixForTest(const std::string& extension_id); | 165 void InitFinalRedirectURLPrefixForTest(const std::string& extension_id); |
158 | 166 |
159 private: | 167 private: |
160 virtual ~IdentityLaunchWebAuthFlowFunction(); | 168 virtual ~IdentityLaunchWebAuthFlowFunction(); |
161 virtual bool RunImpl() OVERRIDE; | 169 virtual bool RunImpl() OVERRIDE; |
162 | 170 |
163 // WebAuthFlow::Delegate implementation. | 171 // WebAuthFlow::Delegate implementation. |
164 virtual void OnAuthFlowFailure(WebAuthFlow::Failure failure) OVERRIDE; | 172 virtual void OnAuthFlowFailure(WebAuthFlow::Failure failure) OVERRIDE; |
165 virtual void OnAuthFlowURLChange(const GURL& redirect_url) OVERRIDE; | 173 virtual void OnAuthFlowURLChange(const GURL& redirect_url) OVERRIDE; |
| 174 virtual void OnAuthFlowTitleChange(const std::string& title) OVERRIDE {} |
166 | 175 |
167 // Helper to initialize final URL prefix. | 176 // Helper to initialize final URL prefix. |
168 void InitFinalRedirectURLPrefix(const std::string& extension_id); | 177 void InitFinalRedirectURLPrefix(const std::string& extension_id); |
169 | 178 |
170 scoped_ptr<WebAuthFlow> auth_flow_; | 179 scoped_ptr<WebAuthFlow> auth_flow_; |
171 GURL final_url_prefix_; | 180 GURL final_url_prefix_; |
172 }; | 181 }; |
173 | 182 |
174 class IdentityTokenCacheValue { | 183 class IdentityTokenCacheValue { |
175 public: | 184 public: |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 IdentityMintRequestQueue mint_queue_; | 270 IdentityMintRequestQueue mint_queue_; |
262 std::map<TokenCacheKey, IdentityTokenCacheValue> token_cache_; | 271 std::map<TokenCacheKey, IdentityTokenCacheValue> token_cache_; |
263 }; | 272 }; |
264 | 273 |
265 template <> | 274 template <> |
266 void ProfileKeyedAPIFactory<IdentityAPI>::DeclareFactoryDependencies(); | 275 void ProfileKeyedAPIFactory<IdentityAPI>::DeclareFactoryDependencies(); |
267 | 276 |
268 } // namespace extensions | 277 } // namespace extensions |
269 | 278 |
270 #endif // CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_ | 279 #endif // CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_ |
OLD | NEW |