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