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; |
78 void CompleteFunctionWithResult(const std::string& access_token); | |
miket_OOO
2013/04/16 20:42:56
Should these two have OVERRIDE? If not, they don't
Michael Courage
2013/04/16 22:04:23
Done.
| |
79 void CompleteFunctionWithError(const std::string& error); | |
80 | |
81 // Initiate/complete the sub-flows. | |
82 void StartSigninFlow(); | |
83 void StartMintTokenFlow(IdentityMintRequestQueue::MintType type); | |
84 void CompleteMintTokenFlow(); | |
85 | |
86 // IdentityMintRequestQueue::Request implementation: | |
87 virtual void StartMintToken(IdentityMintRequestQueue::MintType type) OVERRIDE; | |
58 | 88 |
59 // OAuth2MintTokenFlow::Delegate implementation: | 89 // OAuth2MintTokenFlow::Delegate implementation: |
60 virtual void OnMintTokenSuccess(const std::string& access_token) OVERRIDE; | 90 virtual void OnMintTokenSuccess(const std::string& access_token) OVERRIDE; |
61 virtual void OnMintTokenFailure( | 91 virtual void OnMintTokenFailure( |
62 const GoogleServiceAuthError& error) OVERRIDE; | 92 const GoogleServiceAuthError& error) OVERRIDE; |
63 virtual void OnIssueAdviceSuccess( | 93 virtual void OnIssueAdviceSuccess( |
64 const IssueAdviceInfo& issue_advice) OVERRIDE; | 94 const IssueAdviceInfo& issue_advice) OVERRIDE; |
65 | 95 |
66 // IdentitySigninFlow::Delegate implementation: | 96 // IdentitySigninFlow::Delegate implementation: |
67 virtual void SigninSuccess(const std::string& token) OVERRIDE; | 97 virtual void SigninSuccess(const std::string& token) OVERRIDE; |
68 virtual void SigninFailed() OVERRIDE; | 98 virtual void SigninFailed() OVERRIDE; |
69 | 99 |
70 // ExtensionInstallPrompt::Delegate implementation: | 100 // ExtensionInstallPrompt::Delegate implementation: |
71 virtual void InstallUIProceed() OVERRIDE; | 101 virtual void InstallUIProceed() OVERRIDE; |
72 virtual void InstallUIAbort(bool user_initiated) OVERRIDE; | 102 virtual void InstallUIAbort(bool user_initiated) OVERRIDE; |
73 | 103 |
74 // Starts a MintTokenFlow with the given mode. | 104 // Starts a mint token request to GAIA. |
75 void StartFlow(OAuth2MintTokenFlow::Mode mode); | 105 void StartGaiaRequest(OAuth2MintTokenFlow::Mode mode); |
76 | 106 |
107 // Methods for invoking UI. Overridable for testing. | |
77 virtual void ShowLoginPopup(); | 108 virtual void ShowLoginPopup(); |
78 virtual void ShowOAuthApprovalDialog(const IssueAdviceInfo& issue_advice); | 109 virtual void ShowOAuthApprovalDialog(const IssueAdviceInfo& issue_advice); |
79 // Caller owns the returned instance. | 110 // Caller owns the returned instance. |
80 virtual OAuth2MintTokenFlow* CreateMintTokenFlow( | 111 virtual OAuth2MintTokenFlow* CreateMintTokenFlow( |
81 OAuth2MintTokenFlow::Mode mode); | 112 OAuth2MintTokenFlow::Mode mode); |
82 | 113 |
83 // Checks if there is a master login token to mint tokens for the extension. | 114 // Checks if there is a master login token to mint tokens for the extension. |
84 virtual bool HasLoginToken() const; | 115 virtual bool HasLoginToken() const; |
85 | 116 |
86 bool should_prompt_for_scopes_; | 117 bool should_prompt_for_scopes_; |
118 IdentityMintRequestQueue::MintType mint_token_flow_type_; | |
87 scoped_ptr<OAuth2MintTokenFlow> mint_token_flow_; | 119 scoped_ptr<OAuth2MintTokenFlow> mint_token_flow_; |
88 std::string refresh_token_; | 120 std::string refresh_token_; |
89 bool should_prompt_for_signin_; | 121 bool should_prompt_for_signin_; |
90 | 122 |
91 // When launched in interactive mode, and if there is no existing grant, | 123 // When launched in interactive mode, and if there is no existing grant, |
92 // a permissions prompt will be popped up to the user. | 124 // a permissions prompt will be popped up to the user. |
125 IssueAdviceInfo issue_advice_; | |
93 scoped_ptr<ExtensionInstallPrompt> install_ui_; | 126 scoped_ptr<ExtensionInstallPrompt> install_ui_; |
94 scoped_ptr<IdentitySigninFlow> signin_flow_; | 127 scoped_ptr<IdentitySigninFlow> signin_flow_; |
95 }; | 128 }; |
96 | 129 |
97 class IdentityLaunchWebAuthFlowFunction : public AsyncExtensionFunction, | 130 class IdentityLaunchWebAuthFlowFunction : public AsyncExtensionFunction, |
98 public WebAuthFlow::Delegate { | 131 public WebAuthFlow::Delegate { |
99 public: | 132 public: |
100 DECLARE_EXTENSION_FUNCTION("experimental.identity.launchWebAuthFlow", | 133 DECLARE_EXTENSION_FUNCTION("experimental.identity.launchWebAuthFlow", |
101 EXPERIMENTAL_IDENTITY_LAUNCHWEBAUTHFLOW) | 134 EXPERIMENTAL_IDENTITY_LAUNCHWEBAUTHFLOW) |
102 | 135 |
(...skipping 22 matching lines...) Expand all Loading... | |
125 }; | 158 }; |
126 | 159 |
127 class IdentityAPI : public ProfileKeyedAPI, | 160 class IdentityAPI : public ProfileKeyedAPI, |
128 public SigninGlobalError::AuthStatusProvider, | 161 public SigninGlobalError::AuthStatusProvider, |
129 public content::NotificationObserver { | 162 public content::NotificationObserver { |
130 public: | 163 public: |
131 explicit IdentityAPI(Profile* profile); | 164 explicit IdentityAPI(Profile* profile); |
132 virtual ~IdentityAPI(); | 165 virtual ~IdentityAPI(); |
133 void Initialize(); | 166 void Initialize(); |
134 | 167 |
168 // Request serialization queue for getAuthToken. | |
169 IdentityMintRequestQueue* mint_queue(); | |
170 | |
135 void ReportAuthError(const GoogleServiceAuthError& error); | 171 void ReportAuthError(const GoogleServiceAuthError& error); |
136 | 172 |
137 // ProfileKeyedAPI implementation. | 173 // ProfileKeyedAPI implementation. |
138 virtual void Shutdown() OVERRIDE; | 174 virtual void Shutdown() OVERRIDE; |
139 static ProfileKeyedAPIFactory<IdentityAPI>* GetFactoryInstance(); | 175 static ProfileKeyedAPIFactory<IdentityAPI>* GetFactoryInstance(); |
140 | 176 |
141 // AuthStatusProvider implementation. | 177 // AuthStatusProvider implementation. |
142 virtual GoogleServiceAuthError GetAuthStatus() const OVERRIDE; | 178 virtual GoogleServiceAuthError GetAuthStatus() const OVERRIDE; |
143 | 179 |
144 // content::NotificationObserver implementation. | 180 // content::NotificationObserver implementation. |
145 virtual void Observe(int type, | 181 virtual void Observe(int type, |
146 const content::NotificationSource& source, | 182 const content::NotificationSource& source, |
147 const content::NotificationDetails& details) OVERRIDE; | 183 const content::NotificationDetails& details) OVERRIDE; |
148 | 184 |
149 private: | 185 private: |
150 friend class ProfileKeyedAPIFactory<IdentityAPI>; | 186 friend class ProfileKeyedAPIFactory<IdentityAPI>; |
151 | 187 |
152 // ProfileKeyedAPI implementation. | 188 // ProfileKeyedAPI implementation. |
153 static const char* service_name() { | 189 static const char* service_name() { |
154 return "IdentityAPI"; | 190 return "IdentityAPI"; |
155 } | 191 } |
156 static const bool kServiceIsNULLWhileTesting = true; | 192 static const bool kServiceIsNULLWhileTesting = true; |
157 | 193 |
158 Profile* profile_; | 194 Profile* profile_; |
159 SigninManager* signin_manager_; | 195 SigninManager* signin_manager_; |
160 GoogleServiceAuthError error_; | 196 GoogleServiceAuthError error_; |
161 // Used to listen to notifications from the TokenService. | 197 // Used to listen to notifications from the TokenService. |
162 content::NotificationRegistrar registrar_; | 198 content::NotificationRegistrar registrar_; |
199 IdentityMintRequestQueue mint_queue_; | |
163 }; | 200 }; |
164 | 201 |
165 template <> | 202 template <> |
166 void ProfileKeyedAPIFactory<IdentityAPI>::DeclareFactoryDependencies(); | 203 void ProfileKeyedAPIFactory<IdentityAPI>::DeclareFactoryDependencies(); |
167 | 204 |
168 } // namespace extensions | 205 } // namespace extensions |
169 | 206 |
170 #endif // CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_ | 207 #endif // CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_ |
OLD | NEW |