Clean up AuthenticationChallengeProxy
[WebKit-https.git] / Source / WebKit / UIProcess / Authentication / AuthenticationChallengeProxy.cpp
1 /*
2  * Copyright (C) 2010 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 "AuthenticationChallengeProxy.h"
28
29 #include "AuthenticationDecisionListener.h"
30 #include "AuthenticationManager.h"
31 #include "AuthenticationManagerMessages.h"
32 #include "ChildProcessProxy.h"
33 #include "WebCertificateInfo.h"
34 #include "WebCoreArgumentCoders.h"
35 #include "WebCredential.h"
36 #include "WebProcessProxy.h"
37 #include "WebProtectionSpace.h"
38
39 #if HAVE(SEC_KEY_PROXY)
40 #include "SecKeyProxyStore.h"
41 #endif
42
43 namespace WebKit {
44
45 AuthenticationChallengeProxy::AuthenticationChallengeProxy(WebCore::AuthenticationChallenge&& authenticationChallenge, uint64_t challengeID, Ref<IPC::Connection>&& connection, WeakPtr<SecKeyProxyStore>&& secKeyProxyStore)
46     : m_coreAuthenticationChallenge(WTFMove(authenticationChallenge))
47     , m_listener(AuthenticationDecisionListener::create([challengeID, connection = WTFMove(connection), secKeyProxyStore = WTFMove(secKeyProxyStore)](AuthenticationChallengeDisposition disposition, std::optional<WebCore::Credential>&& credential) {
48         switch (disposition) {
49         case AuthenticationChallengeDisposition::Cancel:
50             connection->send(Messages::AuthenticationManager::CancelChallenge(challengeID), 0);
51             break;
52         case AuthenticationChallengeDisposition::PerformDefaultHandling:
53             connection->send(Messages::AuthenticationManager::PerformDefaultHandling(challengeID), 0);
54             break;
55         case AuthenticationChallengeDisposition::RejectProtectionSpaceAndContinue:
56             connection->send(Messages::AuthenticationManager::RejectProtectionSpaceAndContinue(challengeID), 0);
57             break;
58         case AuthenticationChallengeDisposition::UseCredential:
59             if (!credential) {
60                 connection->send(Messages::AuthenticationManager::ContinueWithoutCredentialForChallenge(challengeID), 0);
61                 break;
62             }
63             
64 #if HAVE(SEC_KEY_PROXY)
65             if (secKeyProxyStore) {
66                 secKeyProxyStore->initialize(*credential);
67                 sendClientCertificateCredentialOverXpc(connection, *secKeyProxyStore, challengeID, *credential);
68                 break;
69             }
70 #endif
71
72             connection->send(Messages::AuthenticationManager::UseCredentialForChallenge(challengeID, *credential), 0);
73         }
74     }))
75 {
76 }
77
78 WebCredential* AuthenticationChallengeProxy::proposedCredential() const
79 {
80     if (!m_webCredential)
81         m_webCredential = WebCredential::create(m_coreAuthenticationChallenge.proposedCredential());
82
83     return m_webCredential.get();
84 }
85
86 WebProtectionSpace* AuthenticationChallengeProxy::protectionSpace() const
87 {
88     if (!m_webProtectionSpace)
89         m_webProtectionSpace = WebProtectionSpace::create(m_coreAuthenticationChallenge.protectionSpace());
90
91     return m_webProtectionSpace.get();
92 }
93
94 } // namespace WebKit