1 // Copyright 2012 The Chromium Authors
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.h"
6
7 #include <utility>
8
9 #include "base/check_op.h"
10 #include "base/functional/bind.h"
11 #include "base/functional/callback_helpers.h"
12 #include "net/base/net_errors.h"
13 #include "net/http/http_auth_challenge_tokenizer.h"
14 #include "net/log/net_log.h"
15 #include "net/log/net_log_event_type.h"
16
17 namespace net {
18
19 HttpAuthHandler::HttpAuthHandler() = default;
20
21 HttpAuthHandler::~HttpAuthHandler() = default;
22
InitFromChallenge(HttpAuthChallengeTokenizer * challenge,HttpAuth::Target target,const SSLInfo & ssl_info,const NetworkAnonymizationKey & network_anonymization_key,const url::SchemeHostPort & scheme_host_port,const NetLogWithSource & net_log)23 bool HttpAuthHandler::InitFromChallenge(
24 HttpAuthChallengeTokenizer* challenge,
25 HttpAuth::Target target,
26 const SSLInfo& ssl_info,
27 const NetworkAnonymizationKey& network_anonymization_key,
28 const url::SchemeHostPort& scheme_host_port,
29 const NetLogWithSource& net_log) {
30 scheme_host_port_ = scheme_host_port;
31 target_ = target;
32 score_ = -1;
33 properties_ = -1;
34 net_log_ = net_log;
35
36 auth_challenge_ = challenge->challenge_text();
37 net_log_.BeginEvent(NetLogEventType::AUTH_HANDLER_INIT);
38 bool ok = Init(challenge, ssl_info, network_anonymization_key);
39 net_log_.EndEvent(NetLogEventType::AUTH_HANDLER_INIT, [&]() {
40 base::Value::Dict params;
41 params.Set("succeeded", ok);
42 params.Set("allows_default_credentials", AllowsDefaultCredentials());
43 return params;
44 });
45
46 // Init() is expected to set the scheme, realm, score, and properties. The
47 // realm may be empty.
48 DCHECK(!ok || score_ != -1);
49 DCHECK(!ok || properties_ != -1);
50 DCHECK(!ok || auth_scheme_ != HttpAuth::AUTH_SCHEME_MAX);
51
52 return ok;
53 }
54
GenerateAuthToken(const AuthCredentials * credentials,const HttpRequestInfo * request,CompletionOnceCallback callback,std::string * auth_token)55 int HttpAuthHandler::GenerateAuthToken(const AuthCredentials* credentials,
56 const HttpRequestInfo* request,
57 CompletionOnceCallback callback,
58 std::string* auth_token) {
59 DCHECK(!callback.is_null());
60 DCHECK(request);
61 DCHECK(credentials != nullptr || AllowsDefaultCredentials());
62 DCHECK(auth_token != nullptr);
63 DCHECK(callback_.is_null());
64 callback_ = std::move(callback);
65 net_log_.BeginEvent(NetLogEventType::AUTH_GENERATE_TOKEN);
66 int rv = GenerateAuthTokenImpl(
67 credentials, request,
68 base::BindOnce(&HttpAuthHandler::OnGenerateAuthTokenComplete,
69 base::Unretained(this)),
70 auth_token);
71 if (rv != ERR_IO_PENDING)
72 FinishGenerateAuthToken(rv);
73 return rv;
74 }
75
NeedsIdentity()76 bool HttpAuthHandler::NeedsIdentity() {
77 return true;
78 }
79
AllowsDefaultCredentials()80 bool HttpAuthHandler::AllowsDefaultCredentials() {
81 return false;
82 }
83
AllowsExplicitCredentials()84 bool HttpAuthHandler::AllowsExplicitCredentials() {
85 return true;
86 }
87
OnGenerateAuthTokenComplete(int rv)88 void HttpAuthHandler::OnGenerateAuthTokenComplete(int rv) {
89 CompletionOnceCallback callback = std::move(callback_);
90 FinishGenerateAuthToken(rv);
91 DCHECK(!callback.is_null());
92 std::move(callback).Run(rv);
93 }
94
FinishGenerateAuthToken(int rv)95 void HttpAuthHandler::FinishGenerateAuthToken(int rv) {
96 DCHECK_NE(rv, ERR_IO_PENDING);
97 net_log_.EndEventWithNetErrorCode(NetLogEventType::AUTH_GENERATE_TOKEN, rv);
98 callback_.Reset();
99 }
100
HandleAnotherChallenge(HttpAuthChallengeTokenizer * challenge)101 HttpAuth::AuthorizationResult HttpAuthHandler::HandleAnotherChallenge(
102 HttpAuthChallengeTokenizer* challenge) {
103 auto authorization_result = HandleAnotherChallengeImpl(challenge);
104 net_log_.AddEvent(NetLogEventType::AUTH_HANDLE_CHALLENGE, [&] {
105 return HttpAuth::NetLogAuthorizationResultParams("authorization_result",
106 authorization_result);
107 });
108 return authorization_result;
109 }
110
111 } // namespace net
112