OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 #include "net/http/http_auth_handler_ntlm.h" | 5 #include "net/http/http_auth_handler_ntlm.h" |
6 | 6 |
7 #if !defined(NTLM_SSPI) | 7 #if !defined(NTLM_SSPI) |
8 #include "base/base64.h" | 8 #include "base/base64.h" |
9 #endif | 9 #endif |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
14 #include "net/base/net_util.h" | 14 #include "net/base/net_util.h" |
15 #include "net/http/http_auth_challenge_tokenizer.h" | 15 #include "net/http/http_auth_challenge_tokenizer.h" |
16 | 16 |
17 namespace net { | 17 namespace net { |
18 | 18 |
19 HttpAuth::AuthorizationResult HttpAuthHandlerNTLM::HandleAnotherChallenge( | 19 HttpAuth::AuthorizationResult HttpAuthHandlerNTLM::HandleAnotherChallenge( |
20 HttpAuthChallengeTokenizer* challenge) { | 20 const HttpAuthChallengeTokenizer& challenge) { |
21 return ParseChallenge(challenge, false); | 21 return ParseChallenge(challenge, false); |
22 } | 22 } |
23 | 23 |
24 bool HttpAuthHandlerNTLM::Init(HttpAuthChallengeTokenizer* tok) { | 24 int HttpAuthHandlerNTLM::Init(const HttpAuthChallengeTokenizer& tok) { |
25 auth_scheme_ = "ntlm"; | 25 auth_scheme_ = "ntlm"; |
26 return ParseChallenge(tok, true) == HttpAuth::AUTHORIZATION_RESULT_ACCEPT; | 26 return ParseChallenge(tok, true) == HttpAuth::AUTHORIZATION_RESULT_ACCEPT |
| 27 ? OK |
| 28 : ERR_INVALID_RESPONSE; |
27 } | 29 } |
28 | 30 |
29 int HttpAuthHandlerNTLM::GenerateAuthTokenImpl( | 31 int HttpAuthHandlerNTLM::GenerateAuthTokenImpl( |
30 const AuthCredentials* credentials, const HttpRequestInfo* request, | 32 const AuthCredentials* credentials, |
31 const CompletionCallback& callback, std::string* auth_token) { | 33 const HttpRequestInfo& request, |
| 34 const CompletionCallback& callback, |
| 35 std::string* auth_token) { |
32 #if defined(NTLM_SSPI) | 36 #if defined(NTLM_SSPI) |
33 return auth_sspi_.GenerateAuthToken(credentials, CreateSPN(origin_), | 37 return auth_sspi_.GenerateAuthToken(credentials, CreateSPN(origin_), |
34 auth_token, callback); | 38 auth_token, callback); |
35 #else // !defined(NTLM_SSPI) | 39 #else // !defined(NTLM_SSPI) |
36 // TODO(cbentzel): Shouldn't be hitting this case. | 40 // TODO(cbentzel): Shouldn't be hitting this case. |
37 if (!credentials) { | 41 if (!credentials) { |
38 LOG(ERROR) << "Username and password are expected to be non-NULL."; | 42 LOG(ERROR) << "Username and password are expected to be non-NULL."; |
39 return ERR_MISSING_AUTH_CREDENTIALS; | 43 return ERR_MISSING_AUTH_CREDENTIALS; |
40 } | 44 } |
41 // TODO(wtc): See if we can use char* instead of void* for in_buf and | 45 // TODO(wtc): See if we can use char* instead of void* for in_buf and |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 // OK, we are done with |out_buf| | 93 // OK, we are done with |out_buf| |
90 free(out_buf); | 94 free(out_buf); |
91 *auth_token = std::string("NTLM ") + encode_output; | 95 *auth_token = std::string("NTLM ") + encode_output; |
92 return OK; | 96 return OK; |
93 #endif | 97 #endif |
94 } | 98 } |
95 | 99 |
96 // The NTLM challenge header looks like: | 100 // The NTLM challenge header looks like: |
97 // WWW-Authenticate: NTLM auth-data | 101 // WWW-Authenticate: NTLM auth-data |
98 HttpAuth::AuthorizationResult HttpAuthHandlerNTLM::ParseChallenge( | 102 HttpAuth::AuthorizationResult HttpAuthHandlerNTLM::ParseChallenge( |
99 HttpAuthChallengeTokenizer* tok, bool initial_challenge) { | 103 const HttpAuthChallengeTokenizer& tok, |
| 104 bool initial_challenge) { |
100 #if defined(NTLM_SSPI) | 105 #if defined(NTLM_SSPI) |
101 // auth_sspi_ contains state for whether or not this is the initial challenge. | 106 // auth_sspi_ contains state for whether or not this is the initial challenge. |
102 return auth_sspi_.ParseChallenge(tok); | 107 return auth_sspi_.ParseChallenge(tok); |
103 #else | 108 #else |
104 // TODO(cbentzel): Most of the logic between SSPI, GSSAPI, and portable NTLM | 109 // TODO(cbentzel): Most of the logic between SSPI, GSSAPI, and portable NTLM |
105 // authentication parsing could probably be shared - just need to know if | 110 // authentication parsing could probably be shared - just need to know if |
106 // there was previously a challenge round. | 111 // there was previously a challenge round. |
107 // TODO(cbentzel): Write a test case to validate that auth_data_ is left empty | 112 // TODO(cbentzel): Write a test case to validate that auth_data_ is left empty |
108 // in all failure conditions. | 113 // in all failure conditions. |
109 auth_data_.clear(); | 114 auth_data_.clear(); |
110 | 115 |
111 // Verify the challenge's auth-scheme. | 116 // Verify the challenge's auth-scheme. |
112 if (!tok->SchemeIs("ntlm")) | 117 if (!tok.SchemeIs("ntlm")) |
113 return HttpAuth::AUTHORIZATION_RESULT_INVALID; | 118 return HttpAuth::AUTHORIZATION_RESULT_INVALID; |
114 | 119 |
115 std::string base64_param = tok->base64_param(); | 120 std::string base64_param = tok.base64_param(); |
116 if (base64_param.empty()) { | 121 if (base64_param.empty()) { |
117 if (!initial_challenge) | 122 if (!initial_challenge) |
118 return HttpAuth::AUTHORIZATION_RESULT_REJECT; | 123 return HttpAuth::AUTHORIZATION_RESULT_REJECT; |
119 return HttpAuth::AUTHORIZATION_RESULT_ACCEPT; | 124 return HttpAuth::AUTHORIZATION_RESULT_ACCEPT; |
120 } else { | 125 } else { |
121 if (initial_challenge) | 126 if (initial_challenge) |
122 return HttpAuth::AUTHORIZATION_RESULT_INVALID; | 127 return HttpAuth::AUTHORIZATION_RESULT_INVALID; |
123 } | 128 } |
124 | 129 |
125 auth_data_ = base64_param; | 130 auth_data_ = base64_param; |
126 return HttpAuth::AUTHORIZATION_RESULT_ACCEPT; | 131 return HttpAuth::AUTHORIZATION_RESULT_ACCEPT; |
127 #endif // defined(NTLM_SSPI) | 132 #endif // defined(NTLM_SSPI) |
128 } | 133 } |
129 | 134 |
130 // static | 135 // static |
131 std::string HttpAuthHandlerNTLM::CreateSPN(const GURL& origin) { | 136 std::string HttpAuthHandlerNTLM::CreateSPN(const GURL& origin) { |
132 // The service principal name of the destination server. See | 137 // The service principal name of the destination server. See |
133 // http://msdn.microsoft.com/en-us/library/ms677949%28VS.85%29.aspx | 138 // http://msdn.microsoft.com/en-us/library/ms677949%28VS.85%29.aspx |
134 std::string target("HTTP/"); | 139 std::string target("HTTP/"); |
135 target.append(GetHostAndPort(origin)); | 140 target.append(GetHostAndPort(origin)); |
136 return target; | 141 return target; |
137 } | 142 } |
138 | 143 |
139 } // namespace net | 144 } // namespace net |
OLD | NEW |