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.h"
6 #include "net/http/http_auth_handler_ntlm.h"
7
8 #include "net/base/completion_once_callback.h"
9 #include "net/base/net_errors.h"
10 #include "net/http/http_auth_mechanism.h"
11 #include "url/scheme_host_port.h"
12
13 namespace net {
14
CreateAuthHandler(HttpAuthChallengeTokenizer * challenge,HttpAuth::Target target,const SSLInfo & ssl_info,const NetworkAnonymizationKey & network_anonymization_key,const url::SchemeHostPort & scheme_host_port,CreateReason reason,int digest_nonce_count,const NetLogWithSource & net_log,HostResolver * host_resolver,std::unique_ptr<HttpAuthHandler> * handler)15 int HttpAuthHandlerNTLM::Factory::CreateAuthHandler(
16 HttpAuthChallengeTokenizer* challenge,
17 HttpAuth::Target target,
18 const SSLInfo& ssl_info,
19 const NetworkAnonymizationKey& network_anonymization_key,
20 const url::SchemeHostPort& scheme_host_port,
21 CreateReason reason,
22 int digest_nonce_count,
23 const NetLogWithSource& net_log,
24 HostResolver* host_resolver,
25 std::unique_ptr<HttpAuthHandler>* handler) {
26 if (reason == CREATE_PREEMPTIVE)
27 return ERR_UNSUPPORTED_AUTH_SCHEME;
28 // TODO(cbentzel): Move towards model of parsing in the factory
29 // method and only constructing when valid.
30 // NOTE: Default credentials are not supported for the portable implementation
31 // of NTLM.
32 auto tmp_handler =
33 std::make_unique<HttpAuthHandlerNTLM>(http_auth_preferences());
34 if (!tmp_handler->InitFromChallenge(challenge, target, ssl_info,
35 network_anonymization_key,
36 scheme_host_port, net_log)) {
37 return ERR_INVALID_RESPONSE;
38 }
39 *handler = std::move(tmp_handler);
40 return OK;
41 }
42
HttpAuthHandlerNTLM(const HttpAuthPreferences * http_auth_preferences)43 HttpAuthHandlerNTLM::HttpAuthHandlerNTLM(
44 const HttpAuthPreferences* http_auth_preferences)
45 : mechanism_(http_auth_preferences) {}
46
NeedsIdentity()47 bool HttpAuthHandlerNTLM::NeedsIdentity() {
48 return mechanism_.NeedsIdentity();
49 }
50
AllowsDefaultCredentials()51 bool HttpAuthHandlerNTLM::AllowsDefaultCredentials() {
52 // Default credentials are not supported in the portable implementation of
53 // NTLM, but are supported in the SSPI implementation.
54 return false;
55 }
56
GenerateAuthTokenImpl(const AuthCredentials * credentials,const HttpRequestInfo * request,CompletionOnceCallback callback,std::string * auth_token)57 int HttpAuthHandlerNTLM::GenerateAuthTokenImpl(
58 const AuthCredentials* credentials,
59 const HttpRequestInfo* request,
60 CompletionOnceCallback callback,
61 std::string* auth_token) {
62 return mechanism_.GenerateAuthToken(credentials, CreateSPN(scheme_host_port_),
63 channel_bindings_, auth_token, net_log(),
64 std::move(callback));
65 }
66
ParseChallenge(HttpAuthChallengeTokenizer * tok)67 HttpAuth::AuthorizationResult HttpAuthHandlerNTLM::ParseChallenge(
68 HttpAuthChallengeTokenizer* tok) {
69 return mechanism_.ParseChallenge(tok);
70 }
71
72 HttpAuthHandlerNTLM::~HttpAuthHandlerNTLM() = default;
73
74 } // namespace net
75