1 // Copyright 2019 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 #ifndef NET_HTTP_HTTP_AUTH_NTLM_MECHANISM_H_ 6 #define NET_HTTP_HTTP_AUTH_NTLM_MECHANISM_H_ 7 8 #include <stdint.h> 9 10 #include <string> 11 12 #include "net/base/auth.h" 13 #include "net/base/net_export.h" 14 #include "net/http/http_auth_mechanism.h" 15 #include "net/ntlm/ntlm_client.h" 16 17 namespace net { 18 19 class NET_EXPORT_PRIVATE HttpAuthNtlmMechanism : public HttpAuthMechanism { 20 public: 21 explicit HttpAuthNtlmMechanism(const HttpAuthPreferences* preferences); 22 ~HttpAuthNtlmMechanism() override; 23 24 HttpAuthNtlmMechanism(const HttpAuthNtlmMechanism&) = delete; 25 HttpAuthNtlmMechanism& operator=(const HttpAuthNtlmMechanism&) = delete; 26 27 // A function that returns the time as the number of 100 nanosecond ticks 28 // since Jan 1, 1601 (UTC). 29 using GetMSTimeProc = uint64_t (*)(); 30 31 // A function that generates n random bytes in the output buffer. 32 using GenerateRandomProc = void (*)(uint8_t* output, size_t n); 33 34 // A function that returns the local host name. Returns an empty string if 35 // the local host name is not available. 36 using HostNameProc = std::string (*)(); 37 38 // For unit tests to override and restore the GenerateRandom and 39 // GetHostName functions. 40 class ScopedProcSetter { 41 public: 42 ScopedProcSetter(GetMSTimeProc ms_time_proc, 43 GenerateRandomProc random_proc, 44 HostNameProc host_name_proc); 45 ~ScopedProcSetter(); 46 47 ScopedProcSetter(const ScopedProcSetter&) = delete; 48 ScopedProcSetter& operator=(const ScopedProcSetter&) = delete; 49 50 private: 51 GetMSTimeProc old_ms_time_proc_; 52 GenerateRandomProc old_random_proc_; 53 HostNameProc old_host_name_proc_; 54 }; 55 56 // HttpAuthMechanism 57 bool Init(const NetLogWithSource& net_log) override; 58 bool NeedsIdentity() const override; 59 bool AllowsExplicitCredentials() const override; 60 HttpAuth::AuthorizationResult ParseChallenge( 61 HttpAuthChallengeTokenizer* tok) override; 62 int GenerateAuthToken(const AuthCredentials* credentials, 63 const std::string& spn, 64 const std::string& channel_bindings, 65 std::string* auth_token, 66 const NetLogWithSource& net_log, 67 CompletionOnceCallback callback) override; 68 void SetDelegation(HttpAuth::DelegationType delegation_type) override; 69 70 private: 71 ntlm::NtlmClient ntlm_client_; 72 73 // Decoded authentication token that the server returned as part of an NTLM 74 // challenge. 75 std::string challenge_token_; 76 77 // Keep track of whether we sent the negotiate token. While it is still spec 78 // compliant to respond to any challenge without a token with a negotiate 79 // token, this mechanism considers it an error to respond to a negotiate token 80 // with an empty token. 81 bool first_token_sent_ = false; 82 }; 83 84 } // namespace net 85 86 #endif // NET_HTTP_HTTP_AUTH_NTLM_MECHANISM_H_ 87