Backed out changeset 793702f190e3 (bug 1853819) for bc failures on browser_webauthn_p...
[gecko.git] / dom / webauthn / WebAuthnController.h
blob9497b146f62809fea453357355498e9e07e7ae65
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_WebAuthnController_h
8 #define mozilla_dom_WebAuthnController_h
10 #include "nsIWebAuthnController.h"
11 #include "mozilla/dom/PWebAuthnTransaction.h"
12 #include "mozilla/Tainting.h"
15 * Parent process manager for WebAuthn API transactions. Handles process
16 * transactions from all content processes, make sure only one transaction is
17 * live at any time. Manages access to hardware and software based key systems.
21 namespace mozilla::dom {
23 class WebAuthnController final : public nsIWebAuthnController {
24 public:
25 NS_DECL_THREADSAFE_ISUPPORTS
26 NS_DECL_NSIU2FTOKENMANAGER
27 NS_DECL_NSIWEBAUTHNCONTROLLER
29 static void Initialize();
30 static WebAuthnController* Get();
31 void Register(PWebAuthnTransactionParent* aTransactionParent,
32 const uint64_t& aTransactionId,
33 const WebAuthnMakeCredentialInfo& aInfo);
35 void Sign(PWebAuthnTransactionParent* aTransactionParent,
36 const uint64_t& aTransactionId,
37 const WebAuthnGetAssertionInfo& aInfo);
39 void Cancel(PWebAuthnTransactionParent* aTransactionParent,
40 const Tainted<uint64_t>& aTransactionId);
42 void MaybeClearTransaction(PWebAuthnTransactionParent* aParent);
44 uint64_t GetCurrentTransactionId() {
45 return mTransaction.isNothing() ? 0 : mTransaction.ref().mTransactionId;
48 bool CurrentTransactionIsRegister() { return mPendingRegisterInfo.isSome(); }
50 bool CurrentTransactionIsSign() { return mPendingSignInfo.isSome(); }
52 // Sends a "webauthn-prompt" observer notification with the given data.
53 template <typename... T>
54 void SendPromptNotification(const char16_t* aFormat, T... aArgs);
56 // Same as SendPromptNotification, but with the already formatted string
57 // void SendPromptNotificationPreformatted(const nsACString& aJSON);
58 // The main thread runnable function for "SendPromptNotification".
59 void RunSendPromptNotification(const nsString& aJSON);
61 private:
62 WebAuthnController();
63 ~WebAuthnController() = default;
64 nsCOMPtr<nsIWebAuthnTransport> GetTransportImpl();
66 void AbortTransaction(const uint64_t& aTransactionId, const nsresult& aError,
67 bool shouldCancelActiveDialog);
68 void AbortOngoingTransaction();
69 void ClearTransaction(bool cancel_prompt);
71 void DoRegister(const WebAuthnMakeCredentialInfo& aInfo,
72 bool aForceNoneAttestation);
73 void DoSign(const WebAuthnGetAssertionInfo& aTransactionInfo);
75 void RunFinishRegister(uint64_t aTransactionId,
76 const RefPtr<nsICtapRegisterResult>& aResult);
77 void RunFinishSign(uint64_t aTransactionId,
78 const nsTArray<RefPtr<nsICtapSignResult>>& aResult);
80 // The main thread runnable function for "nsIU2FTokenManager.ResumeRegister".
81 void RunResumeRegister(uint64_t aTransactionId, bool aForceNoneAttestation);
82 void RunResumeSign(uint64_t aTransactionId);
83 void RunResumeWithSelectedSignResult(uint64_t aTransactionId, uint64_t idx);
84 void RunPinCallback(uint64_t aTransactionId, const nsCString& aPin);
86 // The main thread runnable function for "nsIU2FTokenManager.Cancel".
87 void RunCancel(uint64_t aTransactionId);
89 // Using a raw pointer here, as the lifetime of the IPC object is managed by
90 // the PBackground protocol code. This means we cannot be left holding an
91 // invalid IPC protocol object after the transaction is finished.
92 PWebAuthnTransactionParent* mTransactionParent;
94 nsCOMPtr<nsIWebAuthnTransport> mTransportImpl;
96 // Pending registration info while we wait for user input.
97 Maybe<WebAuthnMakeCredentialInfo> mPendingRegisterInfo;
99 // Pending registration info while we wait for user input.
100 Maybe<WebAuthnGetAssertionInfo> mPendingSignInfo;
102 nsTArray<RefPtr<nsICtapSignResult>> mPendingSignResults;
104 class Transaction {
105 public:
106 Transaction(uint64_t aTransactionId, const nsTArray<uint8_t>& aRpIdHash,
107 const Maybe<nsTArray<uint8_t>>& aAppIdHash,
108 const nsCString& aClientDataJSON,
109 bool aForceNoneAttestation = false)
110 : mTransactionId(aTransactionId),
111 mRpIdHash(aRpIdHash.Clone()),
112 mClientDataJSON(aClientDataJSON) {
113 if (aAppIdHash.isSome()) {
114 mAppIdHash = Some(aAppIdHash.ref().Clone());
115 } else {
116 mAppIdHash = Nothing();
119 uint64_t mTransactionId;
120 nsTArray<uint8_t> mRpIdHash;
121 Maybe<nsTArray<uint8_t>> mAppIdHash;
122 nsCString mClientDataJSON;
125 Maybe<Transaction> mTransaction;
128 } // namespace mozilla::dom
130 #endif // mozilla_dom_U2FTokenManager_h