http_auth_handler.cc revision 3f50c38dc070f4bb515c1b64450dae14f316474e
1// Copyright (c) 2010 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 "net/http/http_auth_handler.h" 6 7#include "base/logging.h" 8#include "base/string_util.h" 9#include "base/stringprintf.h" 10#include "net/base/net_errors.h" 11 12namespace net { 13 14HttpAuthHandler::HttpAuthHandler() 15 : auth_scheme_(AUTH_SCHEME_MAX), 16 score_(-1), 17 target_(HttpAuth::AUTH_NONE), 18 properties_(-1), 19 original_callback_(NULL), 20 ALLOW_THIS_IN_INITIALIZER_LIST( 21 wrapper_callback_( 22 this, &HttpAuthHandler::OnGenerateAuthTokenComplete)) { 23} 24 25HttpAuthHandler::~HttpAuthHandler() { 26} 27 28bool HttpAuthHandler::InitFromChallenge( 29 HttpAuth::ChallengeTokenizer* challenge, 30 HttpAuth::Target target, 31 const GURL& origin, 32 const BoundNetLog& net_log) { 33 origin_ = origin; 34 target_ = target; 35 score_ = -1; 36 properties_ = -1; 37 net_log_ = net_log; 38 39 auth_challenge_ = challenge->challenge_text(); 40 bool ok = Init(challenge); 41 42 // Init() is expected to set the scheme, realm, score, and properties. The 43 // realm may be empty. 44 DCHECK(!ok || !scheme().empty()); 45 DCHECK(!ok || score_ != -1); 46 DCHECK(!ok || properties_ != -1); 47 DCHECK(!ok || auth_scheme_ != AUTH_SCHEME_MAX); 48 49 return ok; 50} 51 52namespace { 53 54NetLog::EventType EventTypeFromAuthTarget(HttpAuth::Target target) { 55 switch (target) { 56 case HttpAuth::AUTH_PROXY: 57 return NetLog::TYPE_AUTH_PROXY; 58 case HttpAuth::AUTH_SERVER: 59 return NetLog::TYPE_AUTH_SERVER; 60 default: 61 NOTREACHED(); 62 return NetLog::TYPE_CANCELLED; 63 } 64} 65 66} // namespace 67 68int HttpAuthHandler::GenerateAuthToken(const string16* username, 69 const string16* password, 70 const HttpRequestInfo* request, 71 CompletionCallback* callback, 72 std::string* auth_token) { 73 // TODO(cbentzel): Enforce non-NULL callback after cleaning up SocketStream. 74 DCHECK(request); 75 DCHECK((username == NULL) == (password == NULL)); 76 DCHECK(username != NULL || AllowsDefaultCredentials()); 77 DCHECK(auth_token != NULL); 78 DCHECK(original_callback_ == NULL); 79 original_callback_ = callback; 80 net_log_.BeginEvent(EventTypeFromAuthTarget(target_), NULL); 81 int rv = GenerateAuthTokenImpl(username, password, request, 82 &wrapper_callback_, auth_token); 83 if (rv != ERR_IO_PENDING) 84 FinishGenerateAuthToken(); 85 return rv; 86} 87 88bool HttpAuthHandler::NeedsIdentity() { 89 return true; 90} 91 92bool HttpAuthHandler::AllowsDefaultCredentials() { 93 return false; 94} 95 96void HttpAuthHandler::OnGenerateAuthTokenComplete(int rv) { 97 CompletionCallback* callback = original_callback_; 98 FinishGenerateAuthToken(); 99 if (callback) 100 callback->Run(rv); 101} 102 103void HttpAuthHandler::FinishGenerateAuthToken() { 104 // TOOD(cbentzel): Should this be done in OK case only? 105 net_log_.EndEvent(EventTypeFromAuthTarget(target_), NULL); 106 original_callback_ = NULL; 107} 108 109} // namespace net 110