summaryrefslogtreecommitdiffstats
path: root/dom/webauthn/tests/browser/browser_webauthn_conditional_mediation.js
blob: fff1ec5daba12d2e35f9a345186bdceec407d322 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const TEST_URL = "https://example.com";

let gAuthenticatorId = add_virtual_authenticator();
let gExpectNotAllowedError = expectError("NotAllowed");
let gExpectAbortError = expectError("Abort");
let gPendingConditionalGetSubject = "webauthn:conditional-get-pending";
let gWebAuthnService = Cc["@mozilla.org/webauthn/service;1"].getService(
  Ci.nsIWebAuthnService
);

add_task(async function test_webauthn_modal_request_cancels_conditional_get() {
  // Open a new tab.
  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_URL);

  let browser = tab.linkedBrowser.browsingContext.embedderElement;
  let browsingContextId = browser.browsingContext.id;

  let transactionId = gWebAuthnService.hasPendingConditionalGet(
    browsingContextId,
    TEST_URL
  );
  Assert.equal(transactionId, 0, "should not have a pending conditional get");

  let requestStarted = TestUtils.topicObserved(gPendingConditionalGetSubject);

  let active = true;
  let condPromise = promiseWebAuthnGetAssertionDiscoverable(tab, "conditional")
    .then(arrivingHereIsBad)
    .catch(gExpectAbortError)
    .then(() => (active = false));

  await requestStarted;

  transactionId = gWebAuthnService.hasPendingConditionalGet(
    browsingContextId,
    TEST_URL
  );
  Assert.notEqual(transactionId, 0, "should have a pending conditional get");

  ok(active, "conditional request should still be active");

  let promptPromise = promiseNotification("webauthn-prompt-register-direct");
  let modalPromise = promiseWebAuthnMakeCredential(tab, "direct")
    .then(arrivingHereIsBad)
    .catch(gExpectNotAllowedError);

  await condPromise;

  ok(!active, "conditional request should not be active");

  // Cancel the modal request with the button.
  await promptPromise;
  PopupNotifications.panel.firstElementChild.secondaryButton.click();
  await modalPromise;

  // Close tab.
  await BrowserTestUtils.removeTab(tab);
});

add_task(async function test_webauthn_resume_conditional_get() {
  // Open a new tab.
  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_URL);

  let browser = tab.linkedBrowser.browsingContext.embedderElement;
  let browsingContextId = browser.browsingContext.id;

  let transactionId = gWebAuthnService.hasPendingConditionalGet(
    browsingContextId,
    TEST_URL
  );
  Assert.equal(transactionId, 0, "should not have a pending conditional get");

  let requestStarted = TestUtils.topicObserved(gPendingConditionalGetSubject);

  let active = true;
  let promise = promiseWebAuthnGetAssertionDiscoverable(tab, "conditional")
    .then(arrivingHereIsBad)
    .catch(gExpectNotAllowedError)
    .then(() => (active = false));

  await requestStarted;

  transactionId = gWebAuthnService.hasPendingConditionalGet(0, TEST_URL);
  Assert.equal(
    transactionId,
    0,
    "hasPendingConditionalGet should check the browsing context id"
  );

  transactionId = gWebAuthnService.hasPendingConditionalGet(
    browsingContextId,
    "https://example.org"
  );
  Assert.equal(
    transactionId,
    0,
    "hasPendingConditionalGet should check the origin"
  );

  transactionId = gWebAuthnService.hasPendingConditionalGet(
    browsingContextId,
    TEST_URL
  );
  Assert.notEqual(transactionId, 0, "should have a pending conditional get");

  ok(active, "request should still be active");

  gWebAuthnService.resumeConditionalGet(transactionId);
  await promise;

  ok(!active, "request should not be active");

  // Close tab.
  await BrowserTestUtils.removeTab(tab);
});

add_task(async function test_webauthn_select_autofill_entry() {
  // Open a new tab.
  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_URL);

  // Add credentials
  let cred1 = await addCredential(gAuthenticatorId, "example.com");
  let cred2 = await addCredential(gAuthenticatorId, "example.com");

  let browser = tab.linkedBrowser.browsingContext.embedderElement;
  let browsingContextId = browser.browsingContext.id;

  let transactionId = gWebAuthnService.hasPendingConditionalGet(
    browsingContextId,
    TEST_URL
  );
  Assert.equal(transactionId, 0, "should not have a pending conditional get");

  let requestStarted = TestUtils.topicObserved(gPendingConditionalGetSubject);

  let active = true;
  let promise = promiseWebAuthnGetAssertionDiscoverable(tab, "conditional")
    .catch(arrivingHereIsBad)
    .then(() => (active = false));

  await requestStarted;

  transactionId = gWebAuthnService.hasPendingConditionalGet(
    browsingContextId,
    TEST_URL
  );
  Assert.notEqual(transactionId, 0, "should have a pending conditional get");

  let autoFillEntries = gWebAuthnService.getAutoFillEntries(transactionId);
  ok(
    autoFillEntries.length == 2 &&
      autoFillEntries[0].rpId == "example.com" &&
      autoFillEntries[1].rpId == "example.com",
    "should have two autofill entries for example.com"
  );

  gWebAuthnService.selectAutoFillEntry(
    transactionId,
    autoFillEntries[0].credentialId
  );
  let result = await promise;

  ok(!active, "request should not be active");

  // Remove credentials
  gWebAuthnService.removeCredential(gAuthenticatorId, cred1);
  gWebAuthnService.removeCredential(gAuthenticatorId, cred2);

  // Close tab.
  await BrowserTestUtils.removeTab(tab);
});