Bug 1769628 [wpt PR 34081] - Update wpt metadata, a=testonly
[gecko.git] / dom / webauthn / PublicKeyCredential.cpp
blob9350e8c2fe5b46479f766f150387ea617db6304c
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 #include "mozilla/dom/Promise.h"
8 #include "mozilla/dom/PublicKeyCredential.h"
9 #include "mozilla/dom/WebAuthenticationBinding.h"
10 #include "nsCycleCollectionParticipant.h"
11 #include "mozilla/dom/AuthenticatorResponse.h"
12 #include "mozilla/HoldDropJSObjects.h"
14 #ifdef OS_WIN
15 # include "WinWebAuthnManager.h"
16 #endif
18 #ifdef MOZ_WIDGET_ANDROID
19 # include "mozilla/java/WebAuthnTokenManagerWrappers.h"
20 #endif
22 namespace mozilla::dom {
24 NS_IMPL_CYCLE_COLLECTION_CLASS(PublicKeyCredential)
25 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(PublicKeyCredential, Credential)
26 tmp->mRawIdCachedObj = nullptr;
27 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
29 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(PublicKeyCredential, Credential)
30 NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
31 NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mRawIdCachedObj)
32 NS_IMPL_CYCLE_COLLECTION_TRACE_END
34 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(PublicKeyCredential,
35 Credential)
36 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
38 NS_IMPL_ADDREF_INHERITED(PublicKeyCredential, Credential)
39 NS_IMPL_RELEASE_INHERITED(PublicKeyCredential, Credential)
41 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PublicKeyCredential)
42 NS_INTERFACE_MAP_END_INHERITING(Credential)
44 PublicKeyCredential::PublicKeyCredential(nsPIDOMWindowInner* aParent)
45 : Credential(aParent), mRawIdCachedObj(nullptr) {
46 mozilla::HoldJSObjects(this);
49 PublicKeyCredential::~PublicKeyCredential() { mozilla::DropJSObjects(this); }
51 JSObject* PublicKeyCredential::WrapObject(JSContext* aCx,
52 JS::Handle<JSObject*> aGivenProto) {
53 return PublicKeyCredential_Binding::Wrap(aCx, this, aGivenProto);
56 void PublicKeyCredential::GetRawId(JSContext* aCx,
57 JS::MutableHandle<JSObject*> aRetVal) {
58 if (!mRawIdCachedObj) {
59 mRawIdCachedObj = mRawId.ToArrayBuffer(aCx);
61 aRetVal.set(mRawIdCachedObj);
64 already_AddRefed<AuthenticatorResponse> PublicKeyCredential::Response() const {
65 RefPtr<AuthenticatorResponse> temp(mResponse);
66 return temp.forget();
69 nsresult PublicKeyCredential::SetRawId(CryptoBuffer& aBuffer) {
70 if (NS_WARN_IF(!mRawId.Assign(aBuffer))) {
71 return NS_ERROR_OUT_OF_MEMORY;
73 return NS_OK;
76 void PublicKeyCredential::SetResponse(RefPtr<AuthenticatorResponse> aResponse) {
77 mResponse = aResponse;
80 /* static */
81 already_AddRefed<Promise>
82 PublicKeyCredential::IsUserVerifyingPlatformAuthenticatorAvailable(
83 GlobalObject& aGlobal, ErrorResult& aError) {
84 RefPtr<Promise> promise =
85 Promise::Create(xpc::CurrentNativeGlobal(aGlobal.Context()), aError);
86 if (aError.Failed()) {
87 return nullptr;
90 // https://w3c.github.io/webauthn/#isUserVerifyingPlatformAuthenticatorAvailable
92 // If on latest windows, call system APIs, otherwise return false, as we don't
93 // have other UVPAAs available at this time.
94 #ifdef OS_WIN
96 if (WinWebAuthnManager::IsUserVerifyingPlatformAuthenticatorAvailable()) {
97 promise->MaybeResolve(true);
98 return promise.forget();
101 promise->MaybeResolve(false);
102 #elif defined(MOZ_WIDGET_ANDROID)
103 auto result = java::WebAuthnTokenManager::
104 WebAuthnIsUserVerifyingPlatformAuthenticatorAvailable();
105 auto geckoResult = java::GeckoResult::LocalRef(std::move(result));
106 MozPromise<bool, bool, false>::FromGeckoResult(geckoResult)
107 ->Then(GetMainThreadSerialEventTarget(), __func__,
108 [promise](const MozPromise<bool, bool,
109 false>::ResolveOrRejectValue& aValue) {
110 if (aValue.IsResolve()) {
111 promise->MaybeResolve(aValue.ResolveValue());
114 #else
115 promise->MaybeResolve(false);
116 #endif
117 return promise.forget();
120 /* static */
121 already_AddRefed<Promise>
122 PublicKeyCredential::IsExternalCTAP2SecurityKeySupported(GlobalObject& aGlobal,
123 ErrorResult& aError) {
124 RefPtr<Promise> promise =
125 Promise::Create(xpc::CurrentNativeGlobal(aGlobal.Context()), aError);
126 if (aError.Failed()) {
127 return nullptr;
130 #ifdef OS_WIN
131 if (WinWebAuthnManager::AreWebAuthNApisAvailable()) {
132 promise->MaybeResolve(true);
133 return promise.forget();
135 #endif
137 promise->MaybeResolve(false);
138 return promise.forget();
141 void PublicKeyCredential::GetClientExtensionResults(
142 AuthenticationExtensionsClientOutputs& aResult) {
143 aResult = mClientExtensionOutputs;
146 void PublicKeyCredential::SetClientExtensionResultAppId(bool aResult) {
147 mClientExtensionOutputs.mAppid.Construct();
148 mClientExtensionOutputs.mAppid.Value() = aResult;
151 void PublicKeyCredential::SetClientExtensionResultHmacSecret(
152 bool aHmacCreateSecret) {
153 mClientExtensionOutputs.mHmacCreateSecret.Construct();
154 mClientExtensionOutputs.mHmacCreateSecret.Value() = aHmacCreateSecret;
157 } // namespace mozilla::dom