This source file includes following definitions.
- ParseRealm
- Init
- ParseChallenge
- HandleAnotherChallenge
- GenerateAuthTokenImpl
- CreateAuthHandler
#include "net/http/http_auth_handler_basic.h"
#include <string>
#include "base/base64.h"
#include "base/i18n/icu_string_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "net/base/net_errors.h"
#include "net/http/http_auth.h"
#include "net/http/http_auth_challenge_tokenizer.h"
namespace net {
namespace {
bool ParseRealm(const HttpAuthChallengeTokenizer& tokenizer,
std::string* realm) {
CHECK(realm);
realm->clear();
HttpUtil::NameValuePairsIterator parameters = tokenizer.param_pairs();
while (parameters.GetNext()) {
if (!LowerCaseEqualsASCII(parameters.name(), "realm"))
continue;
if (!base::ConvertToUtf8AndNormalize(
parameters.value(), base::kCodepageLatin1, realm))
return false;
}
return parameters.valid();
}
}
bool HttpAuthHandlerBasic::Init(HttpAuthChallengeTokenizer* challenge) {
auth_scheme_ = HttpAuth::AUTH_SCHEME_BASIC;
score_ = 1;
properties_ = 0;
return ParseChallenge(challenge);
}
bool HttpAuthHandlerBasic::ParseChallenge(
HttpAuthChallengeTokenizer* challenge) {
if (!LowerCaseEqualsASCII(challenge->scheme(), "basic"))
return false;
std::string realm;
if (!ParseRealm(*challenge, &realm))
return false;
realm_ = realm;
return true;
}
HttpAuth::AuthorizationResult HttpAuthHandlerBasic::HandleAnotherChallenge(
HttpAuthChallengeTokenizer* challenge) {
std::string realm;
if (!ParseRealm(*challenge, &realm))
return HttpAuth::AUTHORIZATION_RESULT_INVALID;
return (realm_ != realm)?
HttpAuth::AUTHORIZATION_RESULT_DIFFERENT_REALM:
HttpAuth::AUTHORIZATION_RESULT_REJECT;
}
int HttpAuthHandlerBasic::GenerateAuthTokenImpl(
const AuthCredentials* credentials, const HttpRequestInfo*,
const CompletionCallback&, std::string* auth_token) {
DCHECK(credentials);
std::string base64_username_password;
base::Base64Encode(base::UTF16ToUTF8(credentials->username()) + ":" +
base::UTF16ToUTF8(credentials->password()),
&base64_username_password);
*auth_token = "Basic " + base64_username_password;
return OK;
}
HttpAuthHandlerBasic::Factory::Factory() {
}
HttpAuthHandlerBasic::Factory::~Factory() {
}
int HttpAuthHandlerBasic::Factory::CreateAuthHandler(
HttpAuthChallengeTokenizer* challenge,
HttpAuth::Target target,
const GURL& origin,
CreateReason reason,
int digest_nonce_count,
const BoundNetLog& net_log,
scoped_ptr<HttpAuthHandler>* handler) {
scoped_ptr<HttpAuthHandler> tmp_handler(new HttpAuthHandlerBasic());
if (!tmp_handler->InitFromChallenge(challenge, target, origin, net_log))
return ERR_INVALID_RESPONSE;
handler->swap(tmp_handler);
return OK;
}
}