1/*
2 * Copyright (C) 2018 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 "WebAuthenticatorCoordinatorProxy.h"
28
29#if ENABLE(WEB_AUTHN)
30
31#include "AuthenticatorManager.h"
32#include "LocalService.h"
33#include "WebAuthenticatorCoordinatorMessages.h"
34#include "WebAuthenticatorCoordinatorProxyMessages.h"
35#include "WebPageProxy.h"
36#include "WebProcessProxy.h"
37#include "WebsiteDataStore.h"
38#include <WebCore/ExceptionData.h>
39#include <WebCore/PublicKeyCredentialData.h>
40#include <wtf/MainThread.h>
41#include <wtf/RunLoop.h>
42
43namespace WebKit {
44
45WebAuthenticatorCoordinatorProxy::WebAuthenticatorCoordinatorProxy(WebPageProxy& webPageProxy)
46 : m_webPageProxy(webPageProxy)
47{
48 m_webPageProxy.process().addMessageReceiver(Messages::WebAuthenticatorCoordinatorProxy::messageReceiverName(), m_webPageProxy.pageID(), *this);
49}
50
51WebAuthenticatorCoordinatorProxy::~WebAuthenticatorCoordinatorProxy()
52{
53 m_webPageProxy.process().removeMessageReceiver(Messages::WebAuthenticatorCoordinatorProxy::messageReceiverName(), m_webPageProxy.pageID());
54}
55
56void WebAuthenticatorCoordinatorProxy::makeCredential(uint64_t messageId, const Vector<uint8_t>& hash, const WebCore::PublicKeyCredentialCreationOptions& options)
57{
58 auto callback = [messageId, weakThis = makeWeakPtr(*this)] (Variant<WebCore::PublicKeyCredentialData, WebCore::ExceptionData>&& result) {
59 ASSERT(RunLoop::isMain());
60 if (!weakThis)
61 return;
62
63 WTF::switchOn(result, [&](const WebCore::PublicKeyCredentialData& data) {
64 weakThis->requestReply(messageId, data, { });
65 }, [&](const WebCore::ExceptionData& exception) {
66 weakThis->requestReply(messageId, { }, exception);
67 });
68 };
69 m_webPageProxy.websiteDataStore().authenticatorManager().makeCredential(hash, options, WTFMove(callback));
70}
71
72void WebAuthenticatorCoordinatorProxy::getAssertion(uint64_t messageId, const Vector<uint8_t>& hash, const WebCore::PublicKeyCredentialRequestOptions& options)
73{
74 auto callback = [messageId, weakThis = makeWeakPtr(*this)] (Variant<WebCore::PublicKeyCredentialData, WebCore::ExceptionData>&& result) {
75 ASSERT(RunLoop::isMain());
76 if (!weakThis)
77 return;
78
79 WTF::switchOn(result, [&](const WebCore::PublicKeyCredentialData& data) {
80 weakThis->requestReply(messageId, data, { });
81 }, [&](const WebCore::ExceptionData& exception) {
82 weakThis->requestReply(messageId, { }, exception);
83 });
84 };
85 m_webPageProxy.websiteDataStore().authenticatorManager().getAssertion(hash, options, WTFMove(callback));
86}
87
88void WebAuthenticatorCoordinatorProxy::isUserVerifyingPlatformAuthenticatorAvailable(uint64_t messageId)
89{
90 m_webPageProxy.send(Messages::WebAuthenticatorCoordinator::IsUserVerifyingPlatformAuthenticatorAvailableReply(messageId, LocalService::isAvailable()));
91}
92
93void WebAuthenticatorCoordinatorProxy::requestReply(uint64_t messageId, const WebCore::PublicKeyCredentialData& data, const WebCore::ExceptionData& exception)
94{
95 m_webPageProxy.send(Messages::WebAuthenticatorCoordinator::RequestReply(messageId, data, exception));
96}
97
98} // namespace WebKit
99
100#endif // ENABLE(WEB_AUTHN)
101