[WebAuthn] Provide a _WKWebAuthenticationPanelUpdatePINInvalid update to UI clients...
[WebKit-https.git] / Source / WebKit / UIProcess / WebAuthentication / Cocoa / WebAuthenticationPanelClient.mm
1 /*
2  * Copyright (C) 2019 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 #import "config.h"
27 #import "WebAuthenticationPanelClient.h"
28
29 #if ENABLE(WEB_AUTHN)
30
31 #import "APIArray.h"
32 #import "APIWebAuthenticationAssertionResponse.h"
33 #import "CompletionHandlerCallChecker.h"
34 #import "WKNSArray.h"
35 #import "_WKWebAuthenticationAssertionResponseInternal.h"
36 #import "_WKWebAuthenticationPanel.h"
37 #import <wtf/BlockPtr.h>
38 #import <wtf/RunLoop.h>
39
40 #import "LocalAuthenticationSoftLink.h"
41
42 namespace WebKit {
43
44 WebAuthenticationPanelClient::WebAuthenticationPanelClient(_WKWebAuthenticationPanel *panel, id <_WKWebAuthenticationPanelDelegate> delegate)
45     : m_panel(panel)
46     , m_delegate(delegate)
47 {
48     m_delegateMethods.panelUpdateWebAuthenticationPanel = [delegate respondsToSelector:@selector(panel:updateWebAuthenticationPanel:)];
49     m_delegateMethods.panelDismissWebAuthenticationPanelWithResult = [delegate respondsToSelector:@selector(panel:dismissWebAuthenticationPanelWithResult:)];
50     m_delegateMethods.panelRequestPinWithRemainingRetriesCompletionHandler = [delegate respondsToSelector:@selector(panel:requestPINWithRemainingRetries:completionHandler:)];
51     m_delegateMethods.panelSelectAssertionResponseSourceCompletionHandler = [delegate respondsToSelector:@selector(panel:selectAssertionResponse:source:completionHandler:)];
52     m_delegateMethods.panelDecidePolicyForLocalAuthenticatorCompletionHandler = [delegate respondsToSelector:@selector(panel:decidePolicyForLocalAuthenticatorWithCompletionHandler:)];
53 }
54
55 RetainPtr<id <_WKWebAuthenticationPanelDelegate> > WebAuthenticationPanelClient::delegate()
56 {
57     return m_delegate.get();
58 }
59
60 static _WKWebAuthenticationPanelUpdate wkWebAuthenticationPanelUpdate(WebAuthenticationStatus status)
61 {
62     if (status == WebAuthenticationStatus::MultipleNFCTagsPresent)
63         return _WKWebAuthenticationPanelUpdateMultipleNFCTagsPresent;
64     if (status == WebAuthenticationStatus::NoCredentialsFound)
65         return _WKWebAuthenticationPanelUpdateNoCredentialsFound;
66     if (status == WebAuthenticationStatus::PinBlocked)
67         return _WKWebAuthenticationPanelUpdatePINBlocked;
68     if (status == WebAuthenticationStatus::PinAuthBlocked)
69         return _WKWebAuthenticationPanelUpdatePINAuthBlocked;
70     if (status == WebAuthenticationStatus::PinInvalid)
71         return _WKWebAuthenticationPanelUpdatePINInvalid;
72     if (status == WebAuthenticationStatus::LAError)
73         return _WKWebAuthenticationPanelUpdateLAError;
74     if (status == WebAuthenticationStatus::LAExcludeCredentialsMatched)
75         return _WKWebAuthenticationPanelUpdateLAExcludeCredentialsMatched;
76     if (status == WebAuthenticationStatus::LANoCredential)
77         return _WKWebAuthenticationPanelUpdateLANoCredential;
78     ASSERT_NOT_REACHED();
79     return _WKWebAuthenticationPanelUpdateMultipleNFCTagsPresent;
80 }
81
82 void WebAuthenticationPanelClient::updatePanel(WebAuthenticationStatus status) const
83 {
84     if (!m_delegateMethods.panelUpdateWebAuthenticationPanel)
85         return;
86
87     auto delegate = m_delegate.get();
88     if (!delegate)
89         return;
90
91     [delegate panel:m_panel updateWebAuthenticationPanel:wkWebAuthenticationPanelUpdate(status)];
92 }
93
94 static _WKWebAuthenticationResult wkWebAuthenticationResult(WebAuthenticationResult result)
95 {
96     switch (result) {
97     case WebAuthenticationResult::Succeeded:
98         return _WKWebAuthenticationResultSucceeded;
99     case WebAuthenticationResult::Failed:
100         return _WKWebAuthenticationResultFailed;
101     }
102     ASSERT_NOT_REACHED();
103     return _WKWebAuthenticationResultFailed;
104 }
105
106 void WebAuthenticationPanelClient::dismissPanel(WebAuthenticationResult result) const
107 {
108     if (!m_delegateMethods.panelDismissWebAuthenticationPanelWithResult)
109         return;
110
111     auto delegate = m_delegate.get();
112     if (!delegate)
113         return;
114
115     [delegate panel:m_panel dismissWebAuthenticationPanelWithResult:wkWebAuthenticationResult(result)];
116 }
117
118 void WebAuthenticationPanelClient::requestPin(uint64_t retries, CompletionHandler<void(const WTF::String&)>&& completionHandler) const
119 {
120     if (!m_delegateMethods.panelRequestPinWithRemainingRetriesCompletionHandler) {
121         completionHandler(String());
122         return;
123     }
124
125     auto delegate = m_delegate.get();
126     if (!delegate) {
127         completionHandler(String());
128         return;
129     }
130
131     auto checker = CompletionHandlerCallChecker::create(delegate.get(), @selector(panel:requestPINWithRemainingRetries:completionHandler:));
132     [delegate panel:m_panel requestPINWithRemainingRetries:retries completionHandler:makeBlockPtr([completionHandler = WTFMove(completionHandler), checker = WTFMove(checker)](NSString *pin) mutable {
133         if (checker->completionHandlerHasBeenCalled())
134             return;
135         checker->didCallCompletionHandler();
136         completionHandler(pin);
137     }).get()];
138 }
139
140 static _WKWebAuthenticationSource wkWebAuthenticationSource(WebAuthenticationSource result)
141 {
142     switch (result) {
143     case WebAuthenticationSource::Local:
144         return _WKWebAuthenticationSourceLocal;
145     case WebAuthenticationSource::External:
146         return _WKWebAuthenticationSourceExternal;
147     }
148     ASSERT_NOT_REACHED();
149     return _WKWebAuthenticationSourceLocal;
150 }
151
152 void WebAuthenticationPanelClient::selectAssertionResponse(Vector<Ref<WebCore::AuthenticatorAssertionResponse>>&& responses, WebAuthenticationSource source, CompletionHandler<void(WebCore::AuthenticatorAssertionResponse*)>&& completionHandler) const
153 {
154     ASSERT(!responses.isEmpty());
155
156     if (!m_delegateMethods.panelSelectAssertionResponseSourceCompletionHandler) {
157         completionHandler(nullptr);
158         return;
159     }
160
161     auto delegate = m_delegate.get();
162     if (!delegate) {
163         completionHandler(nullptr);
164         return;
165     }
166
167     Vector<RefPtr<API::Object>> apiResponses;
168     apiResponses.reserveInitialCapacity(responses.size());
169     for (auto& response : responses)
170         apiResponses.uncheckedAppend(API::WebAuthenticationAssertionResponse::create(response.copyRef()));
171
172     auto checker = CompletionHandlerCallChecker::create(delegate.get(), @selector(panel:selectAssertionResponse:source:completionHandler:));
173     [delegate panel:m_panel selectAssertionResponse:wrapper(API::Array::create(WTFMove(apiResponses))) source:wkWebAuthenticationSource(source) completionHandler:makeBlockPtr([completionHandler = WTFMove(completionHandler), checker = WTFMove(checker)](_WKWebAuthenticationAssertionResponse *response) mutable {
174         if (checker->completionHandlerHasBeenCalled())
175             return;
176         checker->didCallCompletionHandler();
177
178         if (!response) {
179             completionHandler(nullptr);
180             return;
181         }
182         completionHandler(static_cast<API::WebAuthenticationAssertionResponse&>([response _apiObject]).response());
183     }).get()];
184 }
185
186 static LocalAuthenticatorPolicy localAuthenticatorPolicy(_WKLocalAuthenticatorPolicy result)
187 {
188     switch (result) {
189     case _WKLocalAuthenticatorPolicyAllow:
190         return LocalAuthenticatorPolicy::Allow;
191     case _WKLocalAuthenticatorPolicyDisallow:
192         return LocalAuthenticatorPolicy::Disallow;
193     }
194     ASSERT_NOT_REACHED();
195     return LocalAuthenticatorPolicy::Allow;
196 }
197
198 void WebAuthenticationPanelClient::decidePolicyForLocalAuthenticator(CompletionHandler<void(LocalAuthenticatorPolicy)>&& completionHandler) const
199 {
200     if (!m_delegateMethods.panelDecidePolicyForLocalAuthenticatorCompletionHandler) {
201         completionHandler(LocalAuthenticatorPolicy::Disallow);
202         return;
203     }
204
205     auto delegate = m_delegate.get();
206     if (!delegate) {
207         completionHandler(LocalAuthenticatorPolicy::Disallow);
208         return;
209     }
210
211     auto checker = CompletionHandlerCallChecker::create(delegate.get(), @selector(panel:decidePolicyForLocalAuthenticatorWithCompletionHandler:));
212     [delegate panel:m_panel decidePolicyForLocalAuthenticatorWithCompletionHandler:makeBlockPtr([completionHandler = WTFMove(completionHandler), checker = WTFMove(checker)](_WKLocalAuthenticatorPolicy policy) mutable {
213         if (checker->completionHandlerHasBeenCalled())
214             return;
215         checker->didCallCompletionHandler();
216         completionHandler(localAuthenticatorPolicy(policy));
217     }).get()];
218 }
219
220 } // namespace WebKit
221
222 #endif // ENABLE(WEB_AUTHN)