OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_HTTP_HTTP_AUTH_HANDLER_NTLM_H_ | 5 #ifndef NET_HTTP_HTTP_AUTH_HANDLER_NTLM_H_ |
6 #define NET_HTTP_HTTP_AUTH_HANDLER_NTLM_H_ | 6 #define NET_HTTP_HTTP_AUTH_HANDLER_NTLM_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/scoped_ptr.h" | 11 #include "base/scoped_ptr.h" |
| 12 #include "base/string16.h" |
12 #include "net/http/http_auth_handler.h" | 13 #include "net/http/http_auth_handler.h" |
13 | 14 |
14 namespace net { | 15 namespace net { |
15 | 16 |
16 class NTLMAuthModule; | 17 class NTLMAuthModule; |
17 | 18 |
18 // Code for handling HTTP NTLM authentication. | 19 // Code for handling HTTP NTLM authentication. |
19 class HttpAuthHandlerNTLM : public HttpAuthHandler { | 20 class HttpAuthHandlerNTLM : public HttpAuthHandler { |
20 public: | 21 public: |
21 // A function that generates n random bytes in the output buffer. | 22 // A function that generates n random bytes in the output buffer. |
22 typedef void (*GenerateRandomProc)(uint8* output, size_t n); | 23 typedef void (*GenerateRandomProc)(uint8* output, size_t n); |
23 | 24 |
24 // A function that returns the local host name as a null-terminated string | 25 // A function that returns the local host name. Returns an empty string if |
25 // in the output buffer. Returns an empty string if the local host name is | 26 // the local host name is not available. |
26 // not available. | 27 typedef std::string (*HostNameProc)(); |
27 // TODO(wtc): return a std::string instead. | 28 |
28 typedef void (*HostNameProc)(char* name, size_t namelen); | 29 // For unit tests to override and restore the GenerateRandom and |
| 30 // GetHostName functions. |
| 31 class ScopedProcSetter { |
| 32 public: |
| 33 ScopedProcSetter(GenerateRandomProc random_proc, |
| 34 HostNameProc host_name_proc) { |
| 35 old_random_proc_ = SetGenerateRandomProc(random_proc); |
| 36 old_host_name_proc_ = SetHostNameProc(host_name_proc); |
| 37 } |
| 38 |
| 39 ~ScopedProcSetter() { |
| 40 SetGenerateRandomProc(old_random_proc_); |
| 41 SetHostNameProc(old_host_name_proc_); |
| 42 } |
| 43 |
| 44 private: |
| 45 GenerateRandomProc old_random_proc_; |
| 46 HostNameProc old_host_name_proc_; |
| 47 }; |
29 | 48 |
30 HttpAuthHandlerNTLM(); | 49 HttpAuthHandlerNTLM(); |
31 | 50 |
32 virtual ~HttpAuthHandlerNTLM(); | 51 virtual ~HttpAuthHandlerNTLM(); |
33 | 52 |
34 virtual bool NeedsIdentity(); | 53 virtual bool NeedsIdentity(); |
35 | 54 |
36 virtual std::string GenerateCredentials(const std::wstring& username, | 55 virtual std::string GenerateCredentials(const std::wstring& username, |
37 const std::wstring& password, | 56 const std::wstring& password, |
38 const HttpRequestInfo* request, | 57 const HttpRequestInfo* request, |
39 const ProxyInfo* proxy); | 58 const ProxyInfo* proxy); |
40 | 59 |
41 // For unit tests to override the GenerateRandom and GetHostName functions. | |
42 static void SetGenerateRandomProc(GenerateRandomProc proc); | |
43 static void SetHostNameProc(HostNameProc proc); | |
44 | |
45 protected: | 60 protected: |
46 virtual bool Init(std::string::const_iterator challenge_begin, | 61 virtual bool Init(std::string::const_iterator challenge_begin, |
47 std::string::const_iterator challenge_end) { | 62 std::string::const_iterator challenge_end) { |
48 return ParseChallenge(challenge_begin, challenge_end); | 63 return ParseChallenge(challenge_begin, challenge_end); |
49 } | 64 } |
50 | 65 |
51 private: | 66 private: |
| 67 // For unit tests to override the GenerateRandom and GetHostName functions. |
| 68 // Returns the old function. |
| 69 static GenerateRandomProc SetGenerateRandomProc(GenerateRandomProc proc); |
| 70 static HostNameProc SetHostNameProc(HostNameProc proc); |
| 71 |
52 // Parse the challenge, saving the results into this instance. | 72 // Parse the challenge, saving the results into this instance. |
53 // Returns true on success. | 73 // Returns true on success. |
54 bool ParseChallenge(std::string::const_iterator challenge_begin, | 74 bool ParseChallenge(std::string::const_iterator challenge_begin, |
55 std::string::const_iterator challenge_end); | 75 std::string::const_iterator challenge_end); |
56 | 76 |
57 // The actual implementation of NTLM. | 77 // Given an input token received from the server, generate the next output |
58 // | 78 // token to be sent to the server. |
59 // TODO(wtc): This artificial separation of the NTLM auth module from the | 79 int GetNextToken(const void* in_token, |
60 // NTLM auth handler comes from the Mozilla code. It is due to an | 80 uint32 in_token_len, |
61 // architecture constraint of Mozilla's (all crypto code must reside in the | 81 void** out_token, |
62 // "PSM" component), so that the NTLM code, which does crypto, must be | 82 uint32* out_token_len); |
63 // separated from the "netwerk" component. Our source tree doesn't have | 83 |
64 // this constraint, so we may want to merge NTLMAuthModule into this class. | 84 static GenerateRandomProc generate_random_proc_; |
65 scoped_ptr<NTLMAuthModule> ntlm_module_; | 85 static HostNameProc get_host_name_proc_; |
| 86 |
| 87 string16 domain_; |
| 88 string16 username_; |
| 89 string16 password_; |
66 | 90 |
67 // The base64-encoded string following "NTLM" in the "WWW-Authenticate" or | 91 // The base64-encoded string following "NTLM" in the "WWW-Authenticate" or |
68 // "Proxy-Authenticate" response header. | 92 // "Proxy-Authenticate" response header. |
69 std::string auth_data_; | 93 std::string auth_data_; |
70 }; | 94 }; |
71 | 95 |
72 } // namespace net | 96 } // namespace net |
73 | 97 |
74 #endif // NET_HTTP_HTTP_AUTH_HANDLER_NTLM_H_ | 98 #endif // NET_HTTP_HTTP_AUTH_HANDLER_NTLM_H_ |
OLD | NEW |