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 <string> | 9 #include <string> |
9 #include <vector> | 10 #include <vector> |
10 | 11 |
11 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
12 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "chrome/browser/extensions/api/identity/identity_mint_queue.h" |
13 #include "chrome/browser/extensions/api/identity/identity_signin_flow.h" | 15 #include "chrome/browser/extensions/api/identity/identity_signin_flow.h" |
14 #include "chrome/browser/extensions/api/identity/web_auth_flow.h" | 16 #include "chrome/browser/extensions/api/identity/web_auth_flow.h" |
15 #include "chrome/browser/extensions/api/profile_keyed_api_factory.h" | 17 #include "chrome/browser/extensions/api/profile_keyed_api_factory.h" |
16 #include "chrome/browser/extensions/extension_function.h" | 18 #include "chrome/browser/extensions/extension_function.h" |
17 #include "chrome/browser/extensions/extension_install_prompt.h" | 19 #include "chrome/browser/extensions/extension_install_prompt.h" |
18 #include "chrome/browser/signin/signin_global_error.h" | 20 #include "chrome/browser/signin/signin_global_error.h" |
19 #include "google_apis/gaia/oauth2_mint_token_flow.h" | 21 #include "google_apis/gaia/oauth2_mint_token_flow.h" |
20 | 22 |
21 class GetAuthTokenFunctionTest; | 23 class GetAuthTokenFunctionTest; |
22 class MockGetAuthTokenFunction; | 24 class MockGetAuthTokenFunction; |
23 class GoogleServiceAuthError; | 25 class GoogleServiceAuthError; |
24 class Profile; | 26 class Profile; |
25 | 27 |
26 namespace extensions { | 28 namespace extensions { |
27 | 29 |
28 namespace identity_constants { | 30 namespace identity_constants { |
29 extern const char kInvalidClientId[]; | 31 extern const char kInvalidClientId[]; |
30 extern const char kInvalidScopes[]; | 32 extern const char kInvalidScopes[]; |
31 extern const char kAuthFailure[]; | 33 extern const char kAuthFailure[]; |
32 extern const char kNoGrant[]; | 34 extern const char kNoGrant[]; |
33 extern const char kUserRejected[]; | 35 extern const char kUserRejected[]; |
34 extern const char kUserNotSignedIn[]; | 36 extern const char kUserNotSignedIn[]; |
35 extern const char kInteractionRequired[]; | 37 extern const char kInteractionRequired[]; |
36 extern const char kInvalidRedirect[]; | 38 extern const char kInvalidRedirect[]; |
37 } // namespace identity_constants | 39 } // namespace identity_constants |
38 | 40 |
| 41 // identity.getAuthToken fetches an OAuth 2 function for the |
| 42 // caller. The request has three sub-flows: non-interactive, |
| 43 // interactive, and sign-in. |
| 44 // |
| 45 // In the non-interactive flow, getAuthToken requests a token from |
| 46 // GAIA. GAIA may respond with a token, an error, or "consent |
| 47 // required". In the consent required cases, getAuthToken proceeds to |
| 48 // the second, interactive phase. |
| 49 // |
| 50 // The interactive flow presents a scope approval dialog to the |
| 51 // user. If the user approves the request, a grant will be recorded on |
| 52 // the server, and an access token will be returned to the caller. |
| 53 // |
| 54 // In some cases we need to display a sign-in dialog. Normally the |
| 55 // profile will be signed in already, but if it turns out we need a |
| 56 // new login token, there is a sign-in flow. If that flow completes |
| 57 // successfully, getAuthToken proceeds to the non-interactive flow. |
39 class IdentityGetAuthTokenFunction : public AsyncExtensionFunction, | 58 class IdentityGetAuthTokenFunction : public AsyncExtensionFunction, |
| 59 public ExtensionInstallPrompt::Delegate, |
| 60 public IdentityMintRequestQueue::Request, |
40 public OAuth2MintTokenFlow::Delegate, | 61 public OAuth2MintTokenFlow::Delegate, |
41 public ExtensionInstallPrompt::Delegate, | |
42 public IdentitySigninFlow::Delegate { | 62 public IdentitySigninFlow::Delegate { |
43 public: | 63 public: |
44 DECLARE_EXTENSION_FUNCTION("experimental.identity.getAuthToken", | 64 DECLARE_EXTENSION_FUNCTION("experimental.identity.getAuthToken", |
45 EXPERIMENTAL_IDENTITY_GETAUTHTOKEN) | 65 EXPERIMENTAL_IDENTITY_GETAUTHTOKEN) |
46 | 66 |
47 IdentityGetAuthTokenFunction(); | 67 IdentityGetAuthTokenFunction(); |
48 | 68 |
49 protected: | 69 protected: |
50 virtual ~IdentityGetAuthTokenFunction(); | 70 virtual ~IdentityGetAuthTokenFunction(); |
51 | 71 |
52 private: | 72 private: |
53 friend class GetAuthTokenFunctionTest; | 73 friend class GetAuthTokenFunctionTest; |
54 friend class MockGetAuthTokenFunction; | 74 friend class MockGetAuthTokenFunction; |
55 | 75 |
56 // ExtensionFunction: | 76 // ExtensionFunction: |
57 virtual bool RunImpl() OVERRIDE; | 77 virtual bool RunImpl() OVERRIDE; |
58 | 78 |
| 79 // Helpers to report async function results to the caller. |
| 80 void CompleteFunctionWithResult(const std::string& access_token); |
| 81 void CompleteFunctionWithError(const std::string& error); |
| 82 |
| 83 // Initiate/complete the sub-flows. |
| 84 void StartSigninFlow(); |
| 85 void StartMintTokenFlow(IdentityMintRequestQueue::MintType type); |
| 86 void CompleteMintTokenFlow(); |
| 87 |
| 88 // IdentityMintRequestQueue::Request implementation: |
| 89 virtual void StartMintToken(IdentityMintRequestQueue::MintType type) OVERRIDE; |
| 90 |
59 // OAuth2MintTokenFlow::Delegate implementation: | 91 // OAuth2MintTokenFlow::Delegate implementation: |
60 virtual void OnMintTokenSuccess(const std::string& access_token) OVERRIDE; | 92 virtual void OnMintTokenSuccess(const std::string& access_token) OVERRIDE; |
61 virtual void OnMintTokenFailure( | 93 virtual void OnMintTokenFailure( |
62 const GoogleServiceAuthError& error) OVERRIDE; | 94 const GoogleServiceAuthError& error) OVERRIDE; |
63 virtual void OnIssueAdviceSuccess( | 95 virtual void OnIssueAdviceSuccess( |
64 const IssueAdviceInfo& issue_advice) OVERRIDE; | 96 const IssueAdviceInfo& issue_advice) OVERRIDE; |
65 | 97 |
66 // IdentitySigninFlow::Delegate implementation: | 98 // IdentitySigninFlow::Delegate implementation: |
67 virtual void SigninSuccess(const std::string& token) OVERRIDE; | 99 virtual void SigninSuccess(const std::string& token) OVERRIDE; |
68 virtual void SigninFailed() OVERRIDE; | 100 virtual void SigninFailed() OVERRIDE; |
69 | 101 |
70 // ExtensionInstallPrompt::Delegate implementation: | 102 // ExtensionInstallPrompt::Delegate implementation: |
71 virtual void InstallUIProceed() OVERRIDE; | 103 virtual void InstallUIProceed() OVERRIDE; |
72 virtual void InstallUIAbort(bool user_initiated) OVERRIDE; | 104 virtual void InstallUIAbort(bool user_initiated) OVERRIDE; |
73 | 105 |
74 // Starts a MintTokenFlow with the given mode. | 106 // Starts a mint token request to GAIA. |
75 void StartFlow(OAuth2MintTokenFlow::Mode mode); | 107 void StartGaiaRequest(OAuth2MintTokenFlow::Mode mode); |
76 | 108 |
| 109 // Methods for invoking UI. Overridable for testing. |
77 virtual void ShowLoginPopup(); | 110 virtual void ShowLoginPopup(); |
78 virtual void ShowOAuthApprovalDialog(const IssueAdviceInfo& issue_advice); | 111 virtual void ShowOAuthApprovalDialog(const IssueAdviceInfo& issue_advice); |
79 // Caller owns the returned instance. | 112 // Caller owns the returned instance. |
80 virtual OAuth2MintTokenFlow* CreateMintTokenFlow( | 113 virtual OAuth2MintTokenFlow* CreateMintTokenFlow( |
81 OAuth2MintTokenFlow::Mode mode); | 114 OAuth2MintTokenFlow::Mode mode); |
82 | 115 |
83 // Checks if there is a master login token to mint tokens for the extension. | 116 // Checks if there is a master login token to mint tokens for the extension. |
84 virtual bool HasLoginToken() const; | 117 virtual bool HasLoginToken() const; |
85 | 118 |
86 bool should_prompt_for_scopes_; | 119 bool should_prompt_for_scopes_; |
| 120 IdentityMintRequestQueue::MintType mint_token_flow_type_; |
87 scoped_ptr<OAuth2MintTokenFlow> mint_token_flow_; | 121 scoped_ptr<OAuth2MintTokenFlow> mint_token_flow_; |
88 std::string refresh_token_; | 122 std::string refresh_token_; |
89 bool should_prompt_for_signin_; | 123 bool should_prompt_for_signin_; |
90 | 124 |
91 // When launched in interactive mode, and if there is no existing grant, | 125 // When launched in interactive mode, and if there is no existing grant, |
92 // a permissions prompt will be popped up to the user. | 126 // a permissions prompt will be popped up to the user. |
| 127 IssueAdviceInfo issue_advice_; |
93 scoped_ptr<ExtensionInstallPrompt> install_ui_; | 128 scoped_ptr<ExtensionInstallPrompt> install_ui_; |
94 scoped_ptr<IdentitySigninFlow> signin_flow_; | 129 scoped_ptr<IdentitySigninFlow> signin_flow_; |
95 }; | 130 }; |
96 | 131 |
97 class IdentityLaunchWebAuthFlowFunction : public AsyncExtensionFunction, | 132 class IdentityLaunchWebAuthFlowFunction : public AsyncExtensionFunction, |
98 public WebAuthFlow::Delegate { | 133 public WebAuthFlow::Delegate { |
99 public: | 134 public: |
100 DECLARE_EXTENSION_FUNCTION("experimental.identity.launchWebAuthFlow", | 135 DECLARE_EXTENSION_FUNCTION("experimental.identity.launchWebAuthFlow", |
101 EXPERIMENTAL_IDENTITY_LAUNCHWEBAUTHFLOW) | 136 EXPERIMENTAL_IDENTITY_LAUNCHWEBAUTHFLOW) |
102 | 137 |
(...skipping 22 matching lines...) Expand all Loading... |
125 }; | 160 }; |
126 | 161 |
127 class IdentityAPI : public ProfileKeyedAPI, | 162 class IdentityAPI : public ProfileKeyedAPI, |
128 public SigninGlobalError::AuthStatusProvider, | 163 public SigninGlobalError::AuthStatusProvider, |
129 public content::NotificationObserver { | 164 public content::NotificationObserver { |
130 public: | 165 public: |
131 explicit IdentityAPI(Profile* profile); | 166 explicit IdentityAPI(Profile* profile); |
132 virtual ~IdentityAPI(); | 167 virtual ~IdentityAPI(); |
133 void Initialize(); | 168 void Initialize(); |
134 | 169 |
| 170 // Request serialization queue for getAuthToken. |
| 171 IdentityMintRequestQueue* mint_queue(); |
| 172 |
135 void ReportAuthError(const GoogleServiceAuthError& error); | 173 void ReportAuthError(const GoogleServiceAuthError& error); |
136 | 174 |
137 // ProfileKeyedAPI implementation. | 175 // ProfileKeyedAPI implementation. |
138 virtual void Shutdown() OVERRIDE; | 176 virtual void Shutdown() OVERRIDE; |
139 static ProfileKeyedAPIFactory<IdentityAPI>* GetFactoryInstance(); | 177 static ProfileKeyedAPIFactory<IdentityAPI>* GetFactoryInstance(); |
140 | 178 |
141 // AuthStatusProvider implementation. | 179 // AuthStatusProvider implementation. |
142 virtual GoogleServiceAuthError GetAuthStatus() const OVERRIDE; | 180 virtual GoogleServiceAuthError GetAuthStatus() const OVERRIDE; |
143 | 181 |
144 // content::NotificationObserver implementation. | 182 // content::NotificationObserver implementation. |
145 virtual void Observe(int type, | 183 virtual void Observe(int type, |
146 const content::NotificationSource& source, | 184 const content::NotificationSource& source, |
147 const content::NotificationDetails& details) OVERRIDE; | 185 const content::NotificationDetails& details) OVERRIDE; |
148 | 186 |
149 private: | 187 private: |
150 friend class ProfileKeyedAPIFactory<IdentityAPI>; | 188 friend class ProfileKeyedAPIFactory<IdentityAPI>; |
151 | 189 |
152 // ProfileKeyedAPI implementation. | 190 // ProfileKeyedAPI implementation. |
153 static const char* service_name() { | 191 static const char* service_name() { |
154 return "IdentityAPI"; | 192 return "IdentityAPI"; |
155 } | 193 } |
156 static const bool kServiceIsNULLWhileTesting = true; | 194 static const bool kServiceIsNULLWhileTesting = true; |
157 | 195 |
158 Profile* profile_; | 196 Profile* profile_; |
159 SigninManager* signin_manager_; | 197 SigninManager* signin_manager_; |
160 GoogleServiceAuthError error_; | 198 GoogleServiceAuthError error_; |
161 // Used to listen to notifications from the TokenService. | 199 // Used to listen to notifications from the TokenService. |
162 content::NotificationRegistrar registrar_; | 200 content::NotificationRegistrar registrar_; |
| 201 IdentityMintRequestQueue mint_queue_; |
163 }; | 202 }; |
164 | 203 |
165 template <> | 204 template <> |
166 void ProfileKeyedAPIFactory<IdentityAPI>::DeclareFactoryDependencies(); | 205 void ProfileKeyedAPIFactory<IdentityAPI>::DeclareFactoryDependencies(); |
167 | 206 |
168 } // namespace extensions | 207 } // namespace extensions |
169 | 208 |
170 #endif // CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_ | 209 #endif // CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_ |
OLD | NEW |