1/*
2 * Copyright (C) 2010, 2013 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "AuthenticationManager.h"
28
29#include "AuthenticationManagerMessages.h"
30#include "ChildProcess.h"
31#include "Download.h"
32#include "DownloadProxyMessages.h"
33#include "WebCoreArgumentCoders.h"
34#include "WebFrame.h"
35#include "WebPage.h"
36#include "WebPageProxyMessages.h"
37#include <WebCore/AuthenticationChallenge.h>
38#include <WebCore/AuthenticationClient.h>
39
40#if ENABLE(NETWORK_PROCESS)
41#include "NetworkProcessProxyMessages.h"
42#endif
43
44using namespace WebCore;
45
46namespace WebKit {
47
48static uint64_t generateAuthenticationChallengeID()
49{
50    ASSERT(RunLoop::isMain());
51
52    static int64_t uniqueAuthenticationChallengeID;
53    return ++uniqueAuthenticationChallengeID;
54}
55
56const char* AuthenticationManager::supplementName()
57{
58    return "AuthenticationManager";
59}
60
61AuthenticationManager::AuthenticationManager(ChildProcess* process)
62    : m_process(process)
63{
64    m_process->addMessageReceiver(Messages::AuthenticationManager::messageReceiverName(), *this);
65}
66
67uint64_t AuthenticationManager::establishIdentifierForChallenge(const WebCore::AuthenticationChallenge& authenticationChallenge)
68{
69    ASSERT(RunLoop::isMain());
70
71    uint64_t challengeID = generateAuthenticationChallengeID();
72    m_challenges.set(challengeID, authenticationChallenge);
73    return challengeID;
74}
75
76void AuthenticationManager::didReceiveAuthenticationChallenge(WebFrame* frame, const AuthenticationChallenge& authenticationChallenge)
77{
78    ASSERT(frame);
79    ASSERT(frame->page());
80
81    m_process->send(Messages::WebPageProxy::DidReceiveAuthenticationChallenge(frame->frameID(), authenticationChallenge, establishIdentifierForChallenge(authenticationChallenge)), frame->page()->pageID());
82}
83
84#if ENABLE(NETWORK_PROCESS)
85void AuthenticationManager::didReceiveAuthenticationChallenge(uint64_t pageID, uint64_t frameID, const AuthenticationChallenge& authenticationChallenge)
86{
87    ASSERT(pageID);
88    ASSERT(frameID);
89
90    m_process->send(Messages::NetworkProcessProxy::DidReceiveAuthenticationChallenge(pageID, frameID, authenticationChallenge, establishIdentifierForChallenge(authenticationChallenge)));
91}
92#endif
93
94void AuthenticationManager::didReceiveAuthenticationChallenge(Download* download, const AuthenticationChallenge& authenticationChallenge)
95{
96    download->send(Messages::DownloadProxy::DidReceiveAuthenticationChallenge(authenticationChallenge, establishIdentifierForChallenge(authenticationChallenge)));
97}
98
99// Currently, only Mac knows how to respond to authentication challenges with certificate info.
100#if !HAVE(SEC_IDENTITY)
101bool AuthenticationManager::tryUseCertificateInfoForChallenge(const WebCore::AuthenticationChallenge&, const CertificateInfo&)
102{
103    return false;
104}
105#endif
106
107void AuthenticationManager::useCredentialForChallenge(uint64_t challengeID, const Credential& credential, const CertificateInfo& certificateInfo)
108{
109    ASSERT(RunLoop::isMain());
110
111    AuthenticationChallenge challenge = m_challenges.take(challengeID);
112    ASSERT(!challenge.isNull());
113
114    if (tryUseCertificateInfoForChallenge(challenge, certificateInfo))
115        return;
116
117    AuthenticationClient* coreClient = challenge.authenticationClient();
118    if (!coreClient) {
119        // This authentication challenge comes from a download.
120        Download::receivedCredential(challenge, credential);
121        return;
122    }
123
124    coreClient->receivedCredential(challenge, credential);
125}
126
127void AuthenticationManager::continueWithoutCredentialForChallenge(uint64_t challengeID)
128{
129    ASSERT(RunLoop::isMain());
130
131    AuthenticationChallenge challenge = m_challenges.take(challengeID);
132    ASSERT(!challenge.isNull());
133    AuthenticationClient* coreClient = challenge.authenticationClient();
134    if (!coreClient) {
135        // This authentication challenge comes from a download.
136        Download::receivedRequestToContinueWithoutCredential(challenge);
137        return;
138    }
139
140    coreClient->receivedRequestToContinueWithoutCredential(challenge);
141}
142
143void AuthenticationManager::cancelChallenge(uint64_t challengeID)
144{
145    ASSERT(RunLoop::isMain());
146
147    AuthenticationChallenge challenge = m_challenges.take(challengeID);
148    ASSERT(!challenge.isNull());
149    AuthenticationClient* coreClient = challenge.authenticationClient();
150    if (!coreClient) {
151        // This authentication challenge comes from a download.
152        Download::receivedCancellation(challenge);
153        return;
154    }
155
156    coreClient->receivedCancellation(challenge);
157}
158
159void AuthenticationManager::performDefaultHandling(uint64_t challengeID)
160{
161    ASSERT(RunLoop::isMain());
162
163    AuthenticationChallenge challenge = m_challenges.take(challengeID);
164    ASSERT(!challenge.isNull());
165    AuthenticationClient* coreClient = challenge.authenticationClient();
166    if (!coreClient) {
167        // This authentication challenge comes from a download.
168        Download::receivedRequestToPerformDefaultHandling(challenge);
169        return;
170    }
171
172    coreClient->receivedRequestToPerformDefaultHandling(challenge);
173}
174
175void AuthenticationManager::rejectProtectionSpaceAndContinue(uint64_t challengeID)
176{
177    ASSERT(RunLoop::isMain());
178
179    AuthenticationChallenge challenge = m_challenges.take(challengeID);
180    ASSERT(!challenge.isNull());
181    AuthenticationClient* coreClient = challenge.authenticationClient();
182    if (!coreClient) {
183        // This authentication challenge comes from a download.
184        Download::receivedChallengeRejection(challenge);
185        return;
186    }
187
188    coreClient->receivedChallengeRejection(challenge);
189}
190
191} // namespace WebKit
192