This repository has been archived by the owner on Dec 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 162
/
webauthn.js
255 lines (231 loc) · 8.24 KB
/
webauthn.js
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
function hexEncode(buf) {
return Array.from(buf)
.map(function (x) {
return ("0" + x.toString(16)).substr(-2)
})
.join("");
}
function hexDecode(str) {
return new Uint8Array(str.match(/../g).map(function (x) { return parseInt(x, 16) }));
}
function b64enc(buf) {
return base64js.fromByteArray(buf)
.replace(/\+/g, "-")
.replace(/\//g, "_")
.replace(/=/g, "");
}
// Don't drop any blanks
function b64RawEnc(buf) {
return base64js.fromByteArray(buf)
.replace(/\+/g, "-")
.replace(/\//g, "_");
}
function string2buffer(str) {
return (new Uint8Array(str.length)).map(function (x, i) {
return str.charCodeAt(i)
});
}
function buffer2string(buf) {
let str = "";
if (!(buf.constructor === Uint8Array)) {
buf = new Uint8Array(buf);
}
buf.map(function(x){ return str += String.fromCharCode(x) });
return str;
}
var state = {
createResponse: null,
publicKeyCredential: null,
credential: null,
user: {
name: "testuser@example.com",
displayName: "testuser",
},
}
function setUser() {
username = $("#input-email").val();
state.user.name = username.toLowerCase().replace(/\s/g, '') + "@example.com";
state.user.displayName = username.toLowerCase();
}
function checkUserExists() {
$.get('/user/' + state.user.name, {}, null, 'json')
.done(function (response) {
return true;
}).catch(function() {return false;});
}
function getCredentials() {
$.get('/credential/' + state.user.name, {}, null, 'json')
.done(function (response) {
console.log(response)
});
}
function makeCredential() {
hideErrorAlert();
console.log("Fetching options for new credential");
if ($("#input-email").val() === "") {
showErrorAlert("Please enter a username");
return;
}
setUser();
var credential = null;
swal({
title: 'Registering...',
text: 'Tap your security key to finish registration.',
imageUrl: "/images/securitykey.min.svg",
showCancelButton: true,
showConfirmButton: false,
focusConfirm: false,
focusCancel: false,
}).then(function () {
swal({
title: 'Registration Successful!',
text: 'You\'ve registered successfully.',
type: 'success',
timer: 2000
})
}).catch(function(error) {
console.log("Modal Error: " + error);
});
var attestation_type = $('#select-attestation').find(':selected').val();
var authenticator_attachment = $('#select-authenticator').find(':selected').val();
$.get('/makeCredential/' + state.user.name, {attType: attestation_type, authType: authenticator_attachment}, null, 'json')
.done(function (makeCredentialOptions) {
console.log("Credential Options Object");
console.log(makeCredentialOptions);
// Turn the challenge back into the accepted format
makeCredentialOptions.challenge = Uint8Array.from(atob(makeCredentialOptions.challenge), c => c.charCodeAt(0));
// Turn ID into a UInt8Array Buffer for some reason
makeCredentialOptions.user.id = Uint8Array.from(makeCredentialOptions.challenge)
console.log("Credential Options Formatted");
console.log(makeCredentialOptions);
console.log("Creating PublicKeyCredential");
navigator.credentials.create({
publicKey: makeCredentialOptions
}).then(function (newCredential) {
console.log("PublicKeyCredential Created");
console.log(newCredential);
state.createResponse = newCredential;
registerNewCredential(newCredential);
swal.clickConfirm()
}).catch(function (err) {
console.log(err);
swal.closeModal();
});
});
}
// This should be used to verify the auth data with the server
function registerNewCredential(newCredential) {
// Move data into Arrays incase it is super long
let attestationObject = new Uint8Array(newCredential.response.attestationObject);
let clientDataJSON = new Uint8Array(newCredential.response.clientDataJSON);
let rawId = new Uint8Array(newCredential.rawId);
$.post('/makeCredential', {
id: newCredential.id,
rawId: b64enc(rawId),
type: newCredential.type,
attObj: b64RawEnc(attestationObject),
clientData: b64RawEnc(clientDataJSON),
}).done(function(response){
if (response.success) {
window.location.href = "/dashboard/" + state.user.displayName;
} else {
console.log("Error creating credential");
console.log(response);
}
});
}
function addUserErrorMsg(msg) {
if (msg === "username") {
msg = 'Please add username';
} else {
msg = 'Please add email';
}
document.getElementById("user-create-error").innerHTML = msg;
}
function getAssertion() {
hideErrorAlert();
if ($("#input-email").val() === "") {
showErrorAlert("Please enter a username");
return;
}
setUser();
$.get('/user/' + state.user.name, {}, null, 'json').done(function (response) {
console.log(response);
}).then(function() {
swal({
title: 'Logging In...',
text: 'Tap your security key to login.',
imageUrl: "/images/securitykey.min.svg",
showCancelButton: true,
showConfirmButton: false,
focusConfirm: false,
focusCancel: false,
}).then(function () {
swal({
title: 'Logged In!',
text: 'You\'re logged in successfully.',
type: 'success',
timer: 2000
})
}).catch(function(error) {
console.log("Modal Error: " + error);
});
}).catch(function(error) {
showErrorAlert(error.responseText);
return;
});
$.get('/assertion/' + state.user.name, {
}, null, 'json')
.done(function (makeAssertionOptions) {
makeAssertionOptions.challenge = Uint8Array.from(atob(makeAssertionOptions.challenge), c => c.charCodeAt(0));
makeAssertionOptions.allowCredentials.forEach(function (listItem) {
var fixedId = listItem.id.replace(/\_/g, "/").replace(/\-/g, "+")
listItem.id = Uint8Array.from(atob(fixedId), c => c.charCodeAt(0));
});
console.log(makeAssertionOptions);
navigator.credentials.get({ publicKey: makeAssertionOptions })
.then(function (credential) {
console.log(credential);
verifyAssertion(credential);
swal.clickConfirm();
}).catch(function (err) {
console.log(err);
showErrorAlert(err.message);
swal.closeModal();
});
});
}
function verifyAssertion(assertedCredential) {
// Move data into Arrays incase it is super long
let authData = new Uint8Array(assertedCredential.response.authenticatorData);
let clientDataJSON = new Uint8Array(assertedCredential.response.clientDataJSON);
let rawId = new Uint8Array(assertedCredential.rawId);
let sig = new Uint8Array(assertedCredential.response.signature);
$.post('/assertion', {
id: assertedCredential.id,
rawId: b64enc(rawId),
type: assertedCredential.type,
authData: b64RawEnc(authData),
clientData: b64RawEnc(clientDataJSON),
signature: hexEncode(sig),
}).done(function(response){
console.log(response)
if (response.success) {
window.location.href = "/dashboard/" + state.user.displayName;
} else {
showErrorAlert("Error Doing Assertion");
swal.closeModal();
}
});
}
function setCurrentUser(userResponse) {
state.user.name = userResponse.name;
state.user.displayName = userResponse.display_name;
}
function showErrorAlert(msg) {
$("#alert-msg").text(msg);
$("#user-alert").show();
}
function hideErrorAlert() {
$("#user-alert").hide();
}