-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.js
More file actions
85 lines (71 loc) · 2.19 KB
/
client.js
File metadata and controls
85 lines (71 loc) · 2.19 KB
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
//var webauthn = require('@webauthn/client');
import { solveRegistrationChallenge, solveLoginChallenge } from '@webauthn/client';
var webauthn = {
solveRegistrationChallenge,
solveLoginChallenge
};
function wrapPromiseFn(fn) {
return function wrappedPromiseFunction() {
var p = fn.apply(null, arguments);
p.catch(function(err) {
alert('Encountered an error: ' + err.message + '\nPlease refresh. If the error persists, contact the system administrator.');
});
};
}
async function register(finalTarget) {
var challenge = await fetch('/webauthn/register/challenge', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ id: 'generic', name: 'generic' })
}).then(res => res.json());
var creds = await webauthn.solveRegistrationChallenge(challenge);
await fetch('/webauthn/register', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(creds)
});
if (!finalTarget) {
location.reload();
} else {
location.replace(finalTarget);
}
}
async function login() {
var challenge = await fetch('/webauthn/login/challenge', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ name: 'generic' })
}).then(res => res.json());
var creds = await webauthn.solveLoginChallenge(challenge);
await fetch('/webauthn/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(creds)
});
location.reload();
}
async function logout() {
await fetch('/webauthn/logout', {
method: 'POST'
});
location.reload();
}
function maybeAddListener(id, fn) {
var el = document.getElementById(id);
if (el) el.addEventListener('click', fn);
}
document.addEventListener('DOMContentLoaded', function() {
maybeAddListener('initialRegister', wrapPromiseFn(register).bind(null, null));
maybeAddListener('login', wrapPromiseFn(login));
maybeAddListener('newKey', wrapPromiseFn(register).bind(null, null));
maybeAddListener('newKeyRemote', wrapPromiseFn(register).bind(null, '/'));
maybeAddListener('logout', wrapPromiseFn(logout));
}, false);