blob: 3b0a4dcebc71f20de51e2f4e646f4361f1e8db66 [file] [log] [blame]
[email protected]b6754252012-06-13 23:14:381// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]c3b35c22008-09-27 03:19:422// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]c3b35c22008-09-27 03:19:425#include "net/http/http_auth_handler.h"
6
[email protected]49639fa2011-12-20 23:22:417#include "base/bind.h"
8#include "base/bind_helpers.h"
[email protected]ac3fa8e22010-02-05 19:13:299#include "base/logging.h"
[email protected]e5ae96a2010-04-14 20:12:4510#include "net/base/net_errors.h"
[email protected]df41d0d82014-03-13 00:43:2411#include "net/http/http_auth_challenge_tokenizer.h"
mikecirone8b85c432016-09-08 19:11:0012#include "net/log/net_log_event_type.h"
[email protected]ac3fa8e22010-02-05 19:13:2913
[email protected]c3b35c22008-09-27 03:19:4214namespace net {
15
[email protected]7775c7d2010-06-21 17:50:2116HttpAuthHandler::HttpAuthHandler()
[email protected]547fc792011-01-13 13:31:1717 : auth_scheme_(HttpAuth::AUTH_SCHEME_MAX),
[email protected]65f37772010-12-13 18:20:0618 score_(-1),
[email protected]7775c7d2010-06-21 17:50:2119 target_(HttpAuth::AUTH_NONE),
[email protected]49639fa2011-12-20 23:22:4120 properties_(-1) {
[email protected]7775c7d2010-06-21 17:50:2121}
22
[email protected]cb2825c2010-07-01 10:23:3823HttpAuthHandler::~HttpAuthHandler() {
24}
25
asanka5ffd5d72016-03-23 16:20:4926bool HttpAuthHandler::InitFromChallenge(HttpAuthChallengeTokenizer* challenge,
27 HttpAuth::Target target,
28 const SSLInfo& ssl_info,
29 const GURL& origin,
tfarina428341112016-09-22 13:38:2030 const NetLogWithSource& net_log) {
[email protected]4de702f42009-09-18 17:46:1031 origin_ = origin;
[email protected]c3b35c22008-09-27 03:19:4232 target_ = target;
33 score_ = -1;
[email protected]3f918782009-02-28 01:29:2434 properties_ = -1;
[email protected]ac5c06e2010-05-27 15:07:3835 net_log_ = net_log;
[email protected]c3b35c22008-09-27 03:19:4236
[email protected]fa82f932010-05-20 11:09:2437 auth_challenge_ = challenge->challenge_text();
asanka5ffd5d72016-03-23 16:20:4938 bool ok = Init(challenge, ssl_info);
[email protected]c3b35c22008-09-27 03:19:4239
[email protected]3f918782009-02-28 01:29:2440 // Init() is expected to set the scheme, realm, score, and properties. The
41 // realm may be empty.
[email protected]c3b35c22008-09-27 03:19:4242 DCHECK(!ok || score_ != -1);
[email protected]3f918782009-02-28 01:29:2443 DCHECK(!ok || properties_ != -1);
[email protected]547fc792011-01-13 13:31:1744 DCHECK(!ok || auth_scheme_ != HttpAuth::AUTH_SCHEME_MAX);
[email protected]3f918782009-02-28 01:29:2445
[email protected]c3b35c22008-09-27 03:19:4246 return ok;
47}
48
[email protected]7775c7d2010-06-21 17:50:2149namespace {
50
mikecirone8b85c432016-09-08 19:11:0051NetLogEventType EventTypeFromAuthTarget(HttpAuth::Target target) {
[email protected]7775c7d2010-06-21 17:50:2152 switch (target) {
53 case HttpAuth::AUTH_PROXY:
mikecirone8b85c432016-09-08 19:11:0054 return NetLogEventType::AUTH_PROXY;
[email protected]7775c7d2010-06-21 17:50:2155 case HttpAuth::AUTH_SERVER:
mikecirone8b85c432016-09-08 19:11:0056 return NetLogEventType::AUTH_SERVER;
[email protected]7775c7d2010-06-21 17:50:2157 default:
58 NOTREACHED();
mikecirone8b85c432016-09-08 19:11:0059 return NetLogEventType::CANCELLED;
[email protected]7775c7d2010-06-21 17:50:2160 }
61}
62
63} // namespace
64
[email protected]49639fa2011-12-20 23:22:4165int HttpAuthHandler::GenerateAuthToken(
66 const AuthCredentials* credentials, const HttpRequestInfo* request,
67 const CompletionCallback& callback, std::string* auth_token) {
ricea4bbafa7f262014-11-11 17:29:5868 DCHECK(!callback.is_null());
[email protected]bcc528e2010-06-10 15:03:2469 DCHECK(request);
[email protected]f3cf9802011-10-28 18:44:5870 DCHECK(credentials != NULL || AllowsDefaultCredentials());
[email protected]044de0642010-06-17 10:42:1571 DCHECK(auth_token != NULL);
[email protected]49639fa2011-12-20 23:22:4172 DCHECK(callback_.is_null());
73 callback_ = callback;
[email protected]b6754252012-06-13 23:14:3874 net_log_.BeginEvent(EventTypeFromAuthTarget(target_));
[email protected]49639fa2011-12-20 23:22:4175 int rv = GenerateAuthTokenImpl(
76 credentials, request,
77 base::Bind(&HttpAuthHandler::OnGenerateAuthTokenComplete,
78 base::Unretained(this)),
79 auth_token);
[email protected]7775c7d2010-06-21 17:50:2180 if (rv != ERR_IO_PENDING)
81 FinishGenerateAuthToken();
82 return rv;
83}
84
[email protected]7cf40912010-12-09 18:25:0385bool HttpAuthHandler::NeedsIdentity() {
86 return true;
87}
88
89bool HttpAuthHandler::AllowsDefaultCredentials() {
90 return false;
91}
92
[email protected]26d84b02011-08-31 14:07:0893bool HttpAuthHandler::AllowsExplicitCredentials() {
94 return true;
95}
96
[email protected]7775c7d2010-06-21 17:50:2197void HttpAuthHandler::OnGenerateAuthTokenComplete(int rv) {
[email protected]49639fa2011-12-20 23:22:4198 CompletionCallback callback = callback_;
[email protected]7775c7d2010-06-21 17:50:2199 FinishGenerateAuthToken();
ricea4576c8c2014-11-12 18:48:25100 DCHECK(!callback.is_null());
101 callback.Run(rv);
[email protected]7775c7d2010-06-21 17:50:21102}
103
104void HttpAuthHandler::FinishGenerateAuthToken() {
Scott Graham949aef82017-08-23 22:36:54105 // TODO(cbentzel): Should this be done in OK case only?
[email protected]b6754252012-06-13 23:14:38106 net_log_.EndEvent(EventTypeFromAuthTarget(target_));
[email protected]49639fa2011-12-20 23:22:41107 callback_.Reset();
[email protected]bcc528e2010-06-10 15:03:24108}
109
[email protected]3f918782009-02-28 01:29:24110} // namespace net