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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/login/login_prompt.h"
10 #include "base/command_line.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/synchronization/lock.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/password_manager/password_manager.h"
15 #include "chrome/browser/prerender/prerender_contents.h"
16 #include "chrome/browser/tab_contents/tab_util.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/notification_registrar.h"
19 #include "content/public/browser/notification_service.h"
20 #include "content/public/browser/render_frame_host.h"
21 #include "content/public/browser/resource_dispatcher_host.h"
22 #include "content/public/browser/resource_request_info.h"
23 #include "content/public/browser/web_contents.h"
24 #include "grit/generated_resources.h"
25 #include "net/base/auth.h"
26 #include "net/base/net_util.h"
27 #include "net/http/http_transaction_factory.h"
28 #include "net/url_request/url_request.h"
29 #include "net/url_request/url_request_context.h"
30 #include "ui/base/l10n/l10n_util.h"
31 #include "ui/gfx/text_elider.h"
33 using autofill::PasswordForm;
34 using content::BrowserThread;
35 using content::NavigationController;
36 using content::RenderViewHost;
37 using content::RenderViewHostDelegate;
38 using content::ResourceDispatcherHost;
39 using content::ResourceRequestInfo;
40 using content::WebContents;
42 class LoginHandlerImpl;
44 // Helper to remove the ref from an net::URLRequest to the LoginHandler.
45 // Should only be called from the IO thread, since it accesses an
47 void ResetLoginHandlerForRequest(net::URLRequest* request) {
48 ResourceDispatcherHost::Get()->ClearLoginDelegateForRequest(request);
51 // Get the signon_realm under which this auth info should be stored.
53 // The format of the signon_realm for proxy auth is:
54 // proxy-host/auth-realm
55 // The format of the signon_realm for server auth is:
56 // url-scheme://url-host[:url-port]/auth-realm
58 // Be careful when changing this function, since you could make existing
59 // saved logins un-retrievable.
60 std::string GetSignonRealm(const GURL& url,
61 const net::AuthChallengeInfo& auth_info) {
62 std::string signon_realm;
63 if (auth_info.is_proxy) {
64 signon_realm = auth_info.challenger.ToString();
65 signon_realm.append("/");
67 // Take scheme, host, and port from the url.
68 signon_realm = url.GetOrigin().spec();
69 // This ends with a "/".
71 signon_realm.append(auth_info.realm);
75 // ----------------------------------------------------------------------------
78 LoginHandler::LoginHandler(net::AuthChallengeInfo* auth_info,
79 net::URLRequest* request)
80 : handled_auth_(false),
81 auth_info_(auth_info),
83 http_network_session_(
84 request_->context()->http_transaction_factory()->GetSession()),
85 password_manager_(NULL),
87 // This constructor is called on the I/O thread, so we cannot load the nib
88 // here. BuildViewForPasswordManager() will be invoked on the UI thread
89 // later, so wait with loading the nib until then.
90 DCHECK(request_) << "LoginHandler constructed with NULL request";
91 DCHECK(auth_info_.get()) << "LoginHandler constructed with NULL auth info";
93 AddRef(); // matched by LoginHandler::ReleaseSoon().
95 BrowserThread::PostTask(
96 BrowserThread::UI, FROM_HERE,
97 base::Bind(&LoginHandler::AddObservers, this));
99 if (!ResourceRequestInfo::ForRequest(request_)->GetAssociatedRenderFrame(
100 &render_process_host_id_, &render_frame_id_)) {
105 void LoginHandler::OnRequestCancelled() {
106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)) <<
107 "Why is OnRequestCancelled called from the UI thread?";
109 // Reference is no longer valid.
112 // Give up on auth if the request was cancelled.
116 void LoginHandler::SetPasswordForm(const autofill::PasswordForm& form) {
117 password_form_ = form;
120 void LoginHandler::SetPasswordManager(PasswordManager* password_manager) {
121 password_manager_ = password_manager;
124 WebContents* LoginHandler::GetWebContentsForLogin() const {
125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
127 content::RenderFrameHost* rfh = content::RenderFrameHost::FromID(
128 render_process_host_id_, render_frame_id_);
129 return WebContents::FromRenderFrameHost(rfh);
132 void LoginHandler::SetAuth(const base::string16& username,
133 const base::string16& password) {
134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
136 if (TestAndSetAuthHandled())
139 // Tell the password manager the credentials were submitted / accepted.
140 if (password_manager_) {
141 password_form_.username_value = username;
142 password_form_.password_value = password;
143 password_manager_->ProvisionallySavePassword(password_form_);
146 // Calling NotifyAuthSupplied() directly instead of posting a task
147 // allows other LoginHandler instances to queue their
148 // CloseContentsDeferred() before ours. Closing dialogs in the
149 // opposite order as they were created avoids races where remaining
150 // dialogs in the same tab may be briefly displayed to the user
151 // before they are removed.
152 NotifyAuthSupplied(username, password);
154 BrowserThread::PostTask(
155 BrowserThread::UI, FROM_HERE,
156 base::Bind(&LoginHandler::CloseContentsDeferred, this));
157 BrowserThread::PostTask(
158 BrowserThread::IO, FROM_HERE,
159 base::Bind(&LoginHandler::SetAuthDeferred, this, username, password));
162 void LoginHandler::CancelAuth() {
163 if (TestAndSetAuthHandled())
166 // Similar to how we deal with notifications above in SetAuth()
167 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
168 NotifyAuthCancelled();
170 BrowserThread::PostTask(
171 BrowserThread::UI, FROM_HERE,
172 base::Bind(&LoginHandler::NotifyAuthCancelled, this));
175 BrowserThread::PostTask(
176 BrowserThread::UI, FROM_HERE,
177 base::Bind(&LoginHandler::CloseContentsDeferred, this));
178 BrowserThread::PostTask(
179 BrowserThread::IO, FROM_HERE,
180 base::Bind(&LoginHandler::CancelAuthDeferred, this));
184 void LoginHandler::Observe(int type,
185 const content::NotificationSource& source,
186 const content::NotificationDetails& details) {
187 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
188 DCHECK(type == chrome::NOTIFICATION_AUTH_SUPPLIED ||
189 type == chrome::NOTIFICATION_AUTH_CANCELLED);
191 WebContents* requesting_contents = GetWebContentsForLogin();
192 if (!requesting_contents)
195 // Break out early if we aren't interested in the notification.
196 if (WasAuthHandled())
199 LoginNotificationDetails* login_details =
200 content::Details<LoginNotificationDetails>(details).ptr();
202 // WasAuthHandled() should always test positive before we publish
203 // AUTH_SUPPLIED or AUTH_CANCELLED notifications.
204 DCHECK(login_details->handler() != this);
206 // Only handle notification for the identical auth info.
207 if (!login_details->handler()->auth_info()->Equals(*auth_info()))
210 // Ignore login notification events from other profiles.
211 if (login_details->handler()->http_network_session_ !=
212 http_network_session_)
215 // Set or cancel the auth in this handler.
216 if (type == chrome::NOTIFICATION_AUTH_SUPPLIED) {
217 AuthSuppliedLoginNotificationDetails* supplied_details =
218 content::Details<AuthSuppliedLoginNotificationDetails>(details).ptr();
219 SetAuth(supplied_details->username(), supplied_details->password());
221 DCHECK(type == chrome::NOTIFICATION_AUTH_CANCELLED);
226 // Returns whether authentication had been handled (SetAuth or CancelAuth).
227 bool LoginHandler::WasAuthHandled() const {
228 base::AutoLock lock(handled_auth_lock_);
229 bool was_handled = handled_auth_;
233 LoginHandler::~LoginHandler() {
237 void LoginHandler::SetModel(LoginModel* model) {
239 login_model_->RemoveObserver(this);
240 login_model_ = model;
242 login_model_->AddObserver(this);
245 void LoginHandler::NotifyAuthNeeded() {
246 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
247 if (WasAuthHandled())
250 content::NotificationService* service =
251 content::NotificationService::current();
252 NavigationController* controller = NULL;
254 WebContents* requesting_contents = GetWebContentsForLogin();
255 if (requesting_contents)
256 controller = &requesting_contents->GetController();
258 LoginNotificationDetails details(this);
260 service->Notify(chrome::NOTIFICATION_AUTH_NEEDED,
261 content::Source<NavigationController>(controller),
262 content::Details<LoginNotificationDetails>(&details));
265 void LoginHandler::ReleaseSoon() {
266 if (!TestAndSetAuthHandled()) {
267 BrowserThread::PostTask(
268 BrowserThread::IO, FROM_HERE,
269 base::Bind(&LoginHandler::CancelAuthDeferred, this));
270 BrowserThread::PostTask(
271 BrowserThread::UI, FROM_HERE,
272 base::Bind(&LoginHandler::NotifyAuthCancelled, this));
275 BrowserThread::PostTask(
276 BrowserThread::UI, FROM_HERE,
277 base::Bind(&LoginHandler::RemoveObservers, this));
279 // Delete this object once all InvokeLaters have been called.
280 BrowserThread::ReleaseSoon(BrowserThread::IO, FROM_HERE, this);
283 void LoginHandler::AddObservers() {
284 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
286 // This is probably OK; we need to listen to everything and we break out of
287 // the Observe() if we aren't handling the same auth_info().
288 registrar_.reset(new content::NotificationRegistrar);
289 registrar_->Add(this, chrome::NOTIFICATION_AUTH_SUPPLIED,
290 content::NotificationService::AllBrowserContextsAndSources());
291 registrar_->Add(this, chrome::NOTIFICATION_AUTH_CANCELLED,
292 content::NotificationService::AllBrowserContextsAndSources());
295 void LoginHandler::RemoveObservers() {
296 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
301 void LoginHandler::NotifyAuthSupplied(const base::string16& username,
302 const base::string16& password) {
303 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
304 DCHECK(WasAuthHandled());
306 WebContents* requesting_contents = GetWebContentsForLogin();
307 if (!requesting_contents)
310 content::NotificationService* service =
311 content::NotificationService::current();
312 NavigationController* controller =
313 &requesting_contents->GetController();
314 AuthSuppliedLoginNotificationDetails details(this, username, password);
317 chrome::NOTIFICATION_AUTH_SUPPLIED,
318 content::Source<NavigationController>(controller),
319 content::Details<AuthSuppliedLoginNotificationDetails>(&details));
322 void LoginHandler::NotifyAuthCancelled() {
323 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
324 DCHECK(WasAuthHandled());
326 content::NotificationService* service =
327 content::NotificationService::current();
328 NavigationController* controller = NULL;
330 WebContents* requesting_contents = GetWebContentsForLogin();
331 if (requesting_contents)
332 controller = &requesting_contents->GetController();
334 LoginNotificationDetails details(this);
336 service->Notify(chrome::NOTIFICATION_AUTH_CANCELLED,
337 content::Source<NavigationController>(controller),
338 content::Details<LoginNotificationDetails>(&details));
341 // Marks authentication as handled and returns the previous handled state.
342 bool LoginHandler::TestAndSetAuthHandled() {
343 base::AutoLock lock(handled_auth_lock_);
344 bool was_handled = handled_auth_;
345 handled_auth_ = true;
349 // Calls SetAuth from the IO loop.
350 void LoginHandler::SetAuthDeferred(const base::string16& username,
351 const base::string16& password) {
352 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
355 request_->SetAuth(net::AuthCredentials(username, password));
356 ResetLoginHandlerForRequest(request_);
360 // Calls CancelAuth from the IO loop.
361 void LoginHandler::CancelAuthDeferred() {
362 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
365 request_->CancelAuth();
366 // Verify that CancelAuth doesn't destroy the request via our delegate.
367 DCHECK(request_ != NULL);
368 ResetLoginHandlerForRequest(request_);
372 // Closes the view_contents from the UI loop.
373 void LoginHandler::CloseContentsDeferred() {
374 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
379 // Helper to create a PasswordForm and stuff it into a vector as input
380 // for PasswordManager::PasswordFormsParsed, the hook into PasswordManager.
381 void MakeInputForPasswordManager(
382 const GURL& request_url,
383 net::AuthChallengeInfo* auth_info,
384 LoginHandler* handler,
385 std::vector<PasswordForm>* password_manager_input) {
386 PasswordForm dialog_form;
387 if (LowerCaseEqualsASCII(auth_info->scheme, "basic")) {
388 dialog_form.scheme = PasswordForm::SCHEME_BASIC;
389 } else if (LowerCaseEqualsASCII(auth_info->scheme, "digest")) {
390 dialog_form.scheme = PasswordForm::SCHEME_DIGEST;
392 dialog_form.scheme = PasswordForm::SCHEME_OTHER;
394 std::string host_and_port(auth_info->challenger.ToString());
395 if (auth_info->is_proxy) {
396 std::string origin = host_and_port;
397 // We don't expect this to already start with http:// or https://.
398 DCHECK(origin.find("http://") != 0 && origin.find("https://") != 0);
399 origin = std::string("http://") + origin;
400 dialog_form.origin = GURL(origin);
401 } else if (!auth_info->challenger.Equals(
402 net::HostPortPair::FromURL(request_url))) {
403 dialog_form.origin = GURL();
404 NOTREACHED(); // crbug.com/32718
406 dialog_form.origin = GURL(request_url.scheme() + "://" + host_and_port);
408 dialog_form.signon_realm = GetSignonRealm(dialog_form.origin, *auth_info);
409 password_manager_input->push_back(dialog_form);
410 // Set the password form for the handler (by copy).
411 handler->SetPasswordForm(dialog_form);
414 // This callback is run on the UI thread and creates a constrained window with
415 // a LoginView to prompt the user. The response will be sent to LoginHandler,
416 // which then routes it to the net::URLRequest on the I/O thread.
417 void LoginDialogCallback(const GURL& request_url,
418 net::AuthChallengeInfo* auth_info,
419 LoginHandler* handler) {
420 WebContents* parent_contents = handler->GetWebContentsForLogin();
421 if (!parent_contents || handler->WasAuthHandled()) {
422 // The request may have been cancelled, or it may be for a renderer
423 // not hosted by a tab (e.g. an extension). Cancel just in case
424 // (cancelling twice is a no-op).
425 handler->CancelAuth();
429 prerender::PrerenderContents* prerender_contents =
430 prerender::PrerenderContents::FromWebContents(parent_contents);
431 if (prerender_contents) {
432 prerender_contents->Destroy(prerender::FINAL_STATUS_AUTH_NEEDED);
436 PasswordManager* password_manager =
437 PasswordManager::FromWebContents(parent_contents);
438 if (!password_manager) {
439 // Same logic as above.
440 handler->CancelAuth();
444 // Tell the password manager to look for saved passwords.
445 std::vector<PasswordForm> v;
446 MakeInputForPasswordManager(request_url, auth_info, handler, &v);
447 password_manager->OnPasswordFormsParsed(v);
448 handler->SetPasswordManager(password_manager);
450 // The realm is controlled by the remote server, so there is no reason
451 // to believe it is of a reasonable length.
452 base::string16 elided_realm;
453 gfx::ElideString(base::UTF8ToUTF16(auth_info->realm), 120, &elided_realm);
455 base::string16 host_and_port = base::ASCIIToUTF16(
456 request_url.scheme() + "://" + auth_info->challenger.ToString());
457 base::string16 explanation = elided_realm.empty() ?
458 l10n_util::GetStringFUTF16(IDS_LOGIN_DIALOG_DESCRIPTION_NO_REALM,
460 l10n_util::GetStringFUTF16(IDS_LOGIN_DIALOG_DESCRIPTION,
463 handler->BuildViewForPasswordManager(password_manager, explanation);
466 // ----------------------------------------------------------------------------
469 LoginHandler* CreateLoginPrompt(net::AuthChallengeInfo* auth_info,
470 net::URLRequest* request) {
471 LoginHandler* handler = LoginHandler::Create(auth_info, request);
472 BrowserThread::PostTask(
473 BrowserThread::UI, FROM_HERE,
474 base::Bind(&LoginDialogCallback, request->url(),
475 make_scoped_refptr(auth_info), make_scoped_refptr(handler)));