| 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_basic.h" | 5 #include "net/http/http_auth_handler_basic.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 13 #include "net/base/net_string_util.h" | 13 #include "net/base/net_string_util.h" |
| 14 #include "net/http/http_auth.h" | 14 #include "net/http/http_auth.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 namespace { | 19 namespace { |
| 20 | 20 |
| 21 const char* const kBasicSchemeName = "basic"; | 21 const char kBasicSchemeName[] = "basic"; |
| 22 | 22 |
| 23 // Parses a realm from an auth challenge, and converts to UTF8-encoding. | 23 // Parses a realm from an auth challenge, and converts to UTF8-encoding. |
| 24 // Returns whether the realm is invalid or the parameters are invalid. | 24 // Returns whether the realm is invalid or the parameters are invalid. |
| 25 // | 25 // |
| 26 // Note that if a realm was not specified, we will default it to ""; | 26 // Note that if a realm was not specified, we will default it to ""; |
| 27 // so specifying 'Basic realm=""' is equivalent to 'Basic'. | 27 // so specifying 'Basic realm=""' is equivalent to 'Basic'. |
| 28 // | 28 // |
| 29 // This is more generous than RFC 2617, which is pretty clear in the | 29 // This is more generous than RFC 2617, which is pretty clear in the |
| 30 // production of challenge that realm is required. | 30 // production of challenge that realm is required. |
| 31 // | 31 // |
| (...skipping 16 matching lines...) Expand all Loading... |
| 48 if (!ConvertToUtf8AndNormalize(parameters.value(), kCharsetLatin1, realm)) { | 48 if (!ConvertToUtf8AndNormalize(parameters.value(), kCharsetLatin1, realm)) { |
| 49 return false; | 49 return false; |
| 50 } | 50 } |
| 51 } | 51 } |
| 52 return parameters.valid(); | 52 return parameters.valid(); |
| 53 } | 53 } |
| 54 | 54 |
| 55 } // namespace | 55 } // namespace |
| 56 | 56 |
| 57 int HttpAuthHandlerBasic::Init(const HttpAuthChallengeTokenizer& challenge) { | 57 int HttpAuthHandlerBasic::Init(const HttpAuthChallengeTokenizer& challenge) { |
| 58 auth_scheme_ = kBasicSchemeName; | |
| 59 return ParseChallenge(challenge); | 58 return ParseChallenge(challenge); |
| 60 } | 59 } |
| 61 | 60 |
| 61 HttpAuthHandlerBasic::HttpAuthHandlerBasic() |
| 62 : HttpAuthHandler(kBasicSchemeName) {} |
| 63 |
| 62 int HttpAuthHandlerBasic::ParseChallenge( | 64 int HttpAuthHandlerBasic::ParseChallenge( |
| 63 const HttpAuthChallengeTokenizer& challenge) { | 65 const HttpAuthChallengeTokenizer& challenge) { |
| 64 // Verify the challenge's auth-scheme. | 66 // Verify the challenge's auth-scheme. |
| 65 if (!challenge.SchemeIs(kBasicSchemeName)) | 67 if (!challenge.SchemeIs(kBasicSchemeName)) |
| 66 return ERR_INVALID_RESPONSE; | 68 return ERR_INVALID_RESPONSE; |
| 67 | 69 |
| 68 std::string realm; | 70 std::string realm; |
| 69 if (!ParseRealm(challenge, &realm)) | 71 if (!ParseRealm(challenge, &realm)) |
| 70 return ERR_INVALID_RESPONSE; | 72 return ERR_INVALID_RESPONSE; |
| 71 | 73 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 100 *auth_token = "Basic " + base64_username_password; | 102 *auth_token = "Basic " + base64_username_password; |
| 101 return OK; | 103 return OK; |
| 102 } | 104 } |
| 103 | 105 |
| 104 HttpAuthHandlerBasic::Factory::Factory() { | 106 HttpAuthHandlerBasic::Factory::Factory() { |
| 105 } | 107 } |
| 106 | 108 |
| 107 HttpAuthHandlerBasic::Factory::~Factory() { | 109 HttpAuthHandlerBasic::Factory::~Factory() { |
| 108 } | 110 } |
| 109 | 111 |
| 110 int HttpAuthHandlerBasic::Factory::CreateAuthHandler( | 112 scoped_ptr<HttpAuthHandler> |
| 111 const HttpAuthChallengeTokenizer& challenge, | 113 HttpAuthHandlerBasic::Factory::CreateAuthHandlerForScheme( |
| 114 const std::string& scheme) { |
| 115 DCHECK(HttpAuth::IsValidNormalizedScheme(scheme)); |
| 116 if (scheme == kBasicSchemeName) |
| 117 return make_scoped_ptr(new HttpAuthHandlerBasic()); |
| 118 return scoped_ptr<HttpAuthHandler>(); |
| 119 } |
| 120 |
| 121 scoped_ptr<HttpAuthHandler> |
| 122 HttpAuthHandlerBasic::Factory::CreateAndInitPreemptiveAuthHandler( |
| 123 HttpAuthCache::Entry* cache_entry, |
| 124 const HttpAuthChallengeTokenizer& tokenizer, |
| 112 HttpAuth::Target target, | 125 HttpAuth::Target target, |
| 113 const GURL& origin, | 126 const BoundNetLog& net_log) { |
| 114 CreateReason reason, | 127 if (cache_entry->scheme() != kBasicSchemeName) |
| 115 int digest_nonce_count, | 128 return scoped_ptr<HttpAuthHandler>(); |
| 116 const BoundNetLog& net_log, | 129 scoped_ptr<HttpAuthHandler> handler(new HttpAuthHandlerBasic()); |
| 117 scoped_ptr<HttpAuthHandler>* handler) { | 130 int rv = handler->HandleInitialChallenge(tokenizer, target, |
| 118 // TODO(cbentzel): Move towards model of parsing in the factory | 131 cache_entry->origin(), net_log); |
| 119 // method and only constructing when valid. | 132 if (rv == OK) |
| 120 scoped_ptr<HttpAuthHandler> tmp_handler(new HttpAuthHandlerBasic()); | 133 return handler; |
| 121 int result = | 134 return scoped_ptr<HttpAuthHandler>(); |
| 122 tmp_handler->HandleInitialChallenge(challenge, target, origin, net_log); | |
| 123 if (result == OK) | |
| 124 handler->swap(tmp_handler); | |
| 125 return result; | |
| 126 } | 135 } |
| 127 | 136 |
| 128 } // namespace net | 137 } // namespace net |
| OLD | NEW |