tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_autoreauthn_doesnt_show_ui.js (4131B)


      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 file,
      3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 XPCOMUtils.defineLazyServiceGetter(
      8  this,
      9  "IdentityCredentialStorageService",
     10  "@mozilla.org/browser/identity-credential-storage-service;1",
     11  Ci.nsIIdentityCredentialStorageService
     12 );
     13 
     14 const TEST_URL = "https://example.com/";
     15 
     16 add_task(async function test_auto_reauthentication_doesnt_show_ui() {
     17  const idpPrincipal = Services.scriptSecurityManager.createContentPrincipal(
     18    Services.io.newURI("https://example.net"),
     19    {}
     20  );
     21  const rpPrincipal = Services.scriptSecurityManager.createContentPrincipal(
     22    Services.io.newURI("https://example.com"),
     23    {}
     24  );
     25 
     26  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_URL);
     27 
     28  let unlinked = await SpecialPowers.spawn(
     29    tab.linkedBrowser,
     30    [],
     31    async function () {
     32      let promise = content.navigator.credentials.get({
     33        identity: {
     34          mode: "passive",
     35          providers: [
     36            {
     37              configURL:
     38                "https://example.net/browser/dom/credentialmanagement/identity/tests/browser/server_manifest.json",
     39              clientId: "123",
     40              nonce: "nonce",
     41            },
     42          ],
     43        },
     44      });
     45      try {
     46        let cred = await promise;
     47        return cred.token;
     48      } catch (err) {
     49        return err;
     50      }
     51    }
     52  );
     53 
     54  ok(unlinked, "expect a result from the second request.");
     55  ok(unlinked.name, "expect a DOMException which must have a name.");
     56 
     57  // Set account as registered
     58  IdentityCredentialStorageService.setState(
     59    rpPrincipal,
     60    idpPrincipal,
     61    "connected",
     62    true,
     63    false
     64  );
     65  Services.perms.addFromPrincipal(
     66    rpPrincipal,
     67    "credential-allow-silent-access",
     68    Ci.nsIPermissionManager.ALLOW_ACTION,
     69    Ci.nsIPermissionManager.EXPIRE_SESSION
     70  );
     71  Services.perms.addFromPrincipal(
     72    rpPrincipal,
     73    "credential-allow-silent-access^" + idpPrincipal.origin,
     74    Ci.nsIPermissionManager.ALLOW_ACTION,
     75    Ci.nsIPermissionManager.EXPIRE_SESSION
     76  );
     77 
     78  let popupShown = BrowserTestUtils.waitForEvent(
     79    PopupNotifications.panel,
     80    "popupshown"
     81  );
     82 
     83  let notApprovedPromise = SpecialPowers.spawn(
     84    tab.linkedBrowser,
     85    [],
     86    async function () {
     87      let promise = content.navigator.credentials.get({
     88        identity: {
     89          mode: "passive",
     90          providers: [
     91            {
     92              configURL:
     93                "https://example.net/browser/dom/credentialmanagement/identity/tests/browser/server_manifest.json",
     94              clientId: "test",
     95              nonce: "nonce",
     96            },
     97          ],
     98        },
     99      });
    100      try {
    101        let cred = await promise;
    102        return cred.token;
    103      } catch (err) {
    104        return err;
    105      }
    106    }
    107  );
    108 
    109  await popupShown;
    110  tab.linkedBrowser.browsingContext.topChromeWindow.document
    111    .getElementsByClassName("popup-notification-secondary-button")[0]
    112    .click();
    113 
    114  let notApproved = await notApprovedPromise;
    115  ok(notApproved, "expect a result from the second request.");
    116  ok(notApproved.name, "expect a DOMException which must have a name.");
    117 
    118  let approvedAndLinked = await SpecialPowers.spawn(
    119    tab.linkedBrowser,
    120    [],
    121    async function () {
    122      let promise = content.navigator.credentials.get({
    123        identity: {
    124          mode: "passive",
    125          providers: [
    126            {
    127              configURL:
    128                "https://example.net/browser/dom/credentialmanagement/identity/tests/browser/server_manifest.json",
    129              clientId: "123",
    130              nonce: "nonce",
    131            },
    132          ],
    133        },
    134      });
    135      try {
    136        let cred = await promise;
    137        return cred.token;
    138      } catch (err) {
    139        return err;
    140      }
    141    }
    142  );
    143 
    144  is(approvedAndLinked, "result", "Result obtained!");
    145 
    146  // Clear state
    147  IdentityCredentialStorageService.disconnect(rpPrincipal, idpPrincipal);
    148 
    149  // Close tabs.
    150  await BrowserTestUtils.removeTab(tab);
    151  await SpecialPowers.popPrefEnv();
    152 });