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
|
class WebAuthnHandler {
static isWebAuthnSupported() {
return window.PublicKeyCredential;
}
static showErrorMessage(errorMessage) {
console.log("webauthn error: " + errorMessage);
let alertElement = document.getElementById("webauthn-error");
if (alertElement) {
alertElement.textContent += " (" + errorMessage + ")";
alertElement.classList.remove("hidden");
}
}
async isConditionalLoginSupported() {
return WebAuthnHandler.isWebAuthnSupported() &&
window.PublicKeyCredential.isConditionalMediationAvailable &&
window.PublicKeyCredential.isConditionalMediationAvailable();
}
async conditionalLogin(abortController) {
if (await this.isConditionalLoginSupported()) {
this.login("", abortController);
}
}
decodeBuffer(value) {
return Uint8Array.from(atob(value.replace(/-/g, "+").replace(/_/g, "/")), c => c.charCodeAt(0));
}
encodeBuffer(value) {
return btoa(String.fromCharCode.apply(null, new Uint8Array(value)))
.replace(/\+/g, "-")
.replace(/\//g, "_")
.replace(/=/g, "");
}
async post(urlKey, username, data) {
let url = document.body.dataset[urlKey];
if (username) {
url += "?username=" + username;
}
return fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Csrf-Token": getCsrfToken()
},
body: JSON.stringify(data),
});
}
async get(urlKey, username) {
let url = document.body.dataset[urlKey];
if (username) {
url += "?username=" + username;
}
return fetch(url);
}
async removeAllCredentials() {
try {
await this.post("webauthnDeleteAllUrl", null, {});
} catch (err) {
WebAuthnHandler.showErrorMessage(err);
return;
}
window.location.reload();
}
async register() {
let registerBeginResponse;
try {
registerBeginResponse = await this.get("webauthnRegisterBeginUrl");
} catch (err) {
WebAuthnHandler.showErrorMessage(err);
return;
}
let credentialCreationOptions = await registerBeginResponse.json();
credentialCreationOptions.publicKey.challenge = this.decodeBuffer(credentialCreationOptions.publicKey.challenge);
credentialCreationOptions.publicKey.user.id = this.decodeBuffer(credentialCreationOptions.publicKey.user.id);
if (Object.hasOwn(credentialCreationOptions.publicKey, 'excludeCredentials')) {
credentialCreationOptions.publicKey.excludeCredentials.forEach((credential) => credential.id = this.decodeBuffer(credential.id));
}
let attestation = await navigator.credentials.create(credentialCreationOptions);
let registrationFinishResponse;
try {
registrationFinishResponse = await this.post("webauthnRegisterFinishUrl", null, {
id: attestation.id,
rawId: this.encodeBuffer(attestation.rawId),
type: attestation.type,
response: {
attestationObject: this.encodeBuffer(attestation.response.attestationObject),
clientDataJSON: this.encodeBuffer(attestation.response.clientDataJSON),
},
});
} catch (err) {
WebAuthnHandler.showErrorMessage(err);
return;
}
if (!registrationFinishResponse.ok) {
throw new Error("Login failed with HTTP status code " + response.status);
}
let jsonData = await registrationFinishResponse.json();
window.location.href = jsonData.redirect;
}
async login(username, abortController) {
let loginBeginResponse;
try {
loginBeginResponse = await this.get("webauthnLoginBeginUrl", username);
} catch (err) {
WebAuthnHandler.showErrorMessage(err);
return;
}
let credentialRequestOptions = await loginBeginResponse.json();
credentialRequestOptions.publicKey.challenge = this.decodeBuffer(credentialRequestOptions.publicKey.challenge);
if (Object.hasOwn(credentialRequestOptions.publicKey, 'allowCredentials')) {
credentialRequestOptions.publicKey.allowCredentials.forEach((credential) => credential.id = this.decodeBuffer(credential.id));
}
if (abortController) {
credentialRequestOptions.signal = abortController.signal;
credentialRequestOptions.mediation = "conditional";
}
let assertion;
try {
assertion = await navigator.credentials.get(credentialRequestOptions);
}
catch (err) {
// Swallow aborted conditional logins
if (err instanceof DOMException && err.name == "AbortError") {
return;
}
WebAuthnHandler.showErrorMessage(err);
return;
}
if (!assertion) {
return;
}
let loginFinishResponse;
try {
loginFinishResponse = await this.post("webauthnLoginFinishUrl", username, {
id: assertion.id,
rawId: this.encodeBuffer(assertion.rawId),
type: assertion.type,
response: {
authenticatorData: this.encodeBuffer(assertion.response.authenticatorData),
clientDataJSON: this.encodeBuffer(assertion.response.clientDataJSON),
signature: this.encodeBuffer(assertion.response.signature),
userHandle: this.encodeBuffer(assertion.response.userHandle),
},
});
} catch (err) {
WebAuthnHandler.showErrorMessage(err);
return;
}
if (!loginFinishResponse.ok) {
throw new Error("Login failed with HTTP status code " + loginFinishResponse.status);
}
window.location.reload();
}
}
|