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 | #pragma once |
27 | |
28 | #if ENABLE(WEB_AUTHN) |
29 | |
30 | #include "Authenticator.h" |
31 | #include "AuthenticatorTransportService.h" |
32 | #include "WebAuthenticationRequestData.h" |
33 | #include <WebCore/ExceptionData.h> |
34 | #include <WebCore/PublicKeyCredentialData.h> |
35 | #include <wtf/CompletionHandler.h> |
36 | #include <wtf/HashSet.h> |
37 | #include <wtf/Noncopyable.h> |
38 | #include <wtf/RunLoop.h> |
39 | #include <wtf/Vector.h> |
40 | |
41 | namespace WebKit { |
42 | |
43 | class AuthenticatorManager : public AuthenticatorTransportService::Observer, public Authenticator::Observer { |
44 | WTF_MAKE_FAST_ALLOCATED; |
45 | WTF_MAKE_NONCOPYABLE(AuthenticatorManager); |
46 | public: |
47 | using Respond = Variant<WebCore::PublicKeyCredentialData, WebCore::ExceptionData>; |
48 | using Callback = CompletionHandler<void(Respond&&)>; |
49 | using TransportSet = HashSet<WebCore::AuthenticatorTransport, WTF::IntHash<WebCore::AuthenticatorTransport>, WTF::StrongEnumHashTraits<WebCore::AuthenticatorTransport>>; |
50 | |
51 | using AuthenticatorTransportService::Observer::weakPtrFactory; |
52 | using WeakValueType = AuthenticatorTransportService::Observer::WeakValueType; |
53 | |
54 | AuthenticatorManager(); |
55 | virtual ~AuthenticatorManager() = default; |
56 | |
57 | void makeCredential(const Vector<uint8_t>& hash, const WebCore::PublicKeyCredentialCreationOptions&, Callback&&); |
58 | void getAssertion(const Vector<uint8_t>& hash, const WebCore::PublicKeyCredentialRequestOptions&, Callback&&); |
59 | |
60 | virtual bool isMock() const { return false; } |
61 | |
62 | protected: |
63 | Callback& pendingCompletionHandler() { return m_pendingCompletionHandler; } |
64 | RunLoop::Timer<AuthenticatorManager>& requestTimeOutTimer() { return m_requestTimeOutTimer; } |
65 | void clearStateAsync(); // To void cyclic dependence. |
66 | void clearState(); |
67 | |
68 | private: |
69 | // AuthenticatorTransportService::Observer |
70 | void authenticatorAdded(Ref<Authenticator>&&) final; |
71 | |
72 | // Authenticator::Observer |
73 | void respondReceived(Respond&&) final; |
74 | void downgrade(Authenticator* id, Ref<Authenticator>&& downgradedAuthenticator) final; |
75 | |
76 | // Overriden by MockAuthenticatorManager. |
77 | virtual UniqueRef<AuthenticatorTransportService> createService(WebCore::AuthenticatorTransport, AuthenticatorTransportService::Observer&) const; |
78 | // Overriden to return every exception for tests to confirm. |
79 | virtual void respondReceivedInternal(Respond&&); |
80 | |
81 | void startDiscovery(const TransportSet&); |
82 | void initTimeOutTimer(const Optional<unsigned>& timeOutInMs); |
83 | void timeOutTimerFired(); |
84 | |
85 | // Request: We only allow one request per time. A new request will cancel any pending ones. |
86 | WebAuthenticationRequestData m_pendingRequestData; |
87 | Callback m_pendingCompletionHandler; |
88 | RunLoop::Timer<AuthenticatorManager> m_requestTimeOutTimer; |
89 | |
90 | Vector<UniqueRef<AuthenticatorTransportService>> m_services; |
91 | HashSet<Ref<Authenticator>> m_authenticators; |
92 | }; |
93 | |
94 | } // namespace WebKit |
95 | |
96 | #endif // ENABLE(WEB_AUTHN) |
97 | |