WebAuthnService.h (2914B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #ifndef mozilla_dom_WebAuthnService_h_ 6 #define mozilla_dom_WebAuthnService_h_ 7 8 #include "AuthrsBridge_ffi.h" 9 #include "mozilla/DataMutex.h" 10 #include "mozilla/StaticPrefs_security.h" 11 #include "mozilla/dom/WebAuthnPromiseHolder.h" 12 #include "nsIWebAuthnService.h" 13 14 #ifdef MOZ_WIDGET_ANDROID 15 # include "AndroidWebAuthnService.h" 16 #endif 17 18 #ifdef XP_MACOSX 19 # include "MacOSWebAuthnService.h" 20 #endif 21 22 #ifdef XP_WIN 23 # include "WinWebAuthnService.h" 24 #endif 25 26 namespace mozilla::dom { 27 28 already_AddRefed<nsIWebAuthnService> NewWebAuthnService(); 29 30 class WebAuthnService final : public nsIWebAuthnService { 31 public: 32 NS_DECL_THREADSAFE_ISUPPORTS 33 NS_DECL_NSIWEBAUTHNSERVICE 34 35 WebAuthnService() 36 : mTransactionState(Nothing(), "WebAuthnService::mTransactionState") { 37 (void)authrs_service_constructor(getter_AddRefs(mAuthrsService)); 38 #if defined(XP_WIN) 39 if (WinWebAuthnService::AreWebAuthNApisAvailable()) { 40 mPlatformService = new WinWebAuthnService(); 41 } else { 42 mPlatformService = mAuthrsService; 43 } 44 #elif defined(MOZ_WIDGET_ANDROID) 45 mPlatformService = new AndroidWebAuthnService(); 46 #elif defined(XP_MACOSX) 47 if (__builtin_available(macos 13.3, *)) { 48 mPlatformService = NewMacOSWebAuthnServiceIfAvailable(); 49 } 50 if (!mPlatformService) { 51 mPlatformService = mAuthrsService; 52 } 53 #else 54 mPlatformService = mAuthrsService; 55 #endif 56 } 57 58 private: 59 ~WebAuthnService() = default; 60 61 struct TransactionState { 62 nsCOMPtr<nsIWebAuthnService> service; 63 uint64_t transactionId; 64 Maybe<nsCOMPtr<nsIWebAuthnRegisterPromise>> parentRegisterPromise; 65 Maybe<nsCOMPtr<nsIWebAuthnRegisterResult>> registerResult; 66 MozPromiseRequestHolder<WebAuthnRegisterPromise> childRegisterRequest; 67 }; 68 using TransactionStateMutex = DataMutex<Maybe<TransactionState>>; 69 TransactionStateMutex mTransactionState; 70 71 void ShowAttestationConsentPrompt(const nsString& aOrigin, 72 uint64_t aTransactionId, 73 uint64_t aBrowsingContextId); 74 void ResetLocked(const TransactionStateMutex::AutoLock& aGuard); 75 76 nsIWebAuthnService* DefaultService() { 77 if (StaticPrefs::security_webauth_webauthn_enable_softtoken()) { 78 return mAuthrsService; 79 } 80 return mPlatformService; 81 } 82 83 nsIWebAuthnService* AuthrsService() { return mAuthrsService; } 84 85 nsIWebAuthnService* SelectedService() { 86 auto guard = mTransactionState.Lock(); 87 if (guard->isSome()) { 88 return guard->ref().service; 89 } 90 return DefaultService(); 91 } 92 93 nsCOMPtr<nsIWebAuthnService> mAuthrsService; 94 nsCOMPtr<nsIWebAuthnService> mPlatformService; 95 }; 96 97 } // namespace mozilla::dom 98 99 #endif // mozilla_dom_WebAuthnService_h_