blob: efd8c5e43998645f2e44797834316d0f400af05d [file] [log] [blame]
[email protected]ac3fa8e22010-02-05 19:13:291// Copyright (c) 2010 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
5#include "net/http/http_auth_handler_basic.h"
6
[email protected]978df342009-11-24 06:21:537#include <string>
8
9#include "base/base64.h"
[email protected]c3b35c22008-09-27 03:19:4210#include "base/string_util.h"
[email protected]2f4a6c52010-03-08 21:08:1811#include "base/utf_string_conversions.h"
[email protected]ac3fa8e22010-02-05 19:13:2912#include "net/base/net_errors.h"
[email protected]c3b35c22008-09-27 03:19:4213#include "net/http/http_auth.h"
[email protected]c3b35c22008-09-27 03:19:4214
15namespace net {
16
[email protected]22927ad2009-09-21 19:56:1917// 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]fa55e192010-02-15 14:25:5025bool HttpAuthHandlerBasic::Init(HttpAuth::ChallengeTokenizer* challenge) {
[email protected]c3b35c22008-09-27 03:19:4226 scheme_ = "basic";
27 score_ = 1;
[email protected]3f918782009-02-28 01:29:2428 properties_ = 0;
[email protected]c3b35c22008-09-27 03:19:4229
30 // Verify the challenge's auth-scheme.
[email protected]fa55e192010-02-15 14:25:5031 if (!challenge->valid() ||
32 !LowerCaseEqualsASCII(challenge->scheme(), "basic"))
[email protected]c3b35c22008-09-27 03:19:4233 return false;
34
[email protected]22927ad2009-09-21 19:56:1935 // Extract the realm (may be missing).
[email protected]fa55e192010-02-15 14:25:5036 while (challenge->GetNext()) {
37 if (LowerCaseEqualsASCII(challenge->name(), "realm"))
38 realm_ = challenge->unquoted_value();
[email protected]c3b35c22008-09-27 03:19:4239 }
40
[email protected]fa55e192010-02-15 14:25:5041 return challenge->valid();
[email protected]c3b35c22008-09-27 03:19:4242}
43
[email protected]bcc528e2010-06-10 15:03:2444int HttpAuthHandlerBasic::GenerateAuthTokenImpl(
45 const std::wstring* username,
46 const std::wstring* password,
[email protected]c3b35c22008-09-27 03:19:4247 const HttpRequestInfo*,
[email protected]bcc528e2010-06-10 15:03:2448 CompletionCallback*,
[email protected]ac3fa8e22010-02-05 19:13:2949 std::string* auth_token) {
[email protected]c3b35c22008-09-27 03:19:4250 // TODO(eroman): is this the right encoding of username/password?
51 std::string base64_username_password;
[email protected]bcc528e2010-06-10 15:03:2452 if (!base::Base64Encode(WideToUTF8(*username) + ":" + WideToUTF8(*password),
[email protected]ac3fa8e22010-02-05 19:13:2953 &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]c3b35c22008-09-27 03:19:4259}
60
[email protected]fa55e192010-02-15 14:25:5061HttpAuthHandlerBasic::Factory::Factory() {
62}
63
64HttpAuthHandlerBasic::Factory::~Factory() {
65}
66
67int HttpAuthHandlerBasic::Factory::CreateAuthHandler(
68 HttpAuth::ChallengeTokenizer* challenge,
69 HttpAuth::Target target,
70 const GURL& origin,
[email protected]fa82f932010-05-20 11:09:2471 CreateReason reason,
72 int digest_nonce_count,
[email protected]ac5c06e2010-05-27 15:07:3873 const BoundNetLog& net_log,
[email protected]36c8e5f72010-06-07 14:17:1474 scoped_ptr<HttpAuthHandler>* handler) {
[email protected]fa55e192010-02-15 14:25:5075 // TODO(cbentzel): Move towards model of parsing in the factory
76 // method and only constructing when valid.
[email protected]36c8e5f72010-06-07 14:17:1477 scoped_ptr<HttpAuthHandler> tmp_handler(new HttpAuthHandlerBasic());
[email protected]ac5c06e2010-05-27 15:07:3878 if (!tmp_handler->InitFromChallenge(challenge, target, origin, net_log))
[email protected]fa55e192010-02-15 14:25:5079 return ERR_INVALID_RESPONSE;
80 handler->swap(tmp_handler);
81 return OK;
82}
[email protected]ac3fa8e22010-02-05 19:13:2983
[email protected]c3b35c22008-09-27 03:19:4284} // namespace net