summaryrefslogtreecommitdiffstats
path: root/dom/webauthn/tests/test_webauthn_authenticator_transports.html
blob: b1b855126e4a4766ba9698c9a3d1329149bbb0ec (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
<!DOCTYPE html>
<meta charset=utf-8>
<head>
  <title>W3C Web Authentication - Authenticator Transports</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="u2futil.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>

  <h1>W3C Web Authentication - Authenticator Transports</h1>
  <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1406467">Mozilla Bug 1406467</a>

  <script class="testbody" type="text/javascript">
    "use strict";

    function arrivingHereIsGood(aResult) {
      ok(true, "Good result! Received a: " + aResult);
    }

    function arrivingHereIsBad(aResult) {
      ok(false, "Bad result! Received a: " + aResult);
    }

    function expectNotAllowedError(aResult) {
      ok(aResult.toString().startsWith("NotAllowedError"), "Expecting a NotAllowedError, got " + aResult);
    }

    function expectInvalidStateError(aResult) {
      ok(aResult.toString().startsWith("InvalidStateError"), "Expecting a InvalidStateError, got " + aResult);
    }

    // Store the credential of the first successful make credential
    // operation so we can use it to get assertions later.
    let gCredential;

    // Start a new MakeCredential() request.
    function requestMakeCredential(excludeCredentials) {
      let publicKey = {
        rp: {id: document.domain, name: "none", icon: "none"},
        user: {id: new Uint8Array(), name: "none", icon: "none", displayName: "none"},
        challenge: crypto.getRandomValues(new Uint8Array(16)),
        timeout: 5000, // the minimum timeout is actually 15 seconds
        pubKeyCredParams: [{type: "public-key", alg: cose_alg_ECDSA_w_SHA256}],
        excludeCredentials
      };

      return navigator.credentials.create({publicKey});
    }

    // Start a new GetAssertion() request.
    function requestGetAssertion(allowCredentials) {
      let publicKey = {
        challenge: crypto.getRandomValues(new Uint8Array(16)),
        timeout: 5000, // the minimum timeout is actually 15 seconds
        rpId: document.domain,
        allowCredentials
      };

      return navigator.credentials.get({publicKey});
    }

    // Test make credential behavior.
    add_task(async function test_make_credential() {
      // Make a credential.
      await requestMakeCredential([])
         // Save the credential for later.
        .then(res => gCredential = res.rawId)
        .then(arrivingHereIsGood)
        .catch(arrivingHereIsBad);

      // Pass a random credential to exclude.
      await requestMakeCredential([{
        type: "public-key",
        id: crypto.getRandomValues(new Uint8Array(16)),
        transports: ["usb"],
      }]).then(arrivingHereIsGood)
         .catch(arrivingHereIsBad);

      // Pass gCredential with transport=usb.
      // The credential already exists, and the softoken consents to create,
      // so the error is InvalidState and not NotAllowed.
      await requestMakeCredential([{
        type: "public-key",
        id: gCredential,
        transports: ["usb"],
      }]).then(arrivingHereIsBad)
         .catch(expectInvalidStateError);

      // Pass gCredential with transport=nfc.
      // The softoken pretends to support all transports.
      // Also, as above, the credential exists and the token indicates consent.
      await requestMakeCredential([{
        type: "public-key",
        id: gCredential,
        transports: ["nfc"],
      }]).then(arrivingHereIsBad)
         .catch(expectInvalidStateError);

      // Pass gCredential with an empty transports list.
      // As above, the token indicates consent, so expect InvalidStateError.
      await requestMakeCredential([{
        type: "public-key",
        id: gCredential,
        transports: [],
      }]).then(arrivingHereIsBad)
         .catch(expectInvalidStateError);
    });

    // Test get assertion behavior.
    add_task(async function test_get_assertion() {
      // Request an assertion for gCredential.
      await requestGetAssertion([{
        type: "public-key",
        id: gCredential,
        transports: ["usb"],
      }]).then(arrivingHereIsGood)
         .catch(arrivingHereIsBad);

      // Request an assertion for a random credential. The token indicates
      // consent even though this credential doesn't exist, so expect an
      // InvalidStateError.
      await requestGetAssertion([{
        type: "public-key",
        id: crypto.getRandomValues(new Uint8Array(16)),
        transports: ["usb"],
      }]).then(arrivingHereIsBad)
         .catch(expectInvalidStateError);

      // Request an assertion for gCredential with transport=nfc.
      // The softoken pretends to support all transports.
      await requestGetAssertion([{
        type: "public-key",
        id: gCredential,
        transports: ["nfc"],
      }]).then(arrivingHereIsGood)
         .catch(arrivingHereIsBad);

      // Request an assertion for gCredential with an empty transports list.
      await requestGetAssertion([{
        type: "public-key",
        id: gCredential,
        transports: [],
      }]).then(arrivingHereIsGood)
         .catch(arrivingHereIsBad);
    });
  </script>

</body>
</html>