AuthenticationManager.cpp [plain text]
#include "config.h"
#include "AuthenticationManager.h"
#include "AuthenticationManagerMessages.h"
#include "ChildProcess.h"
#include "Download.h"
#include "DownloadProxyMessages.h"
#include "WebCoreArgumentCoders.h"
#include "WebFrame.h"
#include "WebPage.h"
#include "WebPageProxyMessages.h"
#include <WebCore/AuthenticationChallenge.h>
#include <WebCore/AuthenticationClient.h>
#if ENABLE(NETWORK_PROCESS)
#include "NetworkProcessProxyMessages.h"
#endif
using namespace WebCore;
namespace WebKit {
static uint64_t generateAuthenticationChallengeID()
{
ASSERT(RunLoop::isMain());
static int64_t uniqueAuthenticationChallengeID;
return ++uniqueAuthenticationChallengeID;
}
static bool canCoalesceChallenge(const WebCore::AuthenticationChallenge& challenge)
{
return challenge.protectionSpace().authenticationScheme() != ProtectionSpaceAuthenticationSchemeServerTrustEvaluationRequested;
}
const char* AuthenticationManager::supplementName()
{
return "AuthenticationManager";
}
AuthenticationManager::AuthenticationManager(ChildProcess* process)
: m_process(process)
{
m_process->addMessageReceiver(Messages::AuthenticationManager::messageReceiverName(), *this);
}
uint64_t AuthenticationManager::addChallengeToChallengeMap(const WebCore::AuthenticationChallenge& authenticationChallenge)
{
ASSERT(RunLoop::isMain());
uint64_t challengeID = generateAuthenticationChallengeID();
m_challenges.set(challengeID, authenticationChallenge);
return challengeID;
}
bool AuthenticationManager::shouldCoalesceChallenge(uint64_t challengeID, const AuthenticationChallenge& challenge) const
{
if (!canCoalesceChallenge(challenge))
return false;
auto end = m_challenges.end();
for (auto it = m_challenges.begin(); it != end; ++it) {
if (it->key != challengeID && ProtectionSpace::compare(challenge.protectionSpace(), it->value.protectionSpace()))
return true;
}
return false;
}
Vector<uint64_t> AuthenticationManager::coalesceChallengesMatching(uint64_t challengeID) const
{
AuthenticationChallenge challenge = m_challenges.get(challengeID);
ASSERT(!challenge.isNull());
Vector<uint64_t> challengesToCoalesce;
challengesToCoalesce.append(challengeID);
if (!canCoalesceChallenge(challenge))
return challengesToCoalesce;
auto end = m_challenges.end();
for (auto it = m_challenges.begin(); it != end; ++it) {
if (it->key != challengeID && ProtectionSpace::compare(challenge.protectionSpace(), it->value.protectionSpace()))
challengesToCoalesce.append(it->key);
}
return challengesToCoalesce;
}
void AuthenticationManager::didReceiveAuthenticationChallenge(WebFrame* frame, const AuthenticationChallenge& authenticationChallenge)
{
ASSERT(frame);
ASSERT(frame->page());
uint64_t challengeID = addChallengeToChallengeMap(authenticationChallenge);
if (shouldCoalesceChallenge(challengeID, authenticationChallenge))
return;
m_process->send(Messages::WebPageProxy::DidReceiveAuthenticationChallenge(frame->frameID(), authenticationChallenge, challengeID), frame->page()->pageID());
}
#if ENABLE(NETWORK_PROCESS)
void AuthenticationManager::didReceiveAuthenticationChallenge(uint64_t pageID, uint64_t frameID, const AuthenticationChallenge& authenticationChallenge)
{
ASSERT(pageID);
ASSERT(frameID);
uint64_t challengeID = addChallengeToChallengeMap(authenticationChallenge);
if (shouldCoalesceChallenge(challengeID, authenticationChallenge))
return;
m_process->send(Messages::NetworkProcessProxy::DidReceiveAuthenticationChallenge(pageID, frameID, authenticationChallenge, addChallengeToChallengeMap(authenticationChallenge)));
}
#endif
void AuthenticationManager::didReceiveAuthenticationChallenge(Download* download, const AuthenticationChallenge& authenticationChallenge)
{
uint64_t challengeID = addChallengeToChallengeMap(authenticationChallenge);
if (shouldCoalesceChallenge(challengeID, authenticationChallenge))
return;
download->send(Messages::DownloadProxy::DidReceiveAuthenticationChallenge(authenticationChallenge, addChallengeToChallengeMap(authenticationChallenge)));
}
#if !HAVE(SEC_IDENTITY)
bool AuthenticationManager::tryUseCertificateInfoForChallenge(const WebCore::AuthenticationChallenge&, const CertificateInfo&)
{
return false;
}
#endif
void AuthenticationManager::useCredentialForChallenge(uint64_t challengeID, const Credential& credential, const CertificateInfo& certificateInfo)
{
ASSERT(RunLoop::isMain());
for (auto& coalescedChallengeID : coalesceChallengesMatching(challengeID))
useCredentialForSingleChallenge(coalescedChallengeID, credential, certificateInfo);
}
void AuthenticationManager::useCredentialForSingleChallenge(uint64_t challengeID, const Credential& credential, const CertificateInfo& certificateInfo)
{
AuthenticationChallenge challenge = m_challenges.take(challengeID);
ASSERT(!challenge.isNull());
if (tryUseCertificateInfoForChallenge(challenge, certificateInfo))
return;
AuthenticationClient* coreClient = challenge.authenticationClient();
if (!coreClient) {
Download::receivedCredential(challenge, credential);
return;
}
coreClient->receivedCredential(challenge, credential);
}
void AuthenticationManager::continueWithoutCredentialForChallenge(uint64_t challengeID)
{
ASSERT(RunLoop::isMain());
for (auto& coalescedChallengeID : coalesceChallengesMatching(challengeID))
continueWithoutCredentialForSingleChallenge(coalescedChallengeID);
}
void AuthenticationManager::continueWithoutCredentialForSingleChallenge(uint64_t challengeID)
{
AuthenticationChallenge challenge = m_challenges.take(challengeID);
ASSERT(!challenge.isNull());
AuthenticationClient* coreClient = challenge.authenticationClient();
if (!coreClient) {
Download::receivedRequestToContinueWithoutCredential(challenge);
return;
}
coreClient->receivedRequestToContinueWithoutCredential(challenge);
}
void AuthenticationManager::cancelChallenge(uint64_t challengeID)
{
ASSERT(RunLoop::isMain());
for (auto& coalescedChallengeID : coalesceChallengesMatching(challengeID))
cancelSingleChallenge(coalescedChallengeID);
}
void AuthenticationManager::cancelSingleChallenge(uint64_t challengeID)
{
AuthenticationChallenge challenge = m_challenges.take(challengeID);
ASSERT(!challenge.isNull());
AuthenticationClient* coreClient = challenge.authenticationClient();
if (!coreClient) {
Download::receivedCancellation(challenge);
return;
}
coreClient->receivedCancellation(challenge);
}
void AuthenticationManager::performDefaultHandling(uint64_t challengeID)
{
ASSERT(RunLoop::isMain());
for (auto& coalescedChallengeID : coalesceChallengesMatching(challengeID))
performDefaultHandlingForSingleChallenge(coalescedChallengeID);
}
void AuthenticationManager::performDefaultHandlingForSingleChallenge(uint64_t challengeID)
{
AuthenticationChallenge challenge = m_challenges.take(challengeID);
ASSERT(!challenge.isNull());
AuthenticationClient* coreClient = challenge.authenticationClient();
if (!coreClient) {
Download::receivedRequestToPerformDefaultHandling(challenge);
return;
}
coreClient->receivedRequestToPerformDefaultHandling(challenge);
}
void AuthenticationManager::rejectProtectionSpaceAndContinue(uint64_t challengeID)
{
ASSERT(RunLoop::isMain());
for (auto& coalescedChallengeID : coalesceChallengesMatching(challengeID))
rejectProtectionSpaceAndContinueForSingleChallenge(coalescedChallengeID);
}
void AuthenticationManager::rejectProtectionSpaceAndContinueForSingleChallenge(uint64_t challengeID)
{
AuthenticationChallenge challenge = m_challenges.take(challengeID);
ASSERT(!challenge.isNull());
AuthenticationClient* coreClient = challenge.authenticationClient();
if (!coreClient) {
Download::receivedChallengeRejection(challenge);
return;
}
coreClient->receivedChallengeRejection(challenge);
}
}