mirror of
https://github.com/we-promise/sure.git
synced 2026-05-08 13:14:58 +00:00
* feat(auth): add WebAuthn MFA credentials * fix(auth): harden WebAuthn MFA review paths * fix(auth): polish WebAuthn error handling * fix(auth): handle duplicate WebAuthn credential races * fix(auth): permit WebAuthn credential params * fix(auth): trim WebAuthn registration controller cleanup * fix(auth): tighten WebAuthn MFA handling * fix(auth): pin WebAuthn relying party config
63 lines
1.6 KiB
JavaScript
63 lines
1.6 KiB
JavaScript
import WebauthnController from "controllers/webauthn_controller";
|
|
import {
|
|
prepareCredentialRequestOptions,
|
|
serializePublicKeyCredential,
|
|
} from "utils/webauthn";
|
|
|
|
export default class extends WebauthnController {
|
|
static targets = ["error"];
|
|
static values = {
|
|
optionsUrl: String,
|
|
verifyUrl: String,
|
|
unsupportedMessage: String,
|
|
errorFallback: String,
|
|
};
|
|
|
|
async authenticate(event) {
|
|
event.preventDefault();
|
|
this.clearError();
|
|
|
|
if (!window.PublicKeyCredential) {
|
|
this.showError(this.unsupportedMessageValue);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const options = await this.fetchOptions();
|
|
const credential = await navigator.credentials.get({
|
|
publicKey: prepareCredentialRequestOptions(options),
|
|
});
|
|
|
|
await this.verifyCredential(serializePublicKeyCredential(credential));
|
|
} catch (error) {
|
|
this.showError(error.message);
|
|
}
|
|
}
|
|
|
|
async fetchOptions() {
|
|
const response = await fetch(this.optionsUrlValue, {
|
|
method: "POST",
|
|
headers: this.headers,
|
|
credentials: "same-origin",
|
|
});
|
|
|
|
if (!response.ok) throw new Error(await this.errorMessage(response));
|
|
|
|
return response.json();
|
|
}
|
|
|
|
async verifyCredential(credential) {
|
|
const response = await fetch(this.verifyUrlValue, {
|
|
method: "POST",
|
|
headers: this.headers,
|
|
credentials: "same-origin",
|
|
body: JSON.stringify({ credential }),
|
|
});
|
|
|
|
if (!response.ok) throw new Error(await this.errorMessage(response));
|
|
|
|
const result = await response.json();
|
|
window.location.href = result.redirect_url;
|
|
}
|
|
}
|