| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 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 |
| 5 #include "chrome/browser/net/spdyproxy/http_auth_handler_spdyproxy.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/i18n/icu_string_conversions.h" |
| 10 #include "base/metrics/histogram.h" |
| 11 #include "base/string_util.h" |
| 12 #include "base/utf_string_conversions.h" |
| 13 #include "net/base/net_errors.h" |
| 14 #include "net/http/http_auth.h" |
| 15 #include "net/http/http_request_info.h" |
| 16 |
| 17 namespace spdyproxy { |
| 18 |
| 19 using net::AuthCredentials; |
| 20 using net::BoundNetLog; |
| 21 using net::CompletionCallback; |
| 22 using net::HttpAuth; |
| 23 using net::HttpAuthHandler; |
| 24 using net::HttpAuthHandlerFactory; |
| 25 using net::HttpRequestInfo; |
| 26 using net::HttpUtil; |
| 27 |
| 28 HttpAuthHandlerSpdyProxy::Factory::Factory( |
| 29 const GURL& authorized_spdyproxy_origin) |
| 30 : authorized_spdyproxy_origin_(authorized_spdyproxy_origin) { |
| 31 } |
| 32 |
| 33 HttpAuthHandlerSpdyProxy::Factory::~Factory() { |
| 34 } |
| 35 |
| 36 int HttpAuthHandlerSpdyProxy::Factory::CreateAuthHandler( |
| 37 HttpAuth::ChallengeTokenizer* challenge, |
| 38 HttpAuth::Target target, |
| 39 const GURL& origin, |
| 40 CreateReason reason, |
| 41 int digest_nonce_count, |
| 42 const BoundNetLog& net_log, |
| 43 scoped_ptr<HttpAuthHandler>* handler) { |
| 44 // If a spdyproxy auth proxy has not been set, refuse all requests to use this |
| 45 // auth handler. |
| 46 if (authorized_spdyproxy_origin_.possibly_invalid_spec().empty()) { |
| 47 VLOG(1) << "SpdyProxy auth without configuring authorized origin."; |
| 48 return net::ERR_UNSUPPORTED_AUTH_SCHEME; |
| 49 } |
| 50 |
| 51 // We ensure that this authentication handler is used only with an authorized |
| 52 // SPDY proxy, since otherwise a user's authentication token can be |
| 53 // sniffed by a malicious proxy that presents an appropriate challenge. |
| 54 const GURL origin_origin = origin.GetOrigin(); |
| 55 if (!(target == HttpAuth::AUTH_PROXY && |
| 56 origin_origin == authorized_spdyproxy_origin_)) { |
| 57 UMA_HISTOGRAM_COUNTS("Net.UnexpectedSpdyProxyAuth", 1); |
| 58 VLOG(1) << "SpdyProxy auth request with an unexpected config." |
| 59 << " origin: " << origin_origin.possibly_invalid_spec() |
| 60 << " authorized_origin: " |
| 61 << authorized_spdyproxy_origin_.possibly_invalid_spec(); |
| 62 return net::ERR_UNSUPPORTED_AUTH_SCHEME; |
| 63 } |
| 64 |
| 65 scoped_ptr<HttpAuthHandler> tmp_handler( |
| 66 new HttpAuthHandlerSpdyProxy(authorized_spdyproxy_origin_)); |
| 67 if (!tmp_handler->InitFromChallenge(challenge, target, origin, net_log)) |
| 68 return net::ERR_INVALID_RESPONSE; |
| 69 handler->swap(tmp_handler); |
| 70 return net::OK; |
| 71 } |
| 72 |
| 73 HttpAuthHandlerSpdyProxy::HttpAuthHandlerSpdyProxy( |
| 74 const GURL& authorized_spdyproxy_origin) |
| 75 : HttpAuthHandler(), |
| 76 authorized_spdyproxy_origin_(authorized_spdyproxy_origin) { |
| 77 } |
| 78 |
| 79 HttpAuth::AuthorizationResult |
| 80 HttpAuthHandlerSpdyProxy::HandleAnotherChallenge( |
| 81 HttpAuth::ChallengeTokenizer* challenge) { |
| 82 // SpdyProxy authentication is always a single round, so any responses |
| 83 // should be treated as a rejection. |
| 84 return HttpAuth::AUTHORIZATION_RESULT_REJECT; |
| 85 } |
| 86 |
| 87 bool HttpAuthHandlerSpdyProxy::NeedsIdentity() { |
| 88 return true; |
| 89 } |
| 90 |
| 91 bool HttpAuthHandlerSpdyProxy::AllowsDefaultCredentials() { |
| 92 return false; |
| 93 } |
| 94 |
| 95 bool HttpAuthHandlerSpdyProxy::AllowsExplicitCredentials() { |
| 96 return true; |
| 97 } |
| 98 |
| 99 bool HttpAuthHandlerSpdyProxy::Init( |
| 100 HttpAuth::ChallengeTokenizer* challenge) { |
| 101 auth_scheme_ = HttpAuth::AUTH_SCHEME_SPDYPROXY; |
| 102 score_ = 5; |
| 103 properties_ = ENCRYPTS_IDENTITY; |
| 104 return ParseChallenge(challenge); |
| 105 } |
| 106 |
| 107 int HttpAuthHandlerSpdyProxy::GenerateAuthTokenImpl( |
| 108 const AuthCredentials* credentials, const HttpRequestInfo* request, |
| 109 const CompletionCallback&, std::string* auth_token) { |
| 110 DCHECK(credentials); |
| 111 if (credentials->password().length() == 0) { |
| 112 DVLOG(1) << "Received a SpdyProxy auth token request without an " |
| 113 << "available token."; |
| 114 return -1; |
| 115 } |
| 116 *auth_token = "SpdyProxy ps=\"" + ps_token_ + "\", sid=\"" + |
| 117 UTF16ToUTF8(credentials->password()) + "\""; |
| 118 return net::OK; |
| 119 } |
| 120 |
| 121 bool HttpAuthHandlerSpdyProxy::ParseChallenge( |
| 122 HttpAuth::ChallengeTokenizer* challenge) { |
| 123 |
| 124 // Verify the challenge's auth-scheme. |
| 125 if (!LowerCaseEqualsASCII(challenge->scheme(), "spdyproxy")) { |
| 126 VLOG(1) << "Parsed challenge without SpdyProxy type"; |
| 127 return false; |
| 128 } |
| 129 |
| 130 HttpUtil::NameValuePairsIterator parameters = challenge->param_pairs(); |
| 131 |
| 132 // Loop through all the properties. |
| 133 while (parameters.GetNext()) { |
| 134 // FAIL -- couldn't parse a property. |
| 135 if (!ParseChallengeProperty(parameters.name(), |
| 136 parameters.value())) |
| 137 return false; |
| 138 } |
| 139 // Check if tokenizer failed. |
| 140 if (!parameters.valid()) |
| 141 return false; |
| 142 |
| 143 // Check that the required properties were provided. |
| 144 if (realm_.empty()) |
| 145 return false; |
| 146 |
| 147 if (ps_token_.empty()) |
| 148 return false; |
| 149 |
| 150 return true; |
| 151 } |
| 152 |
| 153 bool HttpAuthHandlerSpdyProxy::ParseChallengeProperty( |
| 154 const std::string& name, const std::string& value) { |
| 155 if (LowerCaseEqualsASCII(name, "realm")) { |
| 156 std::string realm; |
| 157 if (!base::ConvertToUtf8AndNormalize(value, base::kCodepageLatin1, &realm)) |
| 158 return false; |
| 159 realm_ = realm; |
| 160 } else if (LowerCaseEqualsASCII(name, "ps")) { |
| 161 ps_token_ = value; |
| 162 } else { |
| 163 VLOG(1) << "Skipping unrecognized SpdyProxy auth property, " << name; |
| 164 } |
| 165 return true; |
| 166 } |
| 167 |
| 168 } // namespace spdyproxy |
| OLD | NEW |