http_auth_handler_ntlm_win.cc 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright (c) 2011 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.
  4. // See "SSPI Sample Application" at
  5. // http://msdn.microsoft.com/en-us/library/aa918273.aspx
  6. // and "NTLM Security Support Provider" at
  7. // http://msdn.microsoft.com/en-us/library/aa923611.aspx.
  8. #include "net/http/http_auth_handler_ntlm.h"
  9. #include "base/strings/string_util.h"
  10. #include "net/base/net_errors.h"
  11. #include "net/dns/host_resolver.h"
  12. #include "net/http/http_auth.h"
  13. #include "net/http/http_auth_preferences.h"
  14. #include "net/http/http_auth_sspi_win.h"
  15. namespace net {
  16. int HttpAuthHandlerNTLM::Factory::CreateAuthHandler(
  17. HttpAuthChallengeTokenizer* challenge,
  18. HttpAuth::Target target,
  19. const SSLInfo& ssl_info,
  20. const NetworkIsolationKey& network_isolation_key,
  21. const url::SchemeHostPort& scheme_host_port,
  22. CreateReason reason,
  23. int digest_nonce_count,
  24. const NetLogWithSource& net_log,
  25. HostResolver* host_resolver,
  26. std::unique_ptr<HttpAuthHandler>* handler) {
  27. if (reason == CREATE_PREEMPTIVE)
  28. return ERR_UNSUPPORTED_AUTH_SCHEME;
  29. // TODO(cbentzel): Move towards model of parsing in the factory
  30. // method and only constructing when valid.
  31. auto tmp_handler = std::make_unique<HttpAuthHandlerNTLM>(
  32. sspi_library_.get(), http_auth_preferences());
  33. if (!tmp_handler->InitFromChallenge(challenge, target, ssl_info,
  34. network_isolation_key, scheme_host_port,
  35. net_log))
  36. return ERR_INVALID_RESPONSE;
  37. *handler = std::move(tmp_handler);
  38. return OK;
  39. }
  40. HttpAuthHandlerNTLM::HttpAuthHandlerNTLM(
  41. SSPILibrary* sspi_library,
  42. const HttpAuthPreferences* http_auth_preferences)
  43. : mechanism_(sspi_library, HttpAuth::AUTH_SCHEME_NTLM),
  44. http_auth_preferences_(http_auth_preferences) {}
  45. int HttpAuthHandlerNTLM::GenerateAuthTokenImpl(
  46. const AuthCredentials* credentials,
  47. const HttpRequestInfo* request,
  48. CompletionOnceCallback callback,
  49. std::string* auth_token) {
  50. return mechanism_.GenerateAuthToken(credentials, CreateSPN(scheme_host_port_),
  51. channel_bindings_, auth_token, net_log(),
  52. std::move(callback));
  53. }
  54. HttpAuthHandlerNTLM::~HttpAuthHandlerNTLM() = default;
  55. // Require identity on first pass instead of second.
  56. bool HttpAuthHandlerNTLM::NeedsIdentity() {
  57. return mechanism_.NeedsIdentity();
  58. }
  59. bool HttpAuthHandlerNTLM::AllowsDefaultCredentials() {
  60. if (target_ == HttpAuth::AUTH_PROXY)
  61. return true;
  62. if (!http_auth_preferences_)
  63. return false;
  64. return http_auth_preferences_->CanUseDefaultCredentials(scheme_host_port_);
  65. }
  66. HttpAuth::AuthorizationResult HttpAuthHandlerNTLM::ParseChallenge(
  67. HttpAuthChallengeTokenizer* tok) {
  68. return mechanism_.ParseChallenge(tok);
  69. }
  70. } // namespace net