http_auth_handler_negotiate.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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_NEGOTIATE_H_
  5. #define NET_HTTP_HTTP_AUTH_HANDLER_NEGOTIATE_H_
  6. #include <memory>
  7. #include <string>
  8. #include <utility>
  9. #include "base/memory/raw_ptr.h"
  10. #include "build/build_config.h"
  11. #include "net/base/completion_once_callback.h"
  12. #include "net/base/net_export.h"
  13. #include "net/base/network_isolation_key.h"
  14. #include "net/dns/host_resolver.h"
  15. #include "net/http/http_auth_handler.h"
  16. #include "net/http/http_auth_handler_factory.h"
  17. #include "net/http/http_auth_mechanism.h"
  18. #if BUILDFLAG(IS_ANDROID)
  19. #include "net/android/http_auth_negotiate_android.h"
  20. #elif BUILDFLAG(IS_WIN)
  21. #include "net/http/http_auth_sspi_win.h"
  22. #elif BUILDFLAG(IS_POSIX)
  23. #include "net/http/http_auth_gssapi_posix.h"
  24. #endif
  25. namespace url {
  26. class SchemeHostPort;
  27. }
  28. namespace net {
  29. class HttpAuthPreferences;
  30. // Handler for WWW-Authenticate: Negotiate protocol.
  31. //
  32. // See http://tools.ietf.org/html/rfc4178 and http://tools.ietf.org/html/rfc4559
  33. // for more information about the protocol.
  34. class NET_EXPORT_PRIVATE HttpAuthHandlerNegotiate : public HttpAuthHandler {
  35. public:
  36. #if BUILDFLAG(IS_WIN)
  37. typedef SSPILibrary AuthLibrary;
  38. #elif BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID)
  39. typedef GSSAPILibrary AuthLibrary;
  40. #endif
  41. class NET_EXPORT_PRIVATE Factory : public HttpAuthHandlerFactory {
  42. public:
  43. explicit Factory(HttpAuthMechanismFactory negotiate_auth_system_factory);
  44. ~Factory() override;
  45. #if !BUILDFLAG(IS_ANDROID)
  46. // Sets the system library to use, thereby assuming ownership of
  47. // |auth_library|.
  48. void set_library(std::unique_ptr<AuthLibrary> auth_provider) {
  49. auth_library_ = std::move(auth_provider);
  50. }
  51. #if BUILDFLAG(IS_POSIX)
  52. const std::string& GetLibraryNameForTesting() const;
  53. #endif // BUILDFLAG(IS_POSIX)
  54. #endif // !BUILDFLAG(IS_ANDROID)
  55. // HttpAuthHandlerFactory overrides
  56. int CreateAuthHandler(HttpAuthChallengeTokenizer* challenge,
  57. HttpAuth::Target target,
  58. const SSLInfo& ssl_info,
  59. const NetworkIsolationKey& network_isolation_key,
  60. const url::SchemeHostPort& scheme_host_port,
  61. CreateReason reason,
  62. int digest_nonce_count,
  63. const NetLogWithSource& net_log,
  64. HostResolver* host_resolver,
  65. std::unique_ptr<HttpAuthHandler>* handler) override;
  66. private:
  67. HttpAuthMechanismFactory negotiate_auth_system_factory_;
  68. bool is_unsupported_ = false;
  69. #if !BUILDFLAG(IS_ANDROID)
  70. std::unique_ptr<AuthLibrary> auth_library_;
  71. #endif // !BUILDFLAG(IS_ANDROID)
  72. };
  73. HttpAuthHandlerNegotiate(std::unique_ptr<HttpAuthMechanism> auth_system,
  74. const HttpAuthPreferences* prefs,
  75. HostResolver* host_resolver);
  76. ~HttpAuthHandlerNegotiate() override;
  77. // HttpAuthHandler
  78. bool NeedsIdentity() override;
  79. bool AllowsDefaultCredentials() override;
  80. bool AllowsExplicitCredentials() override;
  81. const std::string& spn_for_testing() const { return spn_; }
  82. protected:
  83. // HttpAuthHandler
  84. bool Init(HttpAuthChallengeTokenizer* challenge,
  85. const SSLInfo& ssl_info,
  86. const NetworkIsolationKey& network_isolation_key) override;
  87. int GenerateAuthTokenImpl(const AuthCredentials* credentials,
  88. const HttpRequestInfo* request,
  89. CompletionOnceCallback callback,
  90. std::string* auth_token) override;
  91. HttpAuth::AuthorizationResult HandleAnotherChallengeImpl(
  92. HttpAuthChallengeTokenizer* challenge) override;
  93. private:
  94. enum State {
  95. STATE_RESOLVE_CANONICAL_NAME,
  96. STATE_RESOLVE_CANONICAL_NAME_COMPLETE,
  97. STATE_GENERATE_AUTH_TOKEN,
  98. STATE_GENERATE_AUTH_TOKEN_COMPLETE,
  99. STATE_NONE,
  100. };
  101. std::string CreateSPN(const std::string& server,
  102. const url::SchemeHostPort& scheme_host_port);
  103. void OnIOComplete(int result);
  104. void DoCallback(int result);
  105. int DoLoop(int result);
  106. int DoResolveCanonicalName();
  107. int DoResolveCanonicalNameComplete(int rv);
  108. int DoGenerateAuthToken();
  109. int DoGenerateAuthTokenComplete(int rv);
  110. HttpAuth::DelegationType GetDelegationType() const;
  111. std::unique_ptr<HttpAuthMechanism> auth_system_;
  112. const raw_ptr<HostResolver> resolver_;
  113. NetworkIsolationKey network_isolation_key_;
  114. // Members which are needed for DNS lookup + SPN.
  115. std::unique_ptr<HostResolver::ResolveHostRequest> resolve_host_request_;
  116. // Things which should be consistent after first call to GenerateAuthToken.
  117. bool already_called_ = false;
  118. bool has_credentials_ = false;
  119. AuthCredentials credentials_;
  120. std::string spn_;
  121. std::string channel_bindings_;
  122. // Things which vary each round.
  123. CompletionOnceCallback callback_;
  124. raw_ptr<std::string> auth_token_ = nullptr;
  125. State next_state_ = STATE_NONE;
  126. raw_ptr<const HttpAuthPreferences> http_auth_preferences_;
  127. };
  128. } // namespace net
  129. #endif // NET_HTTP_HTTP_AUTH_HANDLER_NEGOTIATE_H_