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 "net/http/http_auth_handler.h"
8 #include "base/bind_helpers.h"
9 #include "base/logging.h"
10 #include "net/base/net_errors.h"
14 HttpAuthHandler::HttpAuthHandler()
15 : auth_scheme_(HttpAuth::AUTH_SCHEME_MAX
),
17 target_(HttpAuth::AUTH_NONE
),
21 HttpAuthHandler::~HttpAuthHandler() {
24 bool HttpAuthHandler::InitFromChallenge(
25 HttpAuth::ChallengeTokenizer
* challenge
,
26 HttpAuth::Target target
,
28 const BoundNetLog
& net_log
) {
35 auth_challenge_
= challenge
->challenge_text();
36 bool ok
= Init(challenge
);
38 // Init() is expected to set the scheme, realm, score, and properties. The
39 // realm may be empty.
40 DCHECK(!ok
|| score_
!= -1);
41 DCHECK(!ok
|| properties_
!= -1);
42 DCHECK(!ok
|| auth_scheme_
!= HttpAuth::AUTH_SCHEME_MAX
);
49 NetLog::EventType
EventTypeFromAuthTarget(HttpAuth::Target target
) {
51 case HttpAuth::AUTH_PROXY
:
52 return NetLog::TYPE_AUTH_PROXY
;
53 case HttpAuth::AUTH_SERVER
:
54 return NetLog::TYPE_AUTH_SERVER
;
57 return NetLog::TYPE_CANCELLED
;
63 int HttpAuthHandler::GenerateAuthToken(
64 const AuthCredentials
* credentials
, const HttpRequestInfo
* request
,
65 const CompletionCallback
& callback
, std::string
* auth_token
) {
66 // TODO(cbentzel): Enforce non-NULL callback after cleaning up SocketStream.
68 DCHECK(credentials
!= NULL
|| AllowsDefaultCredentials());
69 DCHECK(auth_token
!= NULL
);
70 DCHECK(callback_
.is_null());
72 net_log_
.BeginEvent(EventTypeFromAuthTarget(target_
));
73 int rv
= GenerateAuthTokenImpl(
75 base::Bind(&HttpAuthHandler::OnGenerateAuthTokenComplete
,
76 base::Unretained(this)),
78 if (rv
!= ERR_IO_PENDING
)
79 FinishGenerateAuthToken();
83 bool HttpAuthHandler::NeedsIdentity() {
87 bool HttpAuthHandler::AllowsDefaultCredentials() {
91 bool HttpAuthHandler::AllowsExplicitCredentials() {
95 void HttpAuthHandler::OnGenerateAuthTokenComplete(int rv
) {
96 CompletionCallback callback
= callback_
;
97 FinishGenerateAuthToken();
98 if (!callback
.is_null())
102 void HttpAuthHandler::FinishGenerateAuthToken() {
103 // TOOD(cbentzel): Should this be done in OK case only?
104 net_log_
.EndEvent(EventTypeFromAuthTarget(target_
));