| 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 |
| 46 Extension::OAuth2Info oauth2_info = GetOAuth2Info(); |
| 47 |
| 48 // Check that the necessary information is present in the manfist. |
| 49 if (oauth2_info.client_id.empty()) { |
| 50 error_ = kInvalidClientId; |
| 51 return false; |
| 52 } |
| 53 |
| 54 if (oauth2_info.scopes.size() == 0) { |
| 55 error_ = kInvalidScopes; |
| 56 return false; |
| 57 } |
| 58 |
| 40 // Balanced in OnIssueAdviceSuccess|OnMintTokenSuccess|OnMintTokenFailure| | 59 // Balanced in OnIssueAdviceSuccess|OnMintTokenSuccess|OnMintTokenFailure| |
| 41 // InstallUIAbort. | 60 // InstallUIAbort|OnLoginUIClosed. |
| 42 AddRef(); | 61 AddRef(); |
| 43 | 62 |
| 44 if (StartFlow(ExtensionInstallPrompt::ShouldAutomaticallyApproveScopes() ? | 63 if (!HasLoginToken()) { |
| 45 OAuth2MintTokenFlow::MODE_MINT_TOKEN_FORCE : | 64 if (StartLogin()) { |
| 46 OAuth2MintTokenFlow::MODE_MINT_TOKEN_NO_FORCE)) { | 65 return true; |
| 66 } else { |
| 67 Release(); |
| 68 return false; |
| 69 } |
| 70 } |
| 71 |
| 72 if (StartFlow(GetTokenFlowMode())) { |
| 47 return true; | 73 return true; |
| 48 } else { | 74 } else { |
| 49 Release(); | 75 Release(); |
| 50 return false; | 76 return false; |
| 51 } | 77 } |
| 52 } | 78 } |
| 53 | 79 |
| 54 void GetAuthTokenFunction::OnMintTokenSuccess(const std::string& access_token) { | 80 void GetAuthTokenFunction::OnMintTokenSuccess(const std::string& access_token) { |
| 55 result_.reset(Value::CreateStringValue(access_token)); | 81 result_.reset(Value::CreateStringValue(access_token)); |
| 56 SendResponse(true); | 82 SendResponse(true); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 72 install_ui_.reset( | 98 install_ui_.reset( |
| 73 chrome::CreateExtensionInstallPromptWithBrowser(GetCurrentBrowser())); | 99 chrome::CreateExtensionInstallPromptWithBrowser(GetCurrentBrowser())); |
| 74 install_ui_->ConfirmIssueAdvice(this, GetExtension(), issue_advice); | 100 install_ui_->ConfirmIssueAdvice(this, GetExtension(), issue_advice); |
| 75 } else { | 101 } else { |
| 76 error_ = kNoGrant; | 102 error_ = kNoGrant; |
| 77 SendResponse(false); | 103 SendResponse(false); |
| 78 Release(); // Balanced in RunImpl. | 104 Release(); // Balanced in RunImpl. |
| 79 } | 105 } |
| 80 } | 106 } |
| 81 | 107 |
| 108 void GetAuthTokenFunction::OnLoginUIShown(LoginUIService::LoginUI* ui) { |
| 109 // Do nothing when login ui is shown. |
| 110 } |
| 111 |
| 112 void GetAuthTokenFunction::OnLoginUIClosed(LoginUIService::LoginUI* ui) { |
| 113 LoginUIService* login_ui_service = |
| 114 LoginUIServiceFactory::GetForProfile(profile()); |
| 115 login_ui_service->RemoveObserver(this); |
| 116 if (!StartFlow(GetTokenFlowMode())) { |
| 117 SendResponse(false); |
| 118 Release(); |
| 119 } |
| 120 } |
| 121 |
| 82 void GetAuthTokenFunction::InstallUIProceed() { | 122 void GetAuthTokenFunction::InstallUIProceed() { |
| 83 DCHECK(install_ui_->record_oauth2_grant()); | 123 DCHECK(install_ui_->record_oauth2_grant()); |
| 84 // The user has accepted the scopes, so we may now force (recording a grant | 124 // The user has accepted the scopes, so we may now force (recording a grant |
| 85 // and receiving a token). | 125 // and receiving a token). |
| 86 bool success = StartFlow(OAuth2MintTokenFlow::MODE_MINT_TOKEN_FORCE); | 126 bool success = StartFlow(OAuth2MintTokenFlow::MODE_MINT_TOKEN_FORCE); |
| 87 DCHECK(success); | 127 DCHECK(success); |
| 88 } | 128 } |
| 89 | 129 |
| 90 void GetAuthTokenFunction::InstallUIAbort(bool user_initiated) { | 130 void GetAuthTokenFunction::InstallUIAbort(bool user_initiated) { |
| 91 error_ = kUserRejected; | 131 error_ = kUserRejected; |
| 92 SendResponse(false); | 132 SendResponse(false); |
| 93 Release(); // Balanced in RunImpl. | 133 Release(); // Balanced in RunImpl. |
| 94 } | 134 } |
| 95 | 135 |
| 96 bool GetAuthTokenFunction::StartFlow(OAuth2MintTokenFlow::Mode mode) { | 136 bool GetAuthTokenFunction::StartFlow(OAuth2MintTokenFlow::Mode mode) { |
| 97 const Extension* extension = GetExtension(); | 137 const Extension* extension = GetExtension(); |
| 98 Extension::OAuth2Info oauth2_info = extension->oauth2_info(); | 138 Extension::OAuth2Info oauth2_info = GetOAuth2Info(); |
| 99 | 139 |
| 100 if (oauth2_info.client_id.empty()) { | 140 if (!HasLoginToken()) { |
| 101 error_ = kInvalidClientId; | 141 error_ = kUserNotSignedIn; |
| 102 return false; | |
| 103 } | |
| 104 | |
| 105 if (oauth2_info.scopes.size() == 0) { | |
| 106 error_ = kInvalidScopes; | |
| 107 return false; | 142 return false; |
| 108 } | 143 } |
| 109 | 144 |
| 110 TokenService* token_service = TokenServiceFactory::GetForProfile(profile()); | 145 TokenService* token_service = TokenServiceFactory::GetForProfile(profile()); |
| 111 flow_.reset(new OAuth2MintTokenFlow( | 146 flow_.reset(new OAuth2MintTokenFlow( |
| 112 profile()->GetRequestContext(), | 147 profile()->GetRequestContext(), |
| 113 this, | 148 this, |
| 114 OAuth2MintTokenFlow::Parameters( | 149 OAuth2MintTokenFlow::Parameters( |
| 115 token_service->GetOAuth2LoginRefreshToken(), | 150 token_service->GetOAuth2LoginRefreshToken(), |
| 116 extension->id(), | 151 extension->id(), |
| 117 oauth2_info.client_id, | 152 oauth2_info.client_id, |
| 118 oauth2_info.scopes, | 153 oauth2_info.scopes, |
| 119 mode))); | 154 mode))); |
| 120 flow_->Start(); | 155 flow_->Start(); |
| 121 return true; | 156 return true; |
| 122 } | 157 } |
| 123 | 158 |
| 159 bool GetAuthTokenFunction::StartLogin() { |
| 160 if (!interactive_) { |
| 161 error_ = kUserNotSignedIn; |
| 162 return false; |
| 163 } |
| 164 |
| 165 ShowLoginPopup(); |
| 166 return true; |
| 167 } |
| 168 |
| 169 void GetAuthTokenFunction::ShowLoginPopup() { |
| 170 LoginUIService* login_ui_service = |
| 171 LoginUIServiceFactory::GetForProfile(profile()); |
| 172 login_ui_service->AddObserver(this); |
| 173 |
| 174 LoginUIService::LoginUI* login_ui = login_ui_service->current_login_ui(); |
| 175 if (login_ui) { |
| 176 login_ui->FocusUI(); |
| 177 } else { |
| 178 chrome::NavigateParams params(NULL, |
| 179 GURL(chrome::kChromeUISyncPromoURL), |
| 180 content::PAGE_TRANSITION_START_PAGE); |
| 181 params.profile = profile(); |
| 182 params.disposition = NEW_POPUP; |
| 183 chrome::Navigate(¶ms); |
| 184 } |
| 185 } |
| 186 |
| 187 const Extension::OAuth2Info& GetAuthTokenFunction::GetOAuth2Info() const { |
| 188 return GetExtension()->oauth2_info(); |
| 189 |
| 190 } |
| 191 |
| 192 bool GetAuthTokenFunction::HasLoginToken() const { |
| 193 TokenService* token_service = TokenServiceFactory::GetForProfile(profile()); |
| 194 return token_service->HasOAuthLoginToken(); |
| 195 } |
| 196 |
| 197 OAuth2MintTokenFlow::Mode GetAuthTokenFunction::GetTokenFlowMode() const { |
| 198 return ExtensionInstallPrompt::ShouldAutomaticallyApproveScopes() ? |
| 199 OAuth2MintTokenFlow::MODE_MINT_TOKEN_FORCE : |
| 200 OAuth2MintTokenFlow::MODE_MINT_TOKEN_NO_FORCE; |
| 201 } |
| 202 |
| 124 LaunchWebAuthFlowFunction::LaunchWebAuthFlowFunction() {} | 203 LaunchWebAuthFlowFunction::LaunchWebAuthFlowFunction() {} |
| 125 LaunchWebAuthFlowFunction::~LaunchWebAuthFlowFunction() {} | 204 LaunchWebAuthFlowFunction::~LaunchWebAuthFlowFunction() {} |
| 126 | 205 |
| 127 bool LaunchWebAuthFlowFunction::RunImpl() { | 206 bool LaunchWebAuthFlowFunction::RunImpl() { |
| 128 DictionaryValue* arg = NULL; | 207 DictionaryValue* arg = NULL; |
| 129 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &arg)); | 208 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &arg)); |
| 130 | 209 |
| 131 std::string url; | 210 std::string url; |
| 132 EXTENSION_FUNCTION_VALIDATE(arg->GetString("url", &url)); | 211 EXTENSION_FUNCTION_VALIDATE(arg->GetString("url", &url)); |
| 133 | 212 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 152 Release(); // Balanced in RunImpl. | 231 Release(); // Balanced in RunImpl. |
| 153 } | 232 } |
| 154 | 233 |
| 155 void LaunchWebAuthFlowFunction::OnAuthFlowFailure() { | 234 void LaunchWebAuthFlowFunction::OnAuthFlowFailure() { |
| 156 error_ = kInvalidRedirect; | 235 error_ = kInvalidRedirect; |
| 157 SendResponse(false); | 236 SendResponse(false); |
| 158 Release(); // Balanced in RunImpl. | 237 Release(); // Balanced in RunImpl. |
| 159 } | 238 } |
| 160 | 239 |
| 161 } // namespace extensions | 240 } // namespace extensions |
| OLD | NEW |