[WebAuthN] Implement authenticatorMakeCredential
[WebKit-https.git] / Source / WebCore / Modules / credentialmanagement / CredentialsMessenger.h
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 "ExceptionData.h"
31 #include "ExceptionOr.h"
32 #include <JavaScriptCore/ArrayBuffer.h>
33 #include <wtf/CompletionHandler.h>
34 #include <wtf/HashMap.h>
35 #include <wtf/WeakPtr.h>
36
37 namespace WebCore {
38
39 class DeferredPromise;
40
41 struct PublicKeyCredentialCreationOptions;
42 struct PublicKeyCredentialRequestOptions;
43
44 struct CreationReturnBundle {
45     CreationReturnBundle(Ref<ArrayBuffer>&& credentialId, Ref<ArrayBuffer>&& attestationObject)
46         : credentialId(WTFMove(credentialId))
47         , attestationObject(WTFMove(attestationObject))
48     {
49     }
50
51     Ref<ArrayBuffer> credentialId;
52     Ref<ArrayBuffer> attestationObject;
53 };
54 struct AssertionReturnBundle {
55     AssertionReturnBundle(Ref<ArrayBuffer>&& credentialId, Ref<ArrayBuffer>&& authenticatorData, Ref<ArrayBuffer>&& signature, Ref<ArrayBuffer>&& userHandle)
56         : credentialId(WTFMove(credentialId))
57         , authenticatorData(WTFMove(authenticatorData))
58         , signature(WTFMove(signature))
59         , userHandle(WTFMove(userHandle))
60     {
61     }
62
63     Ref<ArrayBuffer> credentialId;
64     Ref<ArrayBuffer> authenticatorData;
65     Ref<ArrayBuffer> signature;
66     Ref<ArrayBuffer> userHandle;
67 };
68
69 using CreationCompletionHandler = CompletionHandler<void(ExceptionOr<CreationReturnBundle>&&)>;
70 using RequestCompletionHandler = CompletionHandler<void(ExceptionOr<AssertionReturnBundle>&&)>;
71 using QueryCompletionHandler = CompletionHandler<void(bool)>;
72
73 class CredentialsMessenger {
74     WTF_MAKE_FAST_ALLOCATED;
75     WTF_MAKE_NONCOPYABLE(CredentialsMessenger);
76 public:
77     CredentialsMessenger() = default;
78
79     // Senders.
80     virtual void makeCredential(const Vector<uint8_t>& hash, const PublicKeyCredentialCreationOptions&, CreationCompletionHandler&&) = 0;
81     virtual void getAssertion(const Vector<uint8_t>& hash, const PublicKeyCredentialRequestOptions&, RequestCompletionHandler&&) = 0;
82     virtual void isUserVerifyingPlatformAuthenticatorAvailable(QueryCompletionHandler&&) = 0;
83
84     // Receivers.
85     WEBCORE_EXPORT void exceptionReply(uint64_t messageId, const ExceptionData&);
86     virtual void makeCredentialReply(uint64_t messageId, const Vector<uint8_t>& credentialId, const Vector<uint8_t>& attestationObject) = 0;
87     virtual void getAssertionReply(uint64_t messageId, const Vector<uint8_t>& credentialId, const Vector<uint8_t>& authenticatorData, const Vector<uint8_t>& signature, const Vector<uint8_t>& userHandle) = 0;
88     virtual void isUserVerifyingPlatformAuthenticatorAvailableReply(uint64_t messageId, bool) = 0;
89
90     auto& weakPtrFactory() const { return m_weakFactory; }
91
92 protected:
93     virtual ~CredentialsMessenger() = default;
94
95     WEBCORE_EXPORT uint64_t addCreationCompletionHandler(CreationCompletionHandler&&);
96     WEBCORE_EXPORT CreationCompletionHandler takeCreationCompletionHandler(uint64_t);
97     WEBCORE_EXPORT uint64_t addRequestCompletionHandler(RequestCompletionHandler&&);
98     WEBCORE_EXPORT RequestCompletionHandler takeRequestCompletionHandler(uint64_t);
99     WEBCORE_EXPORT uint64_t addQueryCompletionHandler(QueryCompletionHandler&&);
100     WEBCORE_EXPORT QueryCompletionHandler takeQueryCompletionHandler(uint64_t);
101
102 private:
103     WeakPtrFactory<CredentialsMessenger> m_weakFactory;
104
105     enum CallBackClassifier : uint64_t {
106         Creation = 0x01,
107         Request = 0x02,
108         Query = 0x03,
109     };
110     // The most significant byte is reserved as callback classifier.
111     uint64_t m_accumulatedMessageId { 1 };
112     HashMap<uint64_t, CreationCompletionHandler> m_pendingCreationCompletionHandlers;
113     HashMap<uint64_t, RequestCompletionHandler> m_pendingRequestCompletionHandlers;
114     HashMap<uint64_t, QueryCompletionHandler> m_pendingQueryCompletionHandlers;
115 };
116
117 } // namespace WebCore
118
119 #endif // ENABLE(WEB_AUTHN)