browser_passive_requires_connected.js (4584B)
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( 17 async function test_passive_filters_out_not_connected_idp_and_leaves_a_connected() { 18 await SpecialPowers.pushPrefEnv({ 19 set: [ 20 [ 21 "dom.security.credentialmanagement.identity.select_first_in_ui_lists", 22 true, 23 ], 24 [ 25 "dom.security.credentialmanagement.identity.reject_delay.enabled", 26 false, 27 ], 28 ], 29 }); 30 31 const idpPrincipal = Services.scriptSecurityManager.createContentPrincipal( 32 Services.io.newURI("https://example.net"), 33 {} 34 ); 35 const rpPrincipal = Services.scriptSecurityManager.createContentPrincipal( 36 Services.io.newURI("https://example.com"), 37 {} 38 ); 39 40 // Set two accounts as registered 41 IdentityCredentialStorageService.setState( 42 rpPrincipal, 43 idpPrincipal, 44 "connected", 45 true, 46 false 47 ); 48 49 let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_URL); 50 51 let expectFailure = await SpecialPowers.spawn( 52 tab.linkedBrowser, 53 [], 54 async function () { 55 let promise = content.navigator.credentials.get({ 56 identity: { 57 mode: "passive", 58 providers: [ 59 { 60 configURL: 61 "https://example.org/browser/dom/credentialmanagement/identity/tests/browser/server_manifest.json", 62 clientId: "browser", 63 nonce: "nonce", 64 }, 65 ], 66 }, 67 }); 68 try { 69 let cred = await promise; 70 return cred.token; 71 } catch (err) { 72 return err; 73 } 74 } 75 ); 76 77 // If this were example.org, we didn't filter it out because it was was connected 78 ok(expectFailure, "expect a result from the second request."); 79 ok(expectFailure.name, "expect a DOMException which must have a name."); 80 81 let sameSiteResult = await SpecialPowers.spawn( 82 tab.linkedBrowser, 83 [], 84 async function () { 85 let promise = content.navigator.credentials.get({ 86 identity: { 87 mode: "passive", 88 providers: [ 89 { 90 configURL: 91 "https://example.org/browser/dom/credentialmanagement/identity/tests/browser/server_manifest.json", 92 clientId: "browser", 93 nonce: "nonce", 94 }, 95 { 96 configURL: 97 "https://www.example.com/browser/dom/credentialmanagement/identity/tests/browser/server_manifest.json", 98 clientId: "browser", 99 nonce: "nonce", 100 }, 101 ], 102 }, 103 }); 104 try { 105 let cred = await promise; 106 return cred.token; 107 } catch (err) { 108 return err; 109 } 110 } 111 ); 112 113 // If this is the right token, we didn't filter out an IDP because it was was connected 114 is(sameSiteResult, "result", "Result obtained!"); 115 116 let connectedResult = await SpecialPowers.spawn( 117 tab.linkedBrowser, 118 [], 119 async function () { 120 let promise = content.navigator.credentials.get({ 121 identity: { 122 mode: "passive", 123 providers: [ 124 { 125 configURL: 126 "https://example.org/browser/dom/credentialmanagement/identity/tests/browser/server_manifest.json", 127 clientId: "browser", 128 nonce: "nonce", 129 }, 130 { 131 configURL: 132 "https://example.net/browser/dom/credentialmanagement/identity/tests/browser/server_manifest.json", 133 clientId: "browser", 134 nonce: "nonce", 135 }, 136 ], 137 }, 138 }); 139 try { 140 let cred = await promise; 141 return cred.token; 142 } catch (err) { 143 return err; 144 } 145 } 146 ); 147 148 // If this is the right token, we didn't filter out an IDP because it was was connected 149 is(connectedResult, "result", "Result obtained!"); 150 151 // Close tabs. 152 await BrowserTestUtils.removeTab(tab); 153 await SpecialPowers.popPrefEnv(); 154 } 155 );