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 #include "chrome/browser/extensions/api/identity/identity_api.h" | 5 #include "chrome/browser/extensions/api/identity/identity_api.h" |
6 | 6 |
7 #include "base/values.h" | 7 #include "base/values.h" |
8 #include "chrome/browser/extensions/extension_install_prompt.h" | 8 #include "chrome/browser/extensions/extension_install_prompt.h" |
9 #include "chrome/browser/extensions/extension_function_dispatcher.h" | 9 #include "chrome/browser/extensions/extension_function_dispatcher.h" |
10 #include "chrome/browser/extensions/extension_service.h" | 10 #include "chrome/browser/extensions/extension_service.h" |
11 #include "chrome/browser/extensions/permissions_updater.h" | 11 #include "chrome/browser/extensions/permissions_updater.h" |
12 #include "chrome/browser/signin/token_service.h" | 12 #include "chrome/browser/signin/token_service.h" |
13 #include "chrome/browser/signin/token_service_factory.h" | 13 #include "chrome/browser/signin/token_service_factory.h" |
14 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
15 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 15 #include "chrome/browser/ui/browser_navigator.h" |
16 #include "chrome/browser/ui/webui/signin/login_ui_service.h" | |
17 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h" | |
16 #include "chrome/common/extensions/extension.h" | 18 #include "chrome/common/extensions/extension.h" |
19 #include "chrome/common/url_constants.h" | |
20 #include "content/public/common/page_transition_types.h" | |
17 #include "googleurl/src/gurl.h" | 21 #include "googleurl/src/gurl.h" |
22 #include "webkit/glue/window_open_disposition.h" | |
18 | 23 |
19 namespace extensions { | 24 namespace extensions { |
20 | 25 |
21 namespace { | 26 namespace { |
22 | 27 |
23 const char kInvalidClientId[] = "Invalid OAuth2 Client ID."; | 28 const char kInvalidClientId[] = "Invalid OAuth2 Client ID."; |
24 const char kInvalidScopes[] = "Invalid OAuth2 scopes."; | 29 const char kInvalidScopes[] = "Invalid OAuth2 scopes."; |
25 const char kInvalidRedirect[] = "Did not redirect to the right URL."; | 30 const char kInvalidRedirect[] = "Did not redirect to the right URL."; |
26 const char kAuthFailure[] = "OAuth2 request failed: "; | 31 const char kAuthFailure[] = "OAuth2 request failed: "; |
27 const char kNoGrant[] = "OAuth2 not granted or revoked."; | 32 const char kNoGrant[] = "OAuth2 not granted or revoked."; |
28 const char kUserRejected[] = "The user did not approve access."; | 33 const char kUserRejected[] = "The user did not approve access."; |
34 const char kUserNotSignedIn[] = "The user is not signed in."; | |
29 | 35 |
30 } // namespace | 36 } // namespace |
31 | 37 |
32 GetAuthTokenFunction::GetAuthTokenFunction() : interactive_(false) {} | 38 GetAuthTokenFunction::GetAuthTokenFunction() : interactive_(false) {} |
33 GetAuthTokenFunction::~GetAuthTokenFunction() {} | 39 GetAuthTokenFunction::~GetAuthTokenFunction() {} |
34 | 40 |
35 bool GetAuthTokenFunction::RunImpl() { | 41 bool GetAuthTokenFunction::RunImpl() { |
36 DictionaryValue* arg = NULL; | 42 DictionaryValue* arg = NULL; |
37 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &arg)); | 43 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &arg)); |
38 arg->GetBoolean("interactive", &interactive_); | 44 arg->GetBoolean("interactive", &interactive_); |
39 | 45 |
40 // Balanced in OnIssueAdviceSuccess|OnMintTokenSuccess|OnMintTokenFailure| | 46 // Balanced in OnIssueAdviceSuccess|OnMintTokenSuccess|OnMintTokenFailure| |
41 // InstallUIAbort. | 47 // InstallUIAbort|OnLoginUIClosed. |
42 AddRef(); | 48 AddRef(); |
43 | 49 |
44 if (StartFlow(ExtensionInstallPrompt::ShouldAutomaticallyApproveScopes() ? | 50 if (!HasLoginToken()) { |
45 OAuth2MintTokenFlow::MODE_MINT_TOKEN_FORCE : | 51 if (StartLogin()) { |
46 OAuth2MintTokenFlow::MODE_MINT_TOKEN_NO_FORCE)) { | 52 return true; |
53 } else { | |
54 Release(); | |
55 return false; | |
56 } | |
57 } | |
58 | |
59 if (StartFlow(GetTokenFlowMode())) { | |
Mihai Parparita -not on Chrome
2012/07/12 00:39:21
Rather than having to always call StartToken with
Munjal (Google)
2012/07/12 18:41:56
Actually ,that is what I first tried until I reali
Mihai Parparita -not on Chrome
2012/07/12 23:31:32
Ah, I'd missed the StartFlow(OAuth2MintTokenFlow::
| |
47 return true; | 60 return true; |
48 } else { | 61 } else { |
49 Release(); | 62 Release(); |
50 return false; | 63 return false; |
51 } | 64 } |
52 } | 65 } |
53 | 66 |
54 void GetAuthTokenFunction::OnMintTokenSuccess(const std::string& access_token) { | 67 void GetAuthTokenFunction::OnMintTokenSuccess(const std::string& access_token) { |
55 result_.reset(Value::CreateStringValue(access_token)); | 68 result_.reset(Value::CreateStringValue(access_token)); |
56 SendResponse(true); | 69 SendResponse(true); |
(...skipping 15 matching lines...) Expand all Loading... | |
72 install_ui_.reset( | 85 install_ui_.reset( |
73 chrome::CreateExtensionInstallPromptWithBrowser(GetCurrentBrowser())); | 86 chrome::CreateExtensionInstallPromptWithBrowser(GetCurrentBrowser())); |
74 install_ui_->ConfirmIssueAdvice(this, GetExtension(), issue_advice); | 87 install_ui_->ConfirmIssueAdvice(this, GetExtension(), issue_advice); |
75 } else { | 88 } else { |
76 error_ = kNoGrant; | 89 error_ = kNoGrant; |
77 SendResponse(false); | 90 SendResponse(false); |
78 Release(); // Balanced in RunImpl. | 91 Release(); // Balanced in RunImpl. |
79 } | 92 } |
80 } | 93 } |
81 | 94 |
95 void GetAuthTokenFunction::OnLoginUIShown(LoginUIService::LoginUI* ui) { | |
96 // Do nothing when login ui is shown. | |
97 } | |
98 | |
99 void GetAuthTokenFunction::OnLoginUIClosed(LoginUIService::LoginUI* ui) { | |
100 LoginUIService* login_ui_service = | |
101 LoginUIServiceFactory::GetForProfile(profile()); | |
102 login_ui_service->RemoveObserver(this); | |
103 if (!StartFlow(GetTokenFlowMode())) { | |
104 SendResponse(false); | |
105 Release(); | |
106 } | |
107 } | |
108 | |
82 void GetAuthTokenFunction::InstallUIProceed() { | 109 void GetAuthTokenFunction::InstallUIProceed() { |
83 DCHECK(install_ui_->record_oauth2_grant()); | 110 DCHECK(install_ui_->record_oauth2_grant()); |
84 // The user has accepted the scopes, so we may now force (recording a grant | 111 // The user has accepted the scopes, so we may now force (recording a grant |
85 // and receiving a token). | 112 // and receiving a token). |
86 bool success = StartFlow(OAuth2MintTokenFlow::MODE_MINT_TOKEN_FORCE); | 113 bool success = StartFlow(OAuth2MintTokenFlow::MODE_MINT_TOKEN_FORCE); |
87 DCHECK(success); | 114 DCHECK(success); |
88 } | 115 } |
89 | 116 |
90 void GetAuthTokenFunction::InstallUIAbort(bool user_initiated) { | 117 void GetAuthTokenFunction::InstallUIAbort(bool user_initiated) { |
91 error_ = kUserRejected; | 118 error_ = kUserRejected; |
92 SendResponse(false); | 119 SendResponse(false); |
93 Release(); // Balanced in RunImpl. | 120 Release(); // Balanced in RunImpl. |
94 } | 121 } |
95 | 122 |
96 bool GetAuthTokenFunction::StartFlow(OAuth2MintTokenFlow::Mode mode) { | 123 bool GetAuthTokenFunction::StartFlow(OAuth2MintTokenFlow::Mode mode) { |
97 const Extension* extension = GetExtension(); | 124 const Extension* extension = GetExtension(); |
98 Extension::OAuth2Info oauth2_info = extension->oauth2_info(); | 125 Extension::OAuth2Info oauth2_info = extension->oauth2_info(); |
99 | 126 |
100 if (oauth2_info.client_id.empty()) { | 127 if (oauth2_info.client_id.empty()) { |
Mihai Parparita -not on Chrome
2012/07/12 00:39:21
Seems like this check (and the scope size one) cou
Munjal (Google)
2012/07/12 18:41:56
Done.
| |
101 error_ = kInvalidClientId; | 128 error_ = kInvalidClientId; |
102 return false; | 129 return false; |
103 } | 130 } |
104 | 131 |
105 if (oauth2_info.scopes.size() == 0) { | 132 if (oauth2_info.scopes.size() == 0) { |
106 error_ = kInvalidScopes; | 133 error_ = kInvalidScopes; |
107 return false; | 134 return false; |
108 } | 135 } |
109 | 136 |
137 if (!HasLoginToken()) { | |
138 error_ = kUserNotSignedIn; | |
139 return false; | |
140 } | |
141 | |
110 TokenService* token_service = TokenServiceFactory::GetForProfile(profile()); | 142 TokenService* token_service = TokenServiceFactory::GetForProfile(profile()); |
111 flow_.reset(new OAuth2MintTokenFlow( | 143 flow_.reset(new OAuth2MintTokenFlow( |
112 profile()->GetRequestContext(), | 144 profile()->GetRequestContext(), |
113 this, | 145 this, |
114 OAuth2MintTokenFlow::Parameters( | 146 OAuth2MintTokenFlow::Parameters( |
115 token_service->GetOAuth2LoginRefreshToken(), | 147 token_service->GetOAuth2LoginRefreshToken(), |
116 extension->id(), | 148 extension->id(), |
117 oauth2_info.client_id, | 149 oauth2_info.client_id, |
118 oauth2_info.scopes, | 150 oauth2_info.scopes, |
119 mode))); | 151 mode))); |
120 flow_->Start(); | 152 flow_->Start(); |
121 return true; | 153 return true; |
122 } | 154 } |
123 | 155 |
156 bool GetAuthTokenFunction::StartLogin() { | |
157 if (!interactive_) { | |
158 error_ = kUserNotSignedIn; | |
159 return false; | |
160 } | |
161 | |
162 LoginUIService* login_ui_service = | |
163 LoginUIServiceFactory::GetForProfile(profile()); | |
164 login_ui_service->AddObserver(this); | |
165 | |
166 LoginUIService::LoginUI* login_ui = login_ui_service->current_login_ui(); | |
167 if (login_ui) { | |
168 // Focus existing UI. | |
Mihai Parparita -not on Chrome
2012/07/12 00:39:21
Nit: This comment (and the one a couple of lines b
Munjal (Google)
2012/07/12 18:41:56
Done.
| |
169 login_ui->FocusUI(); | |
170 } else { | |
171 // Open login UI in a new popup. | |
172 chrome::NavigateParams params(NULL, | |
173 GURL(chrome::kChromeUISyncPromoURL), | |
Mihai Parparita -not on Chrome
2012/07/12 00:39:21
Can knowledge of this page and its URL be encapsul
Munjal (Google)
2012/07/12 18:41:56
Actually I had it exactly that way. But in talking
Mihai Parparita -not on Chrome
2012/07/12 23:31:32
OK
| |
174 content::PAGE_TRANSITION_START_PAGE); | |
175 params.profile = profile(); | |
176 params.disposition = NEW_POPUP; | |
177 chrome::Navigate(¶ms); | |
178 } | |
179 | |
180 return true; | |
181 } | |
182 | |
183 bool GetAuthTokenFunction::HasLoginToken() const { | |
184 TokenService* token_service = TokenServiceFactory::GetForProfile(profile()); | |
185 return token_service->HasOAuthLoginToken(); | |
186 } | |
187 | |
188 OAuth2MintTokenFlow::Mode GetAuthTokenFunction::GetTokenFlowMode() const { | |
189 return ExtensionInstallPrompt::ShouldAutomaticallyApproveScopes() ? | |
190 OAuth2MintTokenFlow::MODE_MINT_TOKEN_FORCE : | |
191 OAuth2MintTokenFlow::MODE_MINT_TOKEN_NO_FORCE; | |
192 } | |
193 | |
124 LaunchWebAuthFlowFunction::LaunchWebAuthFlowFunction() {} | 194 LaunchWebAuthFlowFunction::LaunchWebAuthFlowFunction() {} |
125 LaunchWebAuthFlowFunction::~LaunchWebAuthFlowFunction() {} | 195 LaunchWebAuthFlowFunction::~LaunchWebAuthFlowFunction() {} |
126 | 196 |
127 bool LaunchWebAuthFlowFunction::RunImpl() { | 197 bool LaunchWebAuthFlowFunction::RunImpl() { |
128 DictionaryValue* arg = NULL; | 198 DictionaryValue* arg = NULL; |
129 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &arg)); | 199 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &arg)); |
130 | 200 |
131 std::string url; | 201 std::string url; |
132 EXTENSION_FUNCTION_VALIDATE(arg->GetString("url", &url)); | 202 EXTENSION_FUNCTION_VALIDATE(arg->GetString("url", &url)); |
133 | 203 |
(...skipping 18 matching lines...) Expand all Loading... | |
152 Release(); // Balanced in RunImpl. | 222 Release(); // Balanced in RunImpl. |
153 } | 223 } |
154 | 224 |
155 void LaunchWebAuthFlowFunction::OnAuthFlowFailure() { | 225 void LaunchWebAuthFlowFunction::OnAuthFlowFailure() { |
156 error_ = kInvalidRedirect; | 226 error_ = kInvalidRedirect; |
157 SendResponse(false); | 227 SendResponse(false); |
158 Release(); // Balanced in RunImpl. | 228 Release(); // Balanced in RunImpl. |
159 } | 229 } |
160 | 230 |
161 } // namespace extensions | 231 } // namespace extensions |
OLD | NEW |