-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
164 lines (140 loc) · 5.26 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
<!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>
<h1> WebAuthn GopherJS Express Demo</h1>
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>
<button onclick="reset()">Reset</button>
<br/><br/>
<pre id="users"></pre>
<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 showUsers() {
}
function registerUser() {
username = $("#email").val()
if (username === "") {
alert("Please enter a username");
return;
}
const createPromiseFunc = (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
})
}
$.getJSON('/api/reg/begin/' + username, (data) => {
// Get the client credentials if we can
createPromiseFunc(data).then((credential) => {
let attestationObject = credential.response.attestationObject;
let clientDataJSON = credential.response.clientDataJSON;
let rawId = credential.rawId;
const registrationBody = {
id: credential.id,
rawId: bufferEncode(rawId),
type: credential.type,
response: {
attestationObject: bufferEncode(attestationObject),
clientDataJSON: bufferEncode(clientDataJSON),
},
};
console.log(registrationBody);
$.ajax({
type: 'POST',
url: '/api/reg/finish/' + username,
data: JSON.stringify(registrationBody),
success: function(data) {
alert('Registration Successful');
},
contentType: "application/json",
dataType: 'json'
});
}).catch((err) => {
alert(err);
});
});
}
function loginUser() {
username = $("#email").val()
if (username === "") {
alert("Please enter a username");
return;
}
const loginCredRequest = (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
})
}
$.getJSON('/api/login/begin/' + username, (data) => {
loginCredRequest(data).then((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;
const finishLoginObj = {
id: assertion.id,
rawId: bufferEncode(rawId),
type: assertion.type,
response: {
authenticatorData: bufferEncode(authData),
clientDataJSON: bufferEncode(clientDataJSON),
signature: bufferEncode(sig),
userHandle: bufferEncode(userHandle),
},
}
$.ajax({
type: 'POST',
url: '/api/login/finish/' + username,
data: JSON.stringify(finishLoginObj),
success: function(data) {
alert(JSON.stringify(data));
},
contentType: "application/json",
dataType: 'json'
});
}).catch((err) => {
alert(err);
});
});
}
</script>
</body>
</html>