http_auth_handler_ntlm.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #ifndef NET_HTTP_HTTP_AUTH_HANDLER_NTLM_H_
  5. #define NET_HTTP_HTTP_AUTH_HANDLER_NTLM_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include "base/memory/raw_ptr.h"
  9. #include "build/build_config.h"
  10. // This contains the portable and the SSPI implementations for NTLM.
  11. // We use NTLM_SSPI for Windows, and NTLM_PORTABLE for other platforms.
  12. #if BUILDFLAG(IS_WIN)
  13. #define NTLM_SSPI
  14. #else
  15. #define NTLM_PORTABLE
  16. #endif
  17. #if defined(NTLM_SSPI)
  18. #include "net/http/http_auth_sspi_win.h"
  19. #elif defined(NTLM_PORTABLE)
  20. #include "net/http/http_auth_ntlm_mechanism.h"
  21. #endif
  22. #include <memory>
  23. #include <string>
  24. #include <vector>
  25. #include "net/base/completion_once_callback.h"
  26. #include "net/base/net_export.h"
  27. #include "net/http/http_auth_handler.h"
  28. #include "net/http/http_auth_handler_factory.h"
  29. namespace url {
  30. class SchemeHostPort;
  31. }
  32. namespace net {
  33. class HttpAuthPreferences;
  34. // Code for handling HTTP NTLM authentication.
  35. class NET_EXPORT_PRIVATE HttpAuthHandlerNTLM : public HttpAuthHandler {
  36. public:
  37. class Factory : public HttpAuthHandlerFactory {
  38. public:
  39. Factory();
  40. Factory(const Factory&) = delete;
  41. Factory& operator=(const Factory&) = delete;
  42. ~Factory() override;
  43. int CreateAuthHandler(HttpAuthChallengeTokenizer* challenge,
  44. HttpAuth::Target target,
  45. const SSLInfo& ssl_info,
  46. const NetworkIsolationKey& network_isolation_key,
  47. const url::SchemeHostPort& scheme_host_port,
  48. CreateReason reason,
  49. int digest_nonce_count,
  50. const NetLogWithSource& net_log,
  51. HostResolver* host_resolver,
  52. std::unique_ptr<HttpAuthHandler>* handler) override;
  53. #if defined(NTLM_SSPI)
  54. // Set the SSPILibrary to use. Typically the only callers which need to use
  55. // this are unit tests which pass in a mocked-out version of the SSPI
  56. // library. After the call |sspi_library| will be owned by this Factory and
  57. // will be destroyed when the Factory is destroyed.
  58. void set_sspi_library(std::unique_ptr<SSPILibrary> sspi_library) {
  59. sspi_library_ = std::move(sspi_library);
  60. }
  61. #endif // defined(NTLM_SSPI)
  62. private:
  63. #if defined(NTLM_SSPI)
  64. std::unique_ptr<SSPILibrary> sspi_library_;
  65. #endif // defined(NTLM_SSPI)
  66. };
  67. #if defined(NTLM_PORTABLE)
  68. explicit HttpAuthHandlerNTLM(
  69. const HttpAuthPreferences* http_auth_preferences);
  70. #endif
  71. #if defined(NTLM_SSPI)
  72. HttpAuthHandlerNTLM(SSPILibrary* sspi_library,
  73. const HttpAuthPreferences* http_auth_preferences);
  74. #endif
  75. HttpAuthHandlerNTLM(const HttpAuthHandlerNTLM&) = delete;
  76. HttpAuthHandlerNTLM& operator=(const HttpAuthHandlerNTLM&) = delete;
  77. ~HttpAuthHandlerNTLM() override;
  78. // HttpAuthHandler
  79. bool NeedsIdentity() override;
  80. bool AllowsDefaultCredentials() override;
  81. protected:
  82. // HttpAuthHandler
  83. bool Init(HttpAuthChallengeTokenizer* tok,
  84. const SSLInfo& ssl_info,
  85. const NetworkIsolationKey& network_isolation_key) override;
  86. int GenerateAuthTokenImpl(const AuthCredentials* credentials,
  87. const HttpRequestInfo* request,
  88. CompletionOnceCallback callback,
  89. std::string* auth_token) override;
  90. HttpAuth::AuthorizationResult HandleAnotherChallengeImpl(
  91. HttpAuthChallengeTokenizer* challenge) override;
  92. private:
  93. // Parse the challenge, saving the results into this instance.
  94. HttpAuth::AuthorizationResult ParseChallenge(HttpAuthChallengeTokenizer* tok);
  95. // Create an NTLM SPN to identify the |scheme_host_port| server.
  96. static std::string CreateSPN(const url::SchemeHostPort& scheme_host_port);
  97. #if defined(NTLM_SSPI)
  98. HttpAuthSSPI mechanism_;
  99. raw_ptr<const HttpAuthPreferences> http_auth_preferences_;
  100. #elif defined(NTLM_PORTABLE)
  101. HttpAuthNtlmMechanism mechanism_;
  102. #endif
  103. std::string channel_bindings_;
  104. };
  105. } // namespace net
  106. #endif // NET_HTTP_HTTP_AUTH_HANDLER_NTLM_H_