summaryrefslogtreecommitdiffhomepage
path: root/example/public/index.html
blob: 519b25186acde15053e7ce395e96131697711ac6 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src="https://unpkg.com/@simplewebauthn/browser/dist/bundle/index.es5.umd.min.js"></script>
    <script>
      const { startAuthentication } = SimpleWebAuthnBrowser;
      /**
       * Conditional UI test
       *
       * 1. Start Chrome Canary 105+ with the requisite Conditional UI flag:
       *
       * open -a /Applications/Google\ Chrome\ Canary.app --args --enable-features=WebAuthenticationConditionalUI
       *
       * 2. Create an entry in chrome://settings/passwords (temporary requirement) e.g.:
       *
       *   - Site: https://example.simplewebauthn.dev/
       *   - Username: user@example.simplewebauthn.dev
       *   - Password: whatever
       *
       * 3. Register a credential
       *
       * 4. Reload the page
       *
       * 5. Interact with the username field above the Authenticate button
       *
       * Notes:
       *
       * I'm currently trying to get to calling WebAuthn as fast as I can here, there's a
       * Chrome race condition with autofill that sometimes prevents a credential from appearing.
       *
       * See: https://bugs.chromium.org/p/chromium/issues/detail?id=1322967&q=component%3ABlink%3EWebAuthentication&can=2
       *
       * I've been assured this race condition is temporary, at which point we'll probably be able
       * to include this just before </body> as we'd typically do. And at that point we can
       * probably use async/await as well for more sane-looking code.
       */
      fetch('/generate-authentication-options')
        .then(resp => resp.json())
        .then(opts => {
          console.log('Authentication Options (Autofill)', opts);
          startAuthentication(opts, true)
            .then(async asseResp => {
              // We can assume the DOM has loaded by now because it had to for the user to be able
              // to interact with an input to choose a credential from the autofill
              const elemSuccess = document.querySelector('#authSuccess');
              const elemError = document.querySelector('#authError');
              const elemDebug = document.querySelector('#authDebug');

              printDebug(
                elemDebug,
                'Authentication Response (Autofill)',
                JSON.stringify(asseResp, null, 2),
              );

              const verificationResp = await fetch('/verify-authentication', {
                method: 'POST',
                headers: {
                  'Content-Type': 'application/json',
                },
                body: JSON.stringify(asseResp),
              });

              const verificationJSON = await verificationResp.json();
              printDebug(
                elemDebug,
                'Server Response (Autofill)',
                JSON.stringify(verificationJSON, null, 2),
              );

              if (verificationJSON && verificationJSON.verified) {
                elemSuccess.innerHTML = `User authenticated!`;
              } else {
                elemError.innerHTML = `Oh no, something went wrong! Response: <pre>${JSON.stringify(
                  verificationJSON,
                )}</pre>`;
              }
            })
            .catch(err => {
              console.error('(Autofill)', err);
            });
        });
    </script>
    <link rel="stylesheet" href="./styles.css" />
    <title>SimpleWebAuthn Example Site</title>
  </head>
  <body>
    <div class="container">
      <h1>SimpleWebAuthn Example Site</h1>

      <div class="controls">
        <section id="registration">
          <button id="btnRegBegin">
            <strong>🚪&nbsp;Register</strong>
          </button>
          <p id="regSuccess" class="success"></p>
          <p id="regError" class="error"></p>
          <details open>
            <summary>Console</summary>
            <textarea id="regDebug" spellcheck="false"></textarea>
          </details>
        </section>

        <section id="authentication">
          <form onsubmit="stopSubmit(event)">
            <section id="inputUsername">
              <label for="username">Username</label>
              <input type="text" name="username" autocomplete="username webauthn" autofocus />
              <br />
              <label for="password">Password</label>
              <input type="password" name="password" autocomplete="current-password webauthn" />
              <br />
            </section>
            <button id="btnAuthBegin">
              <strong>🔐&nbsp;Authenticate</strong>
            </button>
          </form>
          <p id="authSuccess" class="success"></p>
          <p id="authError" class="error"></p>
          <details open>
            <summary>Console</summary>
            <textarea id="authDebug" spellcheck="false"></textarea>
          </details>
        </section>
      </div>

      <p class="systemError"></p>
    </div>
    <script>
      const { browserSupportsWebAuthn, startRegistration } = SimpleWebAuthnBrowser;

      function stopSubmit(event) {
        event.preventDefault();
      }

      /**
       * A simple way to control how debug content is written to a debug console element
       */
      function printDebug(elemDebug, title, output) {
        if (elemDebug.innerHTML !== '') {
          elemDebug.innerHTML += '\n';
        }
        elemDebug.innerHTML += `// ${title}\n`;
        elemDebug.innerHTML += `${output}\n`;
      }

      // Hide the Begin button if the browser is incapable of using WebAuthn
      if (!browserSupportsWebAuthn()) {
        document.querySelector('.controls').style.display = 'none';
        document.querySelector('.systemError').innerText =
          "It seems this browser doesn't support WebAuthn...";
      } else {
        function hideAuthForm() {
          document.getElementById('inputUsername').style.display = 'none';
        }

        /**
         * Registration
         */
        document.querySelector('#btnRegBegin').addEventListener('click', async () => {
          const elemSuccess = document.querySelector('#regSuccess');
          const elemError = document.querySelector('#regError');
          const elemDebug = document.querySelector('#regDebug');

          // Reset success/error messages
          elemSuccess.innerHTML = '';
          elemError.innerHTML = '';
          elemDebug.innerHTML = '';

          const resp = await fetch('/generate-registration-options');

          let attResp;
          try {
            const opts = await resp.json();

            printDebug(elemDebug, 'Registration Options', JSON.stringify(opts, null, 2));

            hideAuthForm();

            attResp = await startRegistration(opts);
            printDebug(elemDebug, 'Registration Response', JSON.stringify(attResp, null, 2));
          } catch (error) {
            if (error.name === 'InvalidStateError') {
              elemError.innerText = 'Error: Authenticator was probably already registered by user';
            } else {
              elemError.innerText = error;
            }

            throw error;
          }

          const verificationResp = await fetch('/verify-registration', {
            method: 'POST',
            headers: {
              'Content-Type': 'application/json',
            },
            body: JSON.stringify(attResp),
          });

          const verificationJSON = await verificationResp.json();
          printDebug(elemDebug, 'Server Response', JSON.stringify(verificationJSON, null, 2));

          if (verificationJSON && verificationJSON.verified) {
            elemSuccess.innerHTML = `Authenticator registered!`;
          } else {
            elemError.innerHTML = `Oh no, something went wrong! Response: <pre>${JSON.stringify(
              verificationJSON,
            )}</pre>`;
          }
        });

        /**
         * Authentication
         */
        document.querySelector('#btnAuthBegin').addEventListener('click', async () => {
          const elemSuccess = document.querySelector('#authSuccess');
          const elemError = document.querySelector('#authError');
          const elemDebug = document.querySelector('#authDebug');

          // Reset success/error messages
          elemSuccess.innerHTML = '';
          elemError.innerHTML = '';
          elemDebug.innerHTML = '';

          const resp = await fetch('/generate-authentication-options');

          let asseResp;
          try {
            const opts = await resp.json();
            printDebug(elemDebug, 'Authentication Options', JSON.stringify(opts, null, 2));

            hideAuthForm();

            asseResp = await startAuthentication(opts);
            printDebug(elemDebug, 'Authentication Response', JSON.stringify(asseResp, null, 2));
          } catch (error) {
            elemError.innerText = error;
            throw new Error(error);
          }

          const verificationResp = await fetch('/verify-authentication', {
            method: 'POST',
            headers: {
              'Content-Type': 'application/json',
            },
            body: JSON.stringify(asseResp),
          });

          const verificationJSON = await verificationResp.json();
          printDebug(elemDebug, 'Server Response', JSON.stringify(verificationJSON, null, 2));

          if (verificationJSON && verificationJSON.verified) {
            elemSuccess.innerHTML = `User authenticated!`;
          } else {
            elemError.innerHTML = `Oh no, something went wrong! Response: <pre>${JSON.stringify(
              verificationJSON,
            )}</pre>`;
          }
        });
      }
    </script>
  </body>
</html>