[email protected] | b675425 | 2012-06-13 23:14:38 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 5 | #include "net/http/http_auth_handler.h" |
| 6 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7 | #include "base/bind.h" |
| 8 | #include "base/bind_helpers.h" |
[email protected] | ac3fa8e2 | 2010-02-05 19:13:29 | [diff] [blame] | 9 | #include "base/logging.h" |
[email protected] | e5ae96a | 2010-04-14 20:12:45 | [diff] [blame] | 10 | #include "net/base/net_errors.h" |
[email protected] | df41d0d8 | 2014-03-13 00:43:24 | [diff] [blame] | 11 | #include "net/http/http_auth_challenge_tokenizer.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 12 | #include "net/log/net_log_event_type.h" |
[email protected] | ac3fa8e2 | 2010-02-05 19:13:29 | [diff] [blame] | 13 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 14 | namespace net { |
| 15 | |
[email protected] | 7775c7d | 2010-06-21 17:50:21 | [diff] [blame] | 16 | HttpAuthHandler::HttpAuthHandler() |
[email protected] | 547fc79 | 2011-01-13 13:31:17 | [diff] [blame] | 17 | : auth_scheme_(HttpAuth::AUTH_SCHEME_MAX), |
[email protected] | 65f3777 | 2010-12-13 18:20:06 | [diff] [blame] | 18 | score_(-1), |
[email protected] | 7775c7d | 2010-06-21 17:50:21 | [diff] [blame] | 19 | target_(HttpAuth::AUTH_NONE), |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 20 | properties_(-1) { |
[email protected] | 7775c7d | 2010-06-21 17:50:21 | [diff] [blame] | 21 | } |
| 22 | |
[email protected] | cb2825c | 2010-07-01 10:23:38 | [diff] [blame] | 23 | HttpAuthHandler::~HttpAuthHandler() { |
| 24 | } |
| 25 | |
asanka | 5ffd5d7 | 2016-03-23 16:20:49 | [diff] [blame] | 26 | bool HttpAuthHandler::InitFromChallenge(HttpAuthChallengeTokenizer* challenge, |
| 27 | HttpAuth::Target target, |
| 28 | const SSLInfo& ssl_info, |
| 29 | const GURL& origin, |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 30 | const NetLogWithSource& net_log) { |
[email protected] | 4de702f4 | 2009-09-18 17:46:10 | [diff] [blame] | 31 | origin_ = origin; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 32 | target_ = target; |
| 33 | score_ = -1; |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 34 | properties_ = -1; |
[email protected] | ac5c06e | 2010-05-27 15:07:38 | [diff] [blame] | 35 | net_log_ = net_log; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 36 | |
[email protected] | fa82f93 | 2010-05-20 11:09:24 | [diff] [blame] | 37 | auth_challenge_ = challenge->challenge_text(); |
asanka | 5ffd5d7 | 2016-03-23 16:20:49 | [diff] [blame] | 38 | bool ok = Init(challenge, ssl_info); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 39 | |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 40 | // Init() is expected to set the scheme, realm, score, and properties. The |
| 41 | // realm may be empty. |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 42 | DCHECK(!ok || score_ != -1); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 43 | DCHECK(!ok || properties_ != -1); |
[email protected] | 547fc79 | 2011-01-13 13:31:17 | [diff] [blame] | 44 | DCHECK(!ok || auth_scheme_ != HttpAuth::AUTH_SCHEME_MAX); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 45 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 46 | return ok; |
| 47 | } |
| 48 | |
[email protected] | 7775c7d | 2010-06-21 17:50:21 | [diff] [blame] | 49 | namespace { |
| 50 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 51 | NetLogEventType EventTypeFromAuthTarget(HttpAuth::Target target) { |
[email protected] | 7775c7d | 2010-06-21 17:50:21 | [diff] [blame] | 52 | switch (target) { |
| 53 | case HttpAuth::AUTH_PROXY: |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 54 | return NetLogEventType::AUTH_PROXY; |
[email protected] | 7775c7d | 2010-06-21 17:50:21 | [diff] [blame] | 55 | case HttpAuth::AUTH_SERVER: |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 56 | return NetLogEventType::AUTH_SERVER; |
[email protected] | 7775c7d | 2010-06-21 17:50:21 | [diff] [blame] | 57 | default: |
| 58 | NOTREACHED(); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 59 | return NetLogEventType::CANCELLED; |
[email protected] | 7775c7d | 2010-06-21 17:50:21 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
| 63 | } // namespace |
| 64 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 65 | int HttpAuthHandler::GenerateAuthToken( |
| 66 | const AuthCredentials* credentials, const HttpRequestInfo* request, |
| 67 | const CompletionCallback& callback, std::string* auth_token) { |
ricea | 4bbafa7f26 | 2014-11-11 17:29:58 | [diff] [blame] | 68 | DCHECK(!callback.is_null()); |
[email protected] | bcc528e | 2010-06-10 15:03:24 | [diff] [blame] | 69 | DCHECK(request); |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 70 | DCHECK(credentials != NULL || AllowsDefaultCredentials()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 71 | DCHECK(auth_token != NULL); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 72 | DCHECK(callback_.is_null()); |
| 73 | callback_ = callback; |
[email protected] | b675425 | 2012-06-13 23:14:38 | [diff] [blame] | 74 | net_log_.BeginEvent(EventTypeFromAuthTarget(target_)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 75 | int rv = GenerateAuthTokenImpl( |
| 76 | credentials, request, |
| 77 | base::Bind(&HttpAuthHandler::OnGenerateAuthTokenComplete, |
| 78 | base::Unretained(this)), |
| 79 | auth_token); |
[email protected] | 7775c7d | 2010-06-21 17:50:21 | [diff] [blame] | 80 | if (rv != ERR_IO_PENDING) |
| 81 | FinishGenerateAuthToken(); |
| 82 | return rv; |
| 83 | } |
| 84 | |
[email protected] | 7cf4091 | 2010-12-09 18:25:03 | [diff] [blame] | 85 | bool HttpAuthHandler::NeedsIdentity() { |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | bool HttpAuthHandler::AllowsDefaultCredentials() { |
| 90 | return false; |
| 91 | } |
| 92 | |
[email protected] | 26d84b0 | 2011-08-31 14:07:08 | [diff] [blame] | 93 | bool HttpAuthHandler::AllowsExplicitCredentials() { |
| 94 | return true; |
| 95 | } |
| 96 | |
[email protected] | 7775c7d | 2010-06-21 17:50:21 | [diff] [blame] | 97 | void HttpAuthHandler::OnGenerateAuthTokenComplete(int rv) { |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 98 | CompletionCallback callback = callback_; |
[email protected] | 7775c7d | 2010-06-21 17:50:21 | [diff] [blame] | 99 | FinishGenerateAuthToken(); |
ricea | 4576c8c | 2014-11-12 18:48:25 | [diff] [blame] | 100 | DCHECK(!callback.is_null()); |
| 101 | callback.Run(rv); |
[email protected] | 7775c7d | 2010-06-21 17:50:21 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void HttpAuthHandler::FinishGenerateAuthToken() { |
Scott Graham | 949aef8 | 2017-08-23 22:36:54 | [diff] [blame] | 105 | // TODO(cbentzel): Should this be done in OK case only? |
[email protected] | b675425 | 2012-06-13 23:14:38 | [diff] [blame] | 106 | net_log_.EndEvent(EventTypeFromAuthTarget(target_)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 107 | callback_.Reset(); |
[email protected] | bcc528e | 2010-06-10 15:03:24 | [diff] [blame] | 108 | } |
| 109 | |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 110 | } // namespace net |