2024-07-07 15:32:30 +00:00
|
|
|
import {encodeURLEncodedBase64, decodeURLEncodedBase64} from '../utils.ts';
|
|
|
|
import {showElem} from '../utils/dom.ts';
|
|
|
|
import {GET, POST} from '../modules/fetch.ts';
|
2022-01-14 15:03:31 +00:00
|
|
|
|
2023-09-19 00:50:30 +00:00
|
|
|
const {appSubUrl} = window.config;
|
2022-01-14 15:03:31 +00:00
|
|
|
|
2023-06-06 05:29:37 +00:00
|
|
|
export async function initUserAuthWebAuthn() {
|
2024-07-25 23:26:41 +00:00
|
|
|
const elPrompt = document.querySelector('.user.signin.webauthn-prompt');
|
|
|
|
const elSignInPasskeyBtn = document.querySelector('.signin-passkey');
|
|
|
|
if (!elPrompt && !elSignInPasskeyBtn) {
|
2024-07-23 17:52:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-29 22:50:03 +00:00
|
|
|
if (!detectWebAuthnSupport()) {
|
2022-01-14 15:03:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-29 22:50:03 +00:00
|
|
|
if (elSignInPasskeyBtn) {
|
|
|
|
elSignInPasskeyBtn.addEventListener('click', loginPasskey);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (elPrompt) {
|
|
|
|
login2FA();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function loginPasskey() {
|
|
|
|
const res = await GET(`${appSubUrl}/user/webauthn/passkey/assertion`);
|
|
|
|
if (!res.ok) {
|
|
|
|
webAuthnError('unknown');
|
2022-01-14 15:03:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-29 22:50:03 +00:00
|
|
|
const options = await res.json();
|
|
|
|
options.publicKey.challenge = decodeURLEncodedBase64(options.publicKey.challenge);
|
|
|
|
for (const cred of options.publicKey.allowCredentials ?? []) {
|
|
|
|
cred.id = decodeURLEncodedBase64(cred.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const credential = await navigator.credentials.get({
|
|
|
|
publicKey: options.publicKey,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Move data into Arrays in case it is super long
|
|
|
|
const authData = new Uint8Array(credential.response.authenticatorData);
|
|
|
|
const clientDataJSON = new Uint8Array(credential.response.clientDataJSON);
|
|
|
|
const rawId = new Uint8Array(credential.rawId);
|
|
|
|
const sig = new Uint8Array(credential.response.signature);
|
|
|
|
const userHandle = new Uint8Array(credential.response.userHandle);
|
|
|
|
|
|
|
|
const res = await POST(`${appSubUrl}/user/webauthn/passkey/login`, {
|
|
|
|
data: {
|
|
|
|
id: credential.id,
|
|
|
|
rawId: encodeURLEncodedBase64(rawId),
|
|
|
|
type: credential.type,
|
|
|
|
clientExtensionResults: credential.getClientExtensionResults(),
|
|
|
|
response: {
|
|
|
|
authenticatorData: encodeURLEncodedBase64(authData),
|
|
|
|
clientDataJSON: encodeURLEncodedBase64(clientDataJSON),
|
|
|
|
signature: encodeURLEncodedBase64(sig),
|
|
|
|
userHandle: encodeURLEncodedBase64(userHandle),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (res.status === 500) {
|
|
|
|
webAuthnError('unknown');
|
|
|
|
return;
|
|
|
|
} else if (!res.ok) {
|
|
|
|
webAuthnError('unable-to-process');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const reply = await res.json();
|
|
|
|
|
|
|
|
window.location.href = reply?.redirect ?? `${appSubUrl}/`;
|
|
|
|
} catch (err) {
|
|
|
|
webAuthnError('general', err.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function login2FA() {
|
2023-09-19 00:50:30 +00:00
|
|
|
const res = await GET(`${appSubUrl}/user/webauthn/assertion`);
|
2024-06-29 22:50:03 +00:00
|
|
|
if (!res.ok) {
|
2023-06-06 05:29:37 +00:00
|
|
|
webAuthnError('unknown');
|
|
|
|
return;
|
|
|
|
}
|
2024-06-29 22:50:03 +00:00
|
|
|
|
2023-06-06 05:29:37 +00:00
|
|
|
const options = await res.json();
|
|
|
|
options.publicKey.challenge = decodeURLEncodedBase64(options.publicKey.challenge);
|
2024-06-29 22:50:03 +00:00
|
|
|
for (const cred of options.publicKey.allowCredentials ?? []) {
|
2023-06-06 05:29:37 +00:00
|
|
|
cred.id = decodeURLEncodedBase64(cred.id);
|
|
|
|
}
|
2024-06-29 22:50:03 +00:00
|
|
|
|
2023-06-06 05:29:37 +00:00
|
|
|
try {
|
2023-06-07 11:20:18 +00:00
|
|
|
const credential = await navigator.credentials.get({
|
2024-03-22 14:06:53 +00:00
|
|
|
publicKey: options.publicKey,
|
2023-06-07 11:20:18 +00:00
|
|
|
});
|
2023-06-06 05:29:37 +00:00
|
|
|
await verifyAssertion(credential);
|
|
|
|
} catch (err) {
|
|
|
|
if (!options.publicKey.extensions?.appid) {
|
|
|
|
webAuthnError('general', err.message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delete options.publicKey.extensions.appid;
|
|
|
|
try {
|
2023-06-07 11:20:18 +00:00
|
|
|
const credential = await navigator.credentials.get({
|
2024-03-22 14:06:53 +00:00
|
|
|
publicKey: options.publicKey,
|
2023-06-07 11:20:18 +00:00
|
|
|
});
|
2023-06-06 05:29:37 +00:00
|
|
|
await verifyAssertion(credential);
|
|
|
|
} catch (err) {
|
|
|
|
webAuthnError('general', err.message);
|
|
|
|
}
|
|
|
|
}
|
2022-01-14 15:03:31 +00:00
|
|
|
}
|
|
|
|
|
2023-06-06 05:29:37 +00:00
|
|
|
async function verifyAssertion(assertedCredential) {
|
2023-06-07 11:20:18 +00:00
|
|
|
// Move data into Arrays in case it is super long
|
2022-01-14 15:03:31 +00:00
|
|
|
const authData = new Uint8Array(assertedCredential.response.authenticatorData);
|
|
|
|
const clientDataJSON = new Uint8Array(assertedCredential.response.clientDataJSON);
|
|
|
|
const rawId = new Uint8Array(assertedCredential.rawId);
|
|
|
|
const sig = new Uint8Array(assertedCredential.response.signature);
|
|
|
|
const userHandle = new Uint8Array(assertedCredential.response.userHandle);
|
2023-06-06 05:29:37 +00:00
|
|
|
|
2023-09-19 00:50:30 +00:00
|
|
|
const res = await POST(`${appSubUrl}/user/webauthn/assertion`, {
|
|
|
|
data: {
|
2022-01-14 15:03:31 +00:00
|
|
|
id: assertedCredential.id,
|
2023-02-01 07:24:10 +00:00
|
|
|
rawId: encodeURLEncodedBase64(rawId),
|
2022-01-14 15:03:31 +00:00
|
|
|
type: assertedCredential.type,
|
|
|
|
clientExtensionResults: assertedCredential.getClientExtensionResults(),
|
|
|
|
response: {
|
2023-02-01 07:24:10 +00:00
|
|
|
authenticatorData: encodeURLEncodedBase64(authData),
|
|
|
|
clientDataJSON: encodeURLEncodedBase64(clientDataJSON),
|
|
|
|
signature: encodeURLEncodedBase64(sig),
|
|
|
|
userHandle: encodeURLEncodedBase64(userHandle),
|
2022-01-14 15:03:31 +00:00
|
|
|
},
|
2023-09-19 00:50:30 +00:00
|
|
|
},
|
2022-01-14 15:03:31 +00:00
|
|
|
});
|
2023-06-06 05:29:37 +00:00
|
|
|
if (res.status === 500) {
|
|
|
|
webAuthnError('unknown');
|
|
|
|
return;
|
2024-06-29 22:50:03 +00:00
|
|
|
} else if (!res.ok) {
|
2023-06-06 05:29:37 +00:00
|
|
|
webAuthnError('unable-to-process');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const reply = await res.json();
|
2022-01-14 15:03:31 +00:00
|
|
|
|
2023-06-06 05:29:37 +00:00
|
|
|
window.location.href = reply?.redirect ?? `${appSubUrl}/`;
|
2023-02-01 07:24:10 +00:00
|
|
|
}
|
|
|
|
|
2023-06-06 05:29:37 +00:00
|
|
|
async function webauthnRegistered(newCredential) {
|
2022-01-14 15:03:31 +00:00
|
|
|
const attestationObject = new Uint8Array(newCredential.response.attestationObject);
|
|
|
|
const clientDataJSON = new Uint8Array(newCredential.response.clientDataJSON);
|
|
|
|
const rawId = new Uint8Array(newCredential.rawId);
|
|
|
|
|
2023-09-19 00:50:30 +00:00
|
|
|
const res = await POST(`${appSubUrl}/user/settings/security/webauthn/register`, {
|
|
|
|
data: {
|
2022-01-14 15:03:31 +00:00
|
|
|
id: newCredential.id,
|
2023-02-01 07:24:10 +00:00
|
|
|
rawId: encodeURLEncodedBase64(rawId),
|
2022-01-14 15:03:31 +00:00
|
|
|
type: newCredential.type,
|
|
|
|
response: {
|
2023-02-01 07:24:10 +00:00
|
|
|
attestationObject: encodeURLEncodedBase64(attestationObject),
|
|
|
|
clientDataJSON: encodeURLEncodedBase64(clientDataJSON),
|
2022-01-14 15:03:31 +00:00
|
|
|
},
|
2023-09-19 00:50:30 +00:00
|
|
|
},
|
2022-01-14 15:03:31 +00:00
|
|
|
});
|
2023-06-06 05:29:37 +00:00
|
|
|
|
|
|
|
if (res.status === 409) {
|
|
|
|
webAuthnError('duplicated');
|
|
|
|
return;
|
|
|
|
} else if (res.status !== 201) {
|
|
|
|
webAuthnError('unknown');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
window.location.reload();
|
2022-01-14 15:03:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function webAuthnError(errorType, message) {
|
2024-06-10 20:49:33 +00:00
|
|
|
const elErrorMsg = document.querySelector(`#webauthn-error-msg`);
|
2023-06-06 05:29:37 +00:00
|
|
|
|
2022-01-15 16:52:56 +00:00
|
|
|
if (errorType === 'general') {
|
2023-06-06 05:29:37 +00:00
|
|
|
elErrorMsg.textContent = message || 'unknown error';
|
2022-01-14 15:03:31 +00:00
|
|
|
} else {
|
2023-06-06 05:29:37 +00:00
|
|
|
const elTypedError = document.querySelector(`#webauthn-error [data-webauthn-error-msg=${errorType}]`);
|
|
|
|
if (elTypedError) {
|
|
|
|
elErrorMsg.textContent = `${elTypedError.textContent}${message ? ` ${message}` : ''}`;
|
2022-01-15 16:52:56 +00:00
|
|
|
} else {
|
2023-06-06 05:29:37 +00:00
|
|
|
elErrorMsg.textContent = `unknown error type: ${errorType}${message ? ` ${message}` : ''}`;
|
2022-01-15 16:52:56 +00:00
|
|
|
}
|
2022-01-14 15:03:31 +00:00
|
|
|
}
|
2023-06-06 05:29:37 +00:00
|
|
|
|
|
|
|
showElem('#webauthn-error');
|
2022-01-14 15:03:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function detectWebAuthnSupport() {
|
|
|
|
if (!window.isSecureContext) {
|
|
|
|
webAuthnError('insecure');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof window.PublicKeyCredential !== 'function') {
|
|
|
|
webAuthnError('browser');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function initUserAuthWebAuthnRegister() {
|
2024-06-10 20:49:33 +00:00
|
|
|
const elRegister = document.querySelector('#register-webauthn');
|
2023-06-06 05:29:37 +00:00
|
|
|
if (!elRegister) {
|
2022-01-14 15:03:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-06-07 11:20:18 +00:00
|
|
|
if (!detectWebAuthnSupport()) {
|
|
|
|
elRegister.disabled = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
elRegister.addEventListener('click', async (e) => {
|
2022-01-14 15:03:31 +00:00
|
|
|
e.preventDefault();
|
2023-06-07 11:20:18 +00:00
|
|
|
await webAuthnRegisterRequest();
|
2022-01-14 15:03:31 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-06-06 05:29:37 +00:00
|
|
|
async function webAuthnRegisterRequest() {
|
2024-06-10 20:49:33 +00:00
|
|
|
const elNickname = document.querySelector('#nickname');
|
2023-06-06 05:29:37 +00:00
|
|
|
|
2023-09-19 00:50:30 +00:00
|
|
|
const formData = new FormData();
|
|
|
|
formData.append('name', elNickname.value);
|
2023-06-06 05:29:37 +00:00
|
|
|
|
2023-09-19 00:50:30 +00:00
|
|
|
const res = await POST(`${appSubUrl}/user/settings/security/webauthn/request_register`, {
|
|
|
|
data: formData,
|
2023-06-06 05:29:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (res.status === 409) {
|
|
|
|
webAuthnError('duplicated');
|
|
|
|
return;
|
2024-06-29 22:50:03 +00:00
|
|
|
} else if (!res.ok) {
|
2023-06-06 05:29:37 +00:00
|
|
|
webAuthnError('unknown');
|
2022-01-14 15:03:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-06 05:29:37 +00:00
|
|
|
const options = await res.json();
|
|
|
|
elNickname.closest('div.field').classList.remove('error');
|
|
|
|
|
|
|
|
options.publicKey.challenge = decodeURLEncodedBase64(options.publicKey.challenge);
|
|
|
|
options.publicKey.user.id = decodeURLEncodedBase64(options.publicKey.user.id);
|
|
|
|
if (options.publicKey.excludeCredentials) {
|
|
|
|
for (const cred of options.publicKey.excludeCredentials) {
|
|
|
|
cred.id = decodeURLEncodedBase64(cred.id);
|
2022-01-14 15:03:31 +00:00
|
|
|
}
|
2023-06-06 05:29:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-06-07 11:20:18 +00:00
|
|
|
const credential = await navigator.credentials.create({
|
2024-03-22 14:06:53 +00:00
|
|
|
publicKey: options.publicKey,
|
2023-06-06 05:29:37 +00:00
|
|
|
});
|
2023-06-07 11:20:18 +00:00
|
|
|
await webauthnRegistered(credential);
|
2023-06-06 05:29:37 +00:00
|
|
|
} catch (err) {
|
|
|
|
webAuthnError('unknown', err);
|
|
|
|
}
|
2022-01-14 15:03:31 +00:00
|
|
|
}
|