Files
twofauth-rs/public/build/assets/webauthnService-D363et0U.js
T
wt cf25e28c28
Rust verification / test (push) Successful in 4m10s
initial commit
2026-10-28 15:59:54 +07:00

2 lines
4.7 KiB
JavaScript

import{ae as g}from"./app-dvJP63FL.js";class h{controller;createNewAbortSignal(){if(this.controller){const e=new Error("Cancelling existing WebAuthn API call for new one");e.name="AbortError",this.controller.abort(e)}const r=new AbortController;return this.controller=r,r.signal}}const c=new h;function d(n){return n==="localhost"||/^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$/i.test(n)}function w(n,r){const{publicKey:e}=r;if(n.name==="AbortError"){if(r.signal instanceof AbortSignal)return{phrase:"error.aborted_by_user",type:"is-warning"}}else if(n.name==="ConstraintError"){if(e.authenticatorSelection?.requireResidentKey===!0)return{phrase:"error.authenticator_missing_discoverable_credential_support",type:"is-danger"};if(e.authenticatorSelection?.userVerification==="required")return{phrase:"error.authenticator_missing_user_verification_support",type:"is-danger"}}else{if(n.name==="InvalidStateError")return{phrase:"error.security_device_already_registered",type:"is-danger"};if(n.name==="NotAllowedError")return{phrase:"error.not_allowed_operation",type:"is-danger"};if(n.name==="NotSupportedError")return e.pubKeyCredParams.filter(o=>o.type==="public-key").length===0?{phrase:"error.no_entry_was_of_type_public_key",type:"is-danger"}:{phrase:"error.no_authenticator_support_specified_algorithms",type:"is-danger"};if(n.name==="SecurityError"){const t=window.location.hostname;if(d(t)){if(e.rp.id!==t)return{phrase:"error.security_error_check_rpid",type:"is-danger"}}else return{phrase:"error.2fauth_has_not_a_valid_domain",type:"is-danger"}}else if(n.name==="TypeError"){if(e.user.id.byteLength<1||e.user.id.byteLength>64)return{phrase:"error.user_id_not_between_1_64",type:"is-danger"}}else if(n.name==="UnknownError")return{phrase:"error.unknown_error",type:"is-danger"}}return{phrase:"error.unknown_error",type:"is-danger"}}function f(n,r){const{publicKey:e}=r;if(n.name==="AbortError"){if(r.signal instanceof AbortSignal)return{phrase:"error.aborted_by_user",type:"is-warning"}}else{if(n.name==="NotAllowedError")return{phrase:"error.not_allowed_operation",type:"is-danger"};if(n.name==="SecurityError"){const t=window.location.hostname;if(d(t)){if(e.rpId!==t)return{phrase:"error.security_error_check_rpid",type:"is-danger"}}else return{phrase:"error.2fauth_has_not_a_valid_domain",type:"is-danger"}}else if(n.name==="UnknownError")return{phrase:"error.unknown_error",type:"is-danger"}}return{phrase:"error.unknown_error",type:"is-danger"}}const p=g("web");class a{async register(){let r={webauthn:!0,type:"is-danger",message:""};if(!window.isSecureContext)return r.message="error.https_required",Promise.reject(r);if(!a.supportsWebAuthn)return r.message="error.browser_does_not_support_webauthn",Promise.reject(r);const e=await p.post("/webauthn/register/options").then(i=>i.data);let o={publicKey:a.parseIncomingServerOptions(e)};o.signal=c.createNewAbortSignal();let u;try{u=await navigator.credentials.create(o)}catch(i){const s=w(i,o);return Promise.reject({webauthn:!0,type:s.type,message:s.phrase})}const l=a.parseOutgoingCredentials(u);return p.post("/webauthn/register",l,{returnError:!0})}async authenticate(r){if(!window.isSecureContext)return err.message="error.https_required",Promise.reject(err);if(!a.supportsWebAuthn)return err.message="error.browser_does_not_support_webauthn",Promise.reject(err);const e=await p.post("/webauthn/login/options",{email:r}).then(i=>i.data);let o={publicKey:a.parseIncomingServerOptions(e)};o.signal=c.createNewAbortSignal();const u=await navigator.credentials.get(o).catch(i=>{const s=f(i,o);return Promise.reject({webauthn:!0,type:s.type,message:s.phrase})});let l=a.parseOutgoingCredentials(u);return l.email=r,p.post("/webauthn/login",l,{returnError:!0})}static parseIncomingServerOptions(r){return r.challenge=a.uint8Array(r.challenge),"user"in r&&(r.user={...r.user,id:a.uint8Array(r.user.id)}),["excludeCredentials","allowCredentials"].filter(e=>e in r).forEach(e=>{r[e]=r[e].map(t=>({...t,id:a.uint8Array(t.id)}))}),r}static parseOutgoingCredentials(r){let e={id:r.id,type:r.type,rawId:a.arrayToBase64String(r.rawId),response:{}};return["clientDataJSON","attestationObject","authenticatorData","signature","userHandle"].filter(t=>t in r.response).forEach(t=>e.response[t]=a.arrayToBase64String(r.response[t])),e}static uint8Array(r,e=!1){return Uint8Array.from(e?atob(r):a.base64UrlDecode(r),t=>t.charCodeAt(0))}static arrayToBase64String(r){return btoa(String.fromCharCode(...new Uint8Array(r)))}static base64UrlDecode(r){r=r.replace(/-/g,"+").replace(/_/g,"/");const e=r.length%4;if(e){if(e===1)throw new Error("InvalidLengthError: Input base64url string is the wrong length to determine padding");r+=new Array(5-e).join("=")}return atob(r)}static supportsWebAuthn(){return window?.PublicKeyCredential!==void 0&&typeof window.PublicKeyCredential=="function"}}const _=new a;export{_ as w};