| 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 | |
| 53 | AuthenticatorManager(); |
| 54 | virtual ~AuthenticatorManager() = default; |
| 55 | |
| 56 | void makeCredential(const Vector<uint8_t>& hash, const WebCore::PublicKeyCredentialCreationOptions&, Callback&&); |
| 57 | void getAssertion(const Vector<uint8_t>& hash, const WebCore::PublicKeyCredentialRequestOptions&, Callback&&); |
| 58 | |
| 59 | virtual bool isMock() const { return false; } |
| 60 | |
| 61 | protected: |
| 62 | Callback& pendingCompletionHandler() { return m_pendingCompletionHandler; } |
| 63 | RunLoop::Timer<AuthenticatorManager>& requestTimeOutTimer() { return m_requestTimeOutTimer; } |
| 64 | void clearStateAsync(); // To void cyclic dependence. |
| 65 | void clearState(); |
| 66 | |
| 67 | private: |
| 68 | // AuthenticatorTransportService::Observer |
| 69 | void authenticatorAdded(Ref<Authenticator>&&) final; |
| 70 | |
| 71 | // Authenticator::Observer |
| 72 | void respondReceived(Respond&&) final; |
| 73 | void downgrade(Authenticator* id, Ref<Authenticator>&& downgradedAuthenticator) final; |
| 74 | |
| 75 | // Overriden by MockAuthenticatorManager. |
| 76 | virtual UniqueRef<AuthenticatorTransportService> createService(WebCore::AuthenticatorTransport, AuthenticatorTransportService::Observer&) const; |
| 77 | // Overriden to return every exception for tests to confirm. |
| 78 | virtual void respondReceivedInternal(Respond&&); |
| 79 | |
| 80 | void startDiscovery(const TransportSet&); |
| 81 | void initTimeOutTimer(const Optional<unsigned>& timeOutInMs); |
| 82 | void timeOutTimerFired(); |
| 83 | |
| 84 | // Request: We only allow one request per time. A new request will cancel any pending ones. |
| 85 | WebAuthenticationRequestData m_pendingRequestData; |
| 86 | Callback m_pendingCompletionHandler; |
| 87 | RunLoop::Timer<AuthenticatorManager> m_requestTimeOutTimer; |
| 88 | |
| 89 | Vector<UniqueRef<AuthenticatorTransportService>> m_services; |
| 90 | HashSet<Ref<Authenticator>> m_authenticators; |
| 91 | }; |
| 92 | |
| 93 | } // namespace WebKit |
| 94 | |
| 95 | #endif // ENABLE(WEB_AUTHN) |
| 96 | |