07e86335862fca9115139f6f4751ee5c2aee73a1
[WebKit-https.git] / Source / WebKit2 / Shared / Authentication / AuthenticationManager.cpp
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 #include <wtf/Atomics.h>
40
41 #if ENABLE(NETWORK_PROCESS)
42 #include "NetworkProcessProxyMessages.h"
43 #endif
44
45 using namespace WebCore;
46
47 namespace WebKit {
48
49 static uint64_t generateAuthenticationChallengeID()
50 {
51     static int64_t uniqueAuthenticationChallengeID;
52     return atomicIncrement(&uniqueAuthenticationChallengeID);
53 }
54
55 const char* AuthenticationManager::supplementName()
56 {
57     return "AuthenticationManager";
58 }
59
60 AuthenticationManager::AuthenticationManager(ChildProcess* process)
61     : m_process(process)
62 {
63     m_process->addMessageReceiver(Messages::AuthenticationManager::messageReceiverName(), this);
64 }
65
66 uint64_t AuthenticationManager::establishIdentifierForChallenge(const WebCore::AuthenticationChallenge& authenticationChallenge)
67 {
68     uint64_t challengeID = generateAuthenticationChallengeID();
69     m_challenges.set(challengeID, authenticationChallenge);
70     return challengeID;
71 }
72
73 void AuthenticationManager::didReceiveAuthenticationChallenge(WebFrame* frame, const AuthenticationChallenge& authenticationChallenge)
74 {
75     ASSERT(frame);
76     ASSERT(frame->page());
77     
78     m_process->send(Messages::WebPageProxy::DidReceiveAuthenticationChallenge(frame->frameID(), authenticationChallenge, establishIdentifierForChallenge(authenticationChallenge)), frame->page()->pageID());
79 }
80
81 #if ENABLE(NETWORK_PROCESS)
82 void AuthenticationManager::didReceiveAuthenticationChallenge(uint64_t pageID, uint64_t frameID, const AuthenticationChallenge& authenticationChallenge)
83 {
84     ASSERT(pageID);
85     ASSERT(frameID);
86     
87     m_process->send(Messages::NetworkProcessProxy::DidReceiveAuthenticationChallenge(pageID, frameID, authenticationChallenge, establishIdentifierForChallenge(authenticationChallenge)));
88 }
89 #endif
90
91 void AuthenticationManager::didReceiveAuthenticationChallenge(Download* download, const AuthenticationChallenge& authenticationChallenge)
92 {
93     download->send(Messages::DownloadProxy::DidReceiveAuthenticationChallenge(authenticationChallenge, establishIdentifierForChallenge(authenticationChallenge)));
94 }
95
96 // Currently, only Mac knows how to respond to authentication challenges with certificate info.
97 #if !USE(SECURITY_FRAMEWORK)
98 bool AuthenticationManager::tryUsePlatformCertificateInfoForChallenge(const WebCore::AuthenticationChallenge&, const PlatformCertificateInfo&)
99 {
100     return false;
101 }
102 #endif
103
104 void AuthenticationManager::useCredentialForChallenge(uint64_t challengeID, const Credential& credential, const PlatformCertificateInfo& certificateInfo)
105 {
106     AuthenticationChallenge challenge = m_challenges.take(challengeID);
107     ASSERT(!challenge.isNull());
108     
109     if (tryUsePlatformCertificateInfoForChallenge(challenge, certificateInfo))
110         return;
111     
112     AuthenticationClient* coreClient = challenge.authenticationClient();
113     if (!coreClient) {
114         // This authentication challenge comes from a download.
115         Download::receivedCredential(challenge, credential);
116         return;
117     }
118
119     coreClient->receivedCredential(challenge, credential);
120 }
121
122 void AuthenticationManager::continueWithoutCredentialForChallenge(uint64_t challengeID)
123 {
124     AuthenticationChallenge challenge = m_challenges.take(challengeID);
125     ASSERT(!challenge.isNull());
126     AuthenticationClient* coreClient = challenge.authenticationClient();
127     if (!coreClient) {
128         // This authentication challenge comes from a download.
129         Download::receivedRequestToContinueWithoutCredential(challenge);
130         return;
131     }
132
133     coreClient->receivedRequestToContinueWithoutCredential(challenge);
134 }
135
136 void AuthenticationManager::cancelChallenge(uint64_t challengeID)
137 {
138     AuthenticationChallenge challenge = m_challenges.take(challengeID);
139     ASSERT(!challenge.isNull());
140     AuthenticationClient* coreClient = challenge.authenticationClient();
141     if (!coreClient) {
142         // This authentication challenge comes from a download.
143         Download::receivedCancellation(challenge);
144         return;
145     }
146
147     coreClient->receivedCancellation(challenge);
148 }
149
150 } // namespace WebKit