Source code

Revision control

Other Tools

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/. */
6
7
#include "mozilla/dom/Promise.h"
8
#include "mozilla/dom/PublicKeyCredential.h"
9
#include "mozilla/dom/WebAuthenticationBinding.h"
10
#include "nsCycleCollectionParticipant.h"
11
12
#ifdef OS_WIN
13
# include "WinWebAuthnManager.h"
14
#endif
15
16
namespace mozilla {
17
namespace dom {
18
19
NS_IMPL_CYCLE_COLLECTION_CLASS(PublicKeyCredential)
20
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(PublicKeyCredential, Credential)
21
tmp->mRawIdCachedObj = nullptr;
22
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
23
24
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(PublicKeyCredential, Credential)
25
NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
26
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mRawIdCachedObj)
27
NS_IMPL_CYCLE_COLLECTION_TRACE_END
28
29
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(PublicKeyCredential,
30
Credential)
31
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
32
33
NS_IMPL_ADDREF_INHERITED(PublicKeyCredential, Credential)
34
NS_IMPL_RELEASE_INHERITED(PublicKeyCredential, Credential)
35
36
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PublicKeyCredential)
37
NS_INTERFACE_MAP_END_INHERITING(Credential)
38
39
PublicKeyCredential::PublicKeyCredential(nsPIDOMWindowInner* aParent)
40
: Credential(aParent), mRawIdCachedObj(nullptr) {
41
mozilla::HoldJSObjects(this);
42
}
43
44
PublicKeyCredential::~PublicKeyCredential() { mozilla::DropJSObjects(this); }
45
46
JSObject* PublicKeyCredential::WrapObject(JSContext* aCx,
47
JS::Handle<JSObject*> aGivenProto) {
48
return PublicKeyCredential_Binding::Wrap(aCx, this, aGivenProto);
49
}
50
51
void PublicKeyCredential::GetRawId(JSContext* aCx,
52
JS::MutableHandle<JSObject*> aRetVal) {
53
if (!mRawIdCachedObj) {
54
mRawIdCachedObj = mRawId.ToArrayBuffer(aCx);
55
}
56
aRetVal.set(mRawIdCachedObj);
57
}
58
59
already_AddRefed<AuthenticatorResponse> PublicKeyCredential::Response() const {
60
RefPtr<AuthenticatorResponse> temp(mResponse);
61
return temp.forget();
62
}
63
64
nsresult PublicKeyCredential::SetRawId(CryptoBuffer& aBuffer) {
65
if (NS_WARN_IF(!mRawId.Assign(aBuffer))) {
66
return NS_ERROR_OUT_OF_MEMORY;
67
}
68
return NS_OK;
69
}
70
71
void PublicKeyCredential::SetResponse(RefPtr<AuthenticatorResponse> aResponse) {
72
mResponse = aResponse;
73
}
74
75
/* static */
76
already_AddRefed<Promise>
77
PublicKeyCredential::IsUserVerifyingPlatformAuthenticatorAvailable(
78
GlobalObject& aGlobal) {
79
nsIGlobalObject* globalObject = xpc::CurrentNativeGlobal(aGlobal.Context());
80
if (NS_WARN_IF(!globalObject)) {
81
return nullptr;
82
}
83
84
ErrorResult rv;
85
RefPtr<Promise> promise = Promise::Create(globalObject, rv);
86
if (rv.Failed()) {
87
return nullptr;
88
}
89
91
//
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
95
96
if (WinWebAuthnManager::IsUserVerifyingPlatformAuthenticatorAvailable()) {
97
promise->MaybeResolve(true);
98
return promise.forget();
99
}
100
101
#endif
102
103
promise->MaybeResolve(false);
104
return promise.forget();
105
}
106
107
/* static */
108
already_AddRefed<Promise>
109
PublicKeyCredential::IsExternalCTAP2SecurityKeySupported(
110
GlobalObject& aGlobal) {
111
nsIGlobalObject* globalObject = xpc::CurrentNativeGlobal(aGlobal.Context());
112
if (NS_WARN_IF(!globalObject)) {
113
return nullptr;
114
}
115
116
ErrorResult rv;
117
RefPtr<Promise> promise = Promise::Create(globalObject, rv);
118
if (rv.Failed()) {
119
return nullptr;
120
}
121
122
#ifdef OS_WIN
123
if (WinWebAuthnManager::AreWebAuthNApisAvailable()) {
124
promise->MaybeResolve(true);
125
return promise.forget();
126
}
127
#endif
128
129
promise->MaybeResolve(false);
130
return promise.forget();
131
}
132
133
void PublicKeyCredential::GetClientExtensionResults(
134
AuthenticationExtensionsClientOutputs& aResult) {
135
aResult = mClientExtensionOutputs;
136
}
137
138
void PublicKeyCredential::SetClientExtensionResultAppId(bool aResult) {
139
mClientExtensionOutputs.mAppid.Construct();
140
mClientExtensionOutputs.mAppid.Value() = aResult;
141
}
142
143
void PublicKeyCredential::SetClientExtensionResultHmacSecret(
144
bool aHmacCreateSecret) {
145
mClientExtensionOutputs.mHmacCreateSecret.Construct();
146
mClientExtensionOutputs.mHmacCreateSecret.Value() = aHmacCreateSecret;
147
}
148
149
} // namespace dom
150
} // namespace mozilla