[email protected] | ac3fa8e2 | 2010-02-05 19:13:29 | [diff] [blame] | 1 | // Copyright (c) 2010 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 | |
| 5 | #include "net/http/http_auth_handler_basic.h" |
| 6 | |
[email protected] | 978df34 | 2009-11-24 06:21:53 | [diff] [blame] | 7 | #include <string> |
| 8 | |
| 9 | #include "base/base64.h" |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 10 | #include "base/string_util.h" |
[email protected] | 2f4a6c5 | 2010-03-08 21:08:18 | [diff] [blame] | 11 | #include "base/utf_string_conversions.h" |
[email protected] | ac3fa8e2 | 2010-02-05 19:13:29 | [diff] [blame] | 12 | #include "net/base/net_errors.h" |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 13 | #include "net/http/http_auth.h" |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 14 | |
| 15 | namespace net { |
| 16 | |
[email protected] | 22927ad | 2009-09-21 19:56:19 | [diff] [blame] | 17 | // Note that if a realm was not specified, we will default it to ""; |
| 18 | // so specifying 'Basic realm=""' is equivalent to 'Basic'. |
| 19 | // |
| 20 | // This is more generous than RFC 2617, which is pretty clear in the |
| 21 | // production of challenge that realm is required. |
| 22 | // |
| 23 | // We allow it to be compatibility with certain embedded webservers that don't |
| 24 | // include a realm (see http://crbug.com/20984.) |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 25 | bool HttpAuthHandlerBasic::Init(HttpAuth::ChallengeTokenizer* challenge) { |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 26 | scheme_ = "basic"; |
| 27 | score_ = 1; |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 28 | properties_ = 0; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 29 | |
| 30 | // Verify the challenge's auth-scheme. |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 31 | if (!challenge->valid() || |
| 32 | !LowerCaseEqualsASCII(challenge->scheme(), "basic")) |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 33 | return false; |
| 34 | |
[email protected] | 22927ad | 2009-09-21 19:56:19 | [diff] [blame] | 35 | // Extract the realm (may be missing). |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 36 | while (challenge->GetNext()) { |
| 37 | if (LowerCaseEqualsASCII(challenge->name(), "realm")) |
| 38 | realm_ = challenge->unquoted_value(); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 39 | } |
| 40 | |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 41 | return challenge->valid(); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 42 | } |
| 43 | |
[email protected] | bcc528e | 2010-06-10 15:03:24 | [diff] [blame] | 44 | int HttpAuthHandlerBasic::GenerateAuthTokenImpl( |
| 45 | const std::wstring* username, |
| 46 | const std::wstring* password, |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 47 | const HttpRequestInfo*, |
[email protected] | bcc528e | 2010-06-10 15:03:24 | [diff] [blame] | 48 | CompletionCallback*, |
[email protected] | ac3fa8e2 | 2010-02-05 19:13:29 | [diff] [blame] | 49 | std::string* auth_token) { |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 50 | // TODO(eroman): is this the right encoding of username/password? |
| 51 | std::string base64_username_password; |
[email protected] | bcc528e | 2010-06-10 15:03:24 | [diff] [blame] | 52 | if (!base::Base64Encode(WideToUTF8(*username) + ":" + WideToUTF8(*password), |
[email protected] | ac3fa8e2 | 2010-02-05 19:13:29 | [diff] [blame] | 53 | &base64_username_password)) { |
| 54 | LOG(ERROR) << "Unexpected problem Base64 encoding."; |
| 55 | return ERR_UNEXPECTED; |
| 56 | } |
| 57 | *auth_token = "Basic " + base64_username_password; |
| 58 | return OK; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 59 | } |
| 60 | |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 61 | HttpAuthHandlerBasic::Factory::Factory() { |
| 62 | } |
| 63 | |
| 64 | HttpAuthHandlerBasic::Factory::~Factory() { |
| 65 | } |
| 66 | |
| 67 | int HttpAuthHandlerBasic::Factory::CreateAuthHandler( |
| 68 | HttpAuth::ChallengeTokenizer* challenge, |
| 69 | HttpAuth::Target target, |
| 70 | const GURL& origin, |
[email protected] | fa82f93 | 2010-05-20 11:09:24 | [diff] [blame] | 71 | CreateReason reason, |
| 72 | int digest_nonce_count, |
[email protected] | ac5c06e | 2010-05-27 15:07:38 | [diff] [blame] | 73 | const BoundNetLog& net_log, |
[email protected] | 36c8e5f7 | 2010-06-07 14:17:14 | [diff] [blame] | 74 | scoped_ptr<HttpAuthHandler>* handler) { |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 75 | // TODO(cbentzel): Move towards model of parsing in the factory |
| 76 | // method and only constructing when valid. |
[email protected] | 36c8e5f7 | 2010-06-07 14:17:14 | [diff] [blame] | 77 | scoped_ptr<HttpAuthHandler> tmp_handler(new HttpAuthHandlerBasic()); |
[email protected] | ac5c06e | 2010-05-27 15:07:38 | [diff] [blame] | 78 | if (!tmp_handler->InitFromChallenge(challenge, target, origin, net_log)) |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 79 | return ERR_INVALID_RESPONSE; |
| 80 | handler->swap(tmp_handler); |
| 81 | return OK; |
| 82 | } |
[email protected] | ac3fa8e2 | 2010-02-05 19:13:29 | [diff] [blame] | 83 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 84 | } // namespace net |