| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #import "ios/chrome/browser/passwords/credential_manager.h" | 5 #import "ios/chrome/browser/passwords/credential_manager.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/ios/ios_util.h" | 9 #include "base/ios/ios_util.h" |
| 10 #import "base/ios/weak_nsobject.h" | 10 #import "base/ios/weak_nsobject.h" |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 | 110 |
| 111 void CredentialManager::CredentialsRequested( | 111 void CredentialManager::CredentialsRequested( |
| 112 int request_id, | 112 int request_id, |
| 113 const GURL& source_url, | 113 const GURL& source_url, |
| 114 bool zero_click_only, | 114 bool zero_click_only, |
| 115 const std::vector<std::string>& federations, | 115 const std::vector<std::string>& federations, |
| 116 bool is_user_initiated) { | 116 bool is_user_initiated) { |
| 117 // Invoked when the page invokes navigator.credentials.request(), this | 117 // Invoked when the page invokes navigator.credentials.request(), this |
| 118 // function will attempt to retrieve a Credential from the PasswordStore that | 118 // function will attempt to retrieve a Credential from the PasswordStore that |
| 119 // meets the specified parameters and, if successful, send it back to the page | 119 // meets the specified parameters and, if successful, send it back to the page |
| 120 // via SendCredentialByID. | 120 // via SendCredential. |
| 121 DCHECK_GE(request_id, 0); | 121 DCHECK_GE(request_id, 0); |
| 122 password_manager::PasswordStore* store = GetPasswordStore(); | 122 password_manager::PasswordStore* store = GetPasswordStore(); |
| 123 | 123 |
| 124 // If there's an outstanding request, or the PasswordStore isn't loaded yet, | 124 // If there's an outstanding request, or the PasswordStore isn't loaded yet, |
| 125 // the request should fail outright and the JS Promise should be rejected | 125 // the request should fail outright and the JS Promise should be rejected |
| 126 // with an appropriate error. | 126 // with an appropriate error. |
| 127 if (pending_request_ || !store) { | 127 if (pending_request_ || !store) { |
| 128 base::MessageLoop::current()->PostTask( | 128 base::MessageLoop::current()->PostTask( |
| 129 FROM_HERE, | 129 FROM_HERE, |
| 130 base::Bind(&CredentialManager::RejectPromise, | 130 base::Bind(&CredentialManager::RejectPromise, |
| 131 weak_factory_.GetWeakPtr(), request_id, | 131 weak_factory_.GetWeakPtr(), request_id, |
| 132 pending_request_ ? ERROR_TYPE_PENDING_REQUEST | 132 pending_request_ ? ERROR_TYPE_PENDING_REQUEST |
| 133 : ERROR_TYPE_PASSWORD_STORE_UNAVAILABLE)); | 133 : ERROR_TYPE_PASSWORD_STORE_UNAVAILABLE)); |
| 134 return; | 134 return; |
| 135 } | 135 } |
| 136 | 136 |
| 137 // If the page requested a zero-click credential -- one that can be returned | 137 // If the page requested a zero-click credential -- one that can be returned |
| 138 // without first asking the user -- and if zero-click isn't currently | 138 // without first asking the user -- and if zero-click isn't currently |
| 139 // available, send back an empty credential. | 139 // available, send back an empty credential. |
| 140 if (zero_click_only && !IsZeroClickAllowed()) { | 140 if (zero_click_only && !IsZeroClickAllowed()) { |
| 141 base::MessageLoop::current()->PostTask( | 141 base::MessageLoop::current()->PostTask( |
| 142 FROM_HERE, base::Bind(&CredentialManager::SendCredentialByID, | 142 FROM_HERE, base::Bind(&CredentialManager::SendCredential, |
| 143 weak_factory_.GetWeakPtr(), request_id, | 143 weak_factory_.GetWeakPtr(), request_id, |
| 144 password_manager::CredentialInfo())); | 144 password_manager::CredentialInfo())); |
| 145 return; | 145 return; |
| 146 } | 146 } |
| 147 | 147 |
| 148 // If the page origin is untrusted, the request should be rejected. | 148 // If the page origin is untrusted, the request should be rejected. |
| 149 GURL page_url; | 149 GURL page_url; |
| 150 if (!GetUrlWithAbsoluteTrust(&page_url)) { | 150 if (!GetUrlWithAbsoluteTrust(&page_url)) { |
| 151 RejectPromise(request_id, ERROR_TYPE_SECURITY_ERROR_UNTRUSTED_ORIGIN); | 151 RejectPromise(request_id, ERROR_TYPE_SECURITY_ERROR_UNTRUSTED_ORIGIN); |
| 152 return; | 152 return; |
| 153 } | 153 } |
| 154 | 154 |
| 155 // Bundle up the arguments and forward them to the PasswordStore, which will | 155 // Bundle up the arguments and forward them to the PasswordStore, which will |
| 156 // asynchronously return the resulting Credential by invoking | 156 // asynchronously return the resulting Credential by invoking |
| 157 // |SendCredential|. | 157 // |SendCredential|. |
| 158 std::vector<GURL> federation_urls; | 158 std::vector<GURL> federation_urls; |
| 159 for (const auto& federation : federations) | 159 for (const auto& federation : federations) |
| 160 federation_urls.push_back(GURL(federation)); | 160 federation_urls.push_back(GURL(federation)); |
| 161 std::vector<std::string> realms; | 161 std::vector<std::string> realms; |
| 162 pending_request_.reset( | 162 pending_request_.reset( |
| 163 new password_manager::CredentialManagerPendingRequestTask( | 163 new password_manager::CredentialManagerPendingRequestTask( |
| 164 this, base::Bind(&CredentialManager::SendCredentialByID, | 164 this, request_id, zero_click_only, page_url, true, federation_urls, |
| 165 base::Unretained(this), request_id), | 165 realms)); |
| 166 zero_click_only, page_url, true, federation_urls, realms)); | |
| 167 store->GetAutofillableLogins(pending_request_.get()); | 166 store->GetAutofillableLogins(pending_request_.get()); |
| 168 } | 167 } |
| 169 | 168 |
| 170 void CredentialManager::SignedIn(int request_id, | 169 void CredentialManager::SignedIn(int request_id, |
| 171 const GURL& source_url, | 170 const GURL& source_url, |
| 172 const web::Credential& credential) { | 171 const web::Credential& credential) { |
| 173 // Invoked when the page invokes navigator.credentials.notifySignedIn(), this | 172 // Invoked when the page invokes navigator.credentials.notifySignedIn(), this |
| 174 // function stores the signed-in |credential| and sends a message back to the | 173 // function stores the signed-in |credential| and sends a message back to the |
| 175 // page to resolve the Promise associated with |request_id|. | 174 // page to resolve the Promise associated with |request_id|. |
| 176 DCHECK(credential.type != web::CredentialType::CREDENTIAL_TYPE_EMPTY); | 175 DCHECK(credential.type != web::CredentialType::CREDENTIAL_TYPE_EMPTY); |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 | 262 |
| 264 GURL CredentialManager::GetOrigin() const { | 263 GURL CredentialManager::GetOrigin() const { |
| 265 web::URLVerificationTrustLevel trust_level = | 264 web::URLVerificationTrustLevel trust_level = |
| 266 web::URLVerificationTrustLevel::kNone; | 265 web::URLVerificationTrustLevel::kNone; |
| 267 const GURL page_url(web_state()->GetCurrentURL(&trust_level)); | 266 const GURL page_url(web_state()->GetCurrentURL(&trust_level)); |
| 268 DCHECK_EQ(trust_level, web::URLVerificationTrustLevel::kAbsolute); | 267 DCHECK_EQ(trust_level, web::URLVerificationTrustLevel::kAbsolute); |
| 269 return page_url; | 268 return page_url; |
| 270 } | 269 } |
| 271 | 270 |
| 272 void CredentialManager::SendCredential( | 271 void CredentialManager::SendCredential( |
| 273 const password_manager::SendCredentialCallback& send_callback, | |
| 274 const password_manager::CredentialInfo& credential) { | |
| 275 send_callback.Run(credential); | |
| 276 } | |
| 277 | |
| 278 void CredentialManager::SendCredentialByID( | |
| 279 int request_id, | 272 int request_id, |
| 280 const password_manager::CredentialInfo& credential) { | 273 const password_manager::CredentialInfo& credential) { |
| 281 // Invoked when the asynchronous interaction with the PasswordStore completes, | 274 // Invoked when the asynchronous interaction with the PasswordStore completes, |
| 282 // this function forwards a |credential| back to the page via |js_manager_| by | 275 // this function forwards a |credential| back to the page via |js_manager_| by |
| 283 // resolving the JavaScript Promise associated with |request_id|. | 276 // resolving the JavaScript Promise associated with |request_id|. |
| 284 base::WeakPtr<CredentialManager> weak_this = weak_factory_.GetWeakPtr(); | 277 base::WeakPtr<CredentialManager> weak_this = weak_factory_.GetWeakPtr(); |
| 285 [js_manager_ | 278 [js_manager_ |
| 286 resolvePromiseWithRequestID:request_id | 279 resolvePromiseWithRequestID:request_id |
| 287 credential:WebCredentialFromCredentialInfo(credential) | 280 credential:WebCredentialFromCredentialInfo(credential) |
| 288 completionHandler:^(BOOL) { | 281 completionHandler:^(BOOL) { |
| 289 if (weak_this) | 282 if (weak_this) |
| 290 weak_this->pending_request_.reset(); | 283 weak_this->pending_request_.reset(); |
| 291 }]; | 284 }]; |
| 292 } | 285 } |
| 293 | 286 |
| 294 void CredentialManager::SendPasswordForm( | 287 void CredentialManager::SendPasswordForm(int request_id, |
| 295 const password_manager::SendCredentialCallback& send_callback, | 288 const autofill::PasswordForm* form) { |
| 296 const autofill::PasswordForm* form) { | |
| 297 password_manager::CredentialInfo info; | 289 password_manager::CredentialInfo info; |
| 298 if (form) { | 290 if (form) { |
| 299 password_manager::CredentialType type_to_return = | 291 password_manager::CredentialType type_to_return = |
| 300 form->federation_origin.unique() | 292 form->federation_origin.unique() |
| 301 ? password_manager::CredentialType::CREDENTIAL_TYPE_PASSWORD | 293 ? password_manager::CredentialType::CREDENTIAL_TYPE_PASSWORD |
| 302 : password_manager::CredentialType::CREDENTIAL_TYPE_FEDERATED; | 294 : password_manager::CredentialType::CREDENTIAL_TYPE_FEDERATED; |
| 303 info = password_manager::CredentialInfo(*form, type_to_return); | 295 info = password_manager::CredentialInfo(*form, type_to_return); |
| 304 // TODO(vasilii): update |skip_zero_click| in the store (crbug.com/594110). | 296 // TODO(vasilii): update |skip_zero_click| in the store (crbug.com/594110). |
| 305 } | 297 } |
| 306 SendCredential(send_callback, info); | 298 SendCredential(request_id, info); |
| 307 } | 299 } |
| 308 | 300 |
| 309 password_manager::PasswordManagerClient* CredentialManager::client() const { | 301 password_manager::PasswordManagerClient* CredentialManager::client() const { |
| 310 return client_; | 302 return client_; |
| 311 } | 303 } |
| 312 | 304 |
| 313 autofill::PasswordForm CredentialManager::GetSynthesizedFormForOrigin() const { | 305 autofill::PasswordForm CredentialManager::GetSynthesizedFormForOrigin() const { |
| 314 autofill::PasswordForm synthetic_form; | 306 autofill::PasswordForm synthetic_form; |
| 315 synthetic_form.origin = web_state()->GetLastCommittedURL().GetOrigin(); | 307 synthetic_form.origin = web_state()->GetLastCommittedURL().GetOrigin(); |
| 316 synthetic_form.signon_realm = synthetic_form.origin.spec(); | 308 synthetic_form.signon_realm = synthetic_form.origin.spec(); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 377 bool CredentialManager::GetUrlWithAbsoluteTrust(GURL* page_url) { | 369 bool CredentialManager::GetUrlWithAbsoluteTrust(GURL* page_url) { |
| 378 web::URLVerificationTrustLevel trust_level = | 370 web::URLVerificationTrustLevel trust_level = |
| 379 web::URLVerificationTrustLevel::kNone; | 371 web::URLVerificationTrustLevel::kNone; |
| 380 const GURL possibly_untrusted_url(web_state()->GetCurrentURL(&trust_level)); | 372 const GURL possibly_untrusted_url(web_state()->GetCurrentURL(&trust_level)); |
| 381 if (trust_level == web::URLVerificationTrustLevel::kAbsolute) { | 373 if (trust_level == web::URLVerificationTrustLevel::kAbsolute) { |
| 382 *page_url = possibly_untrusted_url; | 374 *page_url = possibly_untrusted_url; |
| 383 return true; | 375 return true; |
| 384 } | 376 } |
| 385 return false; | 377 return false; |
| 386 } | 378 } |
| OLD | NEW |