forked from hbolimovsky/webauthn-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
170 lines (150 loc) · 4.99 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebAuthn Demo</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
</head>
<body>
Username:
<br>
<input type="text" name="username" id="email" placeholder="i.e. foo@bar.com">
<br>
<br>
<button onclick="registerUser()">Register</button>
<button onclick="loginUser()">Login</button>
<script>
$(document).ready(function () {
// check whether current browser supports WebAuthn
if (!window.PublicKeyCredential) {
alert("Error: this browser does not support WebAuthn");
return;
}
});
// Base64 to ArrayBuffer
function bufferDecode(value) {
return Uint8Array.from(atob(value), c => c.charCodeAt(0));
}
// ArrayBuffer to URLBase64
function bufferEncode(value) {
return btoa(String.fromCharCode.apply(null, new Uint8Array(value)))
.replace(/\+/g, "-")
.replace(/\//g, "_")
.replace(/=/g, "");;
}
function registerUser() {
username = $("#email").val()
if (username === "") {
alert("Please enter a username");
return;
}
$.get(
'/register/begin/' + username,
null,
function (data) {
return data
},
'json')
.then((credentialCreationOptions) => {
console.log(credentialCreationOptions)
credentialCreationOptions.publicKey.challenge = bufferDecode(credentialCreationOptions.publicKey.challenge);
credentialCreationOptions.publicKey.user.id = bufferDecode(credentialCreationOptions.publicKey.user.id);
if (credentialCreationOptions.publicKey.excludeCredentials) {
for (var i = 0; i < credentialCreationOptions.publicKey.excludeCredentials.length; i++) {
credentialCreationOptions.publicKey.excludeCredentials[i].id = bufferDecode(credentialCreationOptions.publicKey.excludeCredentials[i].id);
}
}
return navigator.credentials.create({
publicKey: credentialCreationOptions.publicKey
})
})
.then((credential) => {
console.log(credential)
let attestationObject = credential.response.attestationObject;
let clientDataJSON = credential.response.clientDataJSON;
let rawId = credential.rawId;
$.post(
'/register/finish/' + username,
JSON.stringify({
id: credential.id,
rawId: bufferEncode(rawId),
type: credential.type,
response: {
attestationObject: bufferEncode(attestationObject),
clientDataJSON: bufferEncode(clientDataJSON),
},
}),
function (data) {
return data
},
'json')
})
.then((success) => {
alert("successfully registered " + username + "!")
return
})
.catch((error) => {
console.log(error)
alert("failed to register " + username)
})
}
function loginUser() {
username = $("#email").val()
if (username === "") {
alert("Please enter a username");
return;
}
$.get(
'/login/begin/' + username,
null,
function (data) {
return data
},
'json')
.then((credentialRequestOptions) => {
console.log(credentialRequestOptions)
credentialRequestOptions.publicKey.challenge = bufferDecode(credentialRequestOptions.publicKey.challenge);
credentialRequestOptions.publicKey.allowCredentials.forEach(function (listItem) {
listItem.id = bufferDecode(listItem.id)
});
return navigator.credentials.get({
publicKey: credentialRequestOptions.publicKey
})
})
.then((assertion) => {
console.log(assertion)
let authData = assertion.response.authenticatorData;
let clientDataJSON = assertion.response.clientDataJSON;
let rawId = assertion.rawId;
let sig = assertion.response.signature;
let userHandle = assertion.response.userHandle;
$.post(
'/login/finish/' + username,
JSON.stringify({
id: assertion.id,
rawId: bufferEncode(rawId),
type: assertion.type,
response: {
authenticatorData: bufferEncode(authData),
clientDataJSON: bufferEncode(clientDataJSON),
signature: bufferEncode(sig),
userHandle: bufferEncode(userHandle),
},
}),
function (data) {
return data
},
'json')
})
.then((success) => {
alert("successfully logged in " + username + "!")
return
})
.catch((error) => {
console.log(error)
alert("failed to register " + username)
})
}
</script>
</body>
</html>