Skip to content

Commit 95f2ce3

Browse files
committed
Bug 2005077 - factor promise creation out of WebAuthnHandler. r=keeler
Differential Revision: https://phabricator.services.mozilla.com/D275702
1 parent 6a697c8 commit 95f2ce3

File tree

4 files changed

+139
-173
lines changed

4 files changed

+139
-173
lines changed

dom/credentialmanagement/CredentialsContainer.cpp

Lines changed: 48 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -29,45 +29,6 @@ NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CredentialsContainer)
2929
NS_INTERFACE_MAP_ENTRY(nsISupports)
3030
NS_INTERFACE_MAP_END
3131

32-
already_AddRefed<Promise> CreatePromise(nsPIDOMWindowInner* aParent,
33-
ErrorResult& aRv) {
34-
MOZ_ASSERT(aParent);
35-
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aParent);
36-
if (NS_WARN_IF(!global)) {
37-
aRv.Throw(NS_ERROR_FAILURE);
38-
return nullptr;
39-
}
40-
RefPtr<Promise> promise = Promise::Create(global, aRv);
41-
if (NS_WARN_IF(aRv.Failed())) {
42-
return nullptr;
43-
}
44-
return promise.forget();
45-
}
46-
47-
already_AddRefed<Promise> CreateAndRejectWithNotAllowed(
48-
nsPIDOMWindowInner* aParent, ErrorResult& aRv) {
49-
MOZ_ASSERT(aParent);
50-
RefPtr<Promise> promise = CreatePromise(aParent, aRv);
51-
if (!promise) {
52-
return nullptr;
53-
}
54-
promise->MaybeRejectWithNotAllowedError(
55-
"CredentialContainer request is not allowed."_ns);
56-
return promise.forget();
57-
}
58-
59-
already_AddRefed<Promise> CreateAndRejectWithNotSupported(
60-
nsPIDOMWindowInner* aParent, ErrorResult& aRv) {
61-
MOZ_ASSERT(aParent);
62-
RefPtr<Promise> promise = CreatePromise(aParent, aRv);
63-
if (!promise) {
64-
return nullptr;
65-
}
66-
promise->MaybeRejectWithNotSupportedError(
67-
"CredentialContainer request is not supported."_ns);
68-
return promise.forget();
69-
}
70-
7132
static bool IsInActiveTab(nsPIDOMWindowInner* aParent) {
7233
// Returns whether aParent is an inner window somewhere in the active tab.
7334
// The active tab is the selected (i.e. visible) tab in the focused window.
@@ -155,6 +116,11 @@ JSObject* CredentialsContainer::WrapObject(JSContext* aCx,
155116

156117
already_AddRefed<Promise> CredentialsContainer::Get(
157118
const CredentialRequestOptions& aOptions, ErrorResult& aRv) {
119+
RefPtr<Promise> promise = Promise::Create(mParent->AsGlobal(), aRv);
120+
if (aRv.Failed()) {
121+
return nullptr;
122+
}
123+
158124
uint64_t totalOptions = 0;
159125
if (aOptions.mPublicKey.WasPassed() &&
160126
StaticPrefs::security_webauth_webauthn()) {
@@ -165,7 +131,9 @@ already_AddRefed<Promise> CredentialsContainer::Get(
165131
totalOptions += 1;
166132
}
167133
if (totalOptions > 1) {
168-
return CreateAndRejectWithNotSupported(mParent, aRv);
134+
promise->MaybeRejectWithNotSupportedError(
135+
"CredentialsContainer request is not supported."_ns);
136+
return promise.forget();
169137
}
170138

171139
bool conditionallyMediated =
@@ -176,33 +144,27 @@ already_AddRefed<Promise> CredentialsContainer::Get(
176144
if (!FeaturePolicyUtils::IsFeatureAllowed(
177145
mParent->GetExtantDoc(), u"publickey-credentials-get"_ns) ||
178146
!(IsInActiveTab(mParent) || conditionallyMediated)) {
179-
return CreateAndRejectWithNotAllowed(mParent, aRv);
147+
promise->MaybeRejectWithNotAllowedError(
148+
"CredentialsContainer request is not allowed."_ns);
149+
return promise.forget();
180150
}
181151

182152
if (conditionallyMediated &&
183153
!StaticPrefs::security_webauthn_enable_conditional_mediation()) {
184-
RefPtr<Promise> promise = CreatePromise(mParent, aRv);
185-
if (!promise) {
186-
return nullptr;
187-
}
188154
promise->MaybeRejectWithTypeError<MSG_INVALID_ENUM_VALUE>(
189155
"mediation", "conditional", "CredentialMediationRequirement");
190156
return promise.forget();
191157
}
192158

193159
EnsureWebAuthnHandler();
194-
return mWebAuthnHandler->GetAssertion(aOptions.mPublicKey.Value(),
195-
conditionallyMediated,
196-
aOptions.mSignal, aRv);
160+
mWebAuthnHandler->GetAssertion(aOptions.mPublicKey.Value(),
161+
conditionallyMediated, aOptions.mSignal,
162+
promise);
163+
return promise.forget();
197164
}
198165

199166
if (aOptions.mIdentity.WasPassed() &&
200167
StaticPrefs::dom_security_credentialmanagement_identity_enabled()) {
201-
RefPtr<Promise> promise = CreatePromise(mParent, aRv);
202-
if (!promise) {
203-
return nullptr;
204-
}
205-
206168
if (conditionallyMediated) {
207169
promise->MaybeRejectWithTypeError<MSG_INVALID_ENUM_VALUE>(
208170
"mediation", "conditional", "CredentialMediationRequirement");
@@ -224,19 +186,28 @@ already_AddRefed<Promise> CredentialsContainer::Get(
224186
return promise.forget();
225187
}
226188

227-
return CreateAndRejectWithNotSupported(mParent, aRv);
189+
promise->MaybeRejectWithNotSupportedError(
190+
"CredentialsContainer request is not supported."_ns);
191+
return promise.forget();
228192
}
229193

230194
already_AddRefed<Promise> CredentialsContainer::Create(
231195
const CredentialCreationOptions& aOptions, ErrorResult& aRv) {
196+
RefPtr<Promise> promise = Promise::Create(mParent->AsGlobal(), aRv);
197+
if (aRv.Failed()) {
198+
return nullptr;
199+
}
200+
232201
// Count the types of options provided. Must not be >1.
233202
uint64_t totalOptions = 0;
234203
if (aOptions.mPublicKey.WasPassed() &&
235204
StaticPrefs::security_webauth_webauthn()) {
236205
totalOptions += 1;
237206
}
238207
if (totalOptions > 1) {
239-
return CreateAndRejectWithNotSupported(mParent, aRv);
208+
promise->MaybeRejectWithNotSupportedError(
209+
"CredentialsContainer request is not supported."_ns);
210+
return promise.forget();
240211
}
241212

242213
if (aOptions.mPublicKey.WasPassed() &&
@@ -251,32 +222,47 @@ already_AddRefed<Promise> CredentialsContainer::Create(
251222
if (!FeaturePolicyUtils::IsFeatureAllowed(
252223
mParent->GetExtantDoc(), u"publickey-credentials-create"_ns) ||
253224
!hasRequiredActivation) {
254-
return CreateAndRejectWithNotAllowed(mParent, aRv);
225+
promise->MaybeRejectWithNotAllowedError(
226+
"CredentialsContainer request is not allowed."_ns);
227+
return promise.forget();
255228
}
256229

257230
EnsureWebAuthnHandler();
258-
return mWebAuthnHandler->MakeCredential(aOptions.mPublicKey.Value(),
259-
aOptions.mSignal, aRv);
231+
mWebAuthnHandler->MakeCredential(aOptions.mPublicKey.Value(),
232+
aOptions.mSignal, promise);
233+
return promise.forget();
260234
}
261235

262-
return CreateAndRejectWithNotSupported(mParent, aRv);
236+
promise->MaybeRejectWithNotSupportedError(
237+
"CredentialsContainer request is not supported."_ns);
238+
return promise.forget();
263239
}
264240

265241
already_AddRefed<Promise> CredentialsContainer::Store(
266242
const Credential& aCredential, ErrorResult& aRv) {
243+
RefPtr<Promise> promise = Promise::Create(mParent->AsGlobal(), aRv);
244+
if (aRv.Failed()) {
245+
return nullptr;
246+
}
247+
267248
nsString type;
268249
aCredential.GetType(type);
269250
if (type.EqualsLiteral("public-key") &&
270251
StaticPrefs::security_webauth_webauthn()) {
271252
if (!IsSameOriginWithAncestors(mParent) || !IsInActiveTab(mParent)) {
272-
return CreateAndRejectWithNotAllowed(mParent, aRv);
253+
promise->MaybeRejectWithNotAllowedError(
254+
"CredentialsContainer request is not allowed."_ns);
255+
return promise.forget();
273256
}
274257

275258
EnsureWebAuthnHandler();
276-
return mWebAuthnHandler->Store(aCredential, aRv);
259+
mWebAuthnHandler->Store(aCredential, promise);
260+
return promise.forget();
277261
}
278262

279-
return CreateAndRejectWithNotSupported(mParent, aRv);
263+
promise->MaybeRejectWithNotSupportedError(
264+
"CredentialsContainer request is not supported."_ns);
265+
return promise.forget();
280266
}
281267

282268
already_AddRefed<Promise> CredentialsContainer::PreventSilentAccess(

dom/webauthn/PublicKeyCredential.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,17 @@ PublicKeyCredential::IsUserVerifyingPlatformAuthenticatorAvailable(
122122
return nullptr;
123123
}
124124

125+
RefPtr<Promise> promise =
126+
Promise::Create(xpc::CurrentNativeGlobal(aGlobal.Context()), aError);
127+
if (aError.Failed()) {
128+
return nullptr;
129+
}
130+
125131
RefPtr<WebAuthnHandler> handler =
126132
window->Navigator()->Credentials()->GetWebAuthnHandler();
127-
return handler->IsUVPAA(aGlobal, aError);
133+
handler->IsUVPAA(promise);
134+
135+
return promise.forget();
128136
}
129137

130138
/* static */

0 commit comments

Comments
 (0)