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"
11 #include "net/http/http_auth_challenge_tokenizer.h"
15 HttpAuthHandler::HttpAuthHandler()
16 : auth_scheme_(HttpAuth::AUTH_SCHEME_MAX
),
18 target_(HttpAuth::AUTH_NONE
),
22 HttpAuthHandler::~HttpAuthHandler() {
25 bool HttpAuthHandler::InitFromChallenge(
26 HttpAuthChallengeTokenizer
* challenge
,
27 HttpAuth::Target target
,
29 const BoundNetLog
& net_log
) {
36 auth_challenge_
= challenge
->challenge_text();
37 bool ok
= Init(challenge
);
39 // Init() is expected to set the scheme, realm, score, and properties. The
40 // realm may be empty.
41 DCHECK(!ok
|| score_
!= -1);
42 DCHECK(!ok
|| properties_
!= -1);
43 DCHECK(!ok
|| auth_scheme_
!= HttpAuth::AUTH_SCHEME_MAX
);
50 NetLog::EventType
EventTypeFromAuthTarget(HttpAuth::Target target
) {
52 case HttpAuth::AUTH_PROXY
:
53 return NetLog::TYPE_AUTH_PROXY
;
54 case HttpAuth::AUTH_SERVER
:
55 return NetLog::TYPE_AUTH_SERVER
;
58 return NetLog::TYPE_CANCELLED
;
64 int HttpAuthHandler::GenerateAuthToken(
65 const AuthCredentials
* credentials
, const HttpRequestInfo
* request
,
66 const CompletionCallback
& callback
, std::string
* auth_token
) {
67 // TODO(cbentzel): Enforce non-NULL callback after cleaning up SocketStream.
69 DCHECK(credentials
!= NULL
|| AllowsDefaultCredentials());
70 DCHECK(auth_token
!= NULL
);
71 DCHECK(callback_
.is_null());
73 net_log_
.BeginEvent(EventTypeFromAuthTarget(target_
));
74 int rv
= GenerateAuthTokenImpl(
76 base::Bind(&HttpAuthHandler::OnGenerateAuthTokenComplete
,
77 base::Unretained(this)),
79 if (rv
!= ERR_IO_PENDING
)
80 FinishGenerateAuthToken();
84 bool HttpAuthHandler::NeedsIdentity() {
88 bool HttpAuthHandler::AllowsDefaultCredentials() {
92 bool HttpAuthHandler::AllowsExplicitCredentials() {
96 void HttpAuthHandler::OnGenerateAuthTokenComplete(int rv
) {
97 CompletionCallback callback
= callback_
;
98 FinishGenerateAuthToken();
99 if (!callback
.is_null())
103 void HttpAuthHandler::FinishGenerateAuthToken() {
104 // TOOD(cbentzel): Should this be done in OK case only?
105 net_log_
.EndEvent(EventTypeFromAuthTarget(target_
));