@nopwdio/sdk-js
Version:
Nopwd JS SDK
2 lines (1 loc) • 2.31 kB
JavaScript
import{b as m}from"./chunk-XGY7T5A3.js";import{a as i,b as d}from"./chunk-VS4C5FIV.js";import{a as r,b as u,d as w,f as h,g,h as l,k as o,l as p,m as y,q as f,r as b,s}from"./chunk-EX74VC7J.js";var S=async function(n){let t=new TextEncoder().encode(n),e=await window.crypto.subtle.digest("SHA-256",t);return new Uint8Array(e)};var T=async function(n,t){try{if(!await x())throw new o;let e=m(n);if(e.amr.includes("webauthn"))throw new Error("A passkey already exists for this website");if(e.exp<Date.now()/1e3)throw new f;let a=await S(e.sub),c=await navigator.credentials.create({signal:t,publicKey:{challenge:d(e.jti),rp:{name:e.aud},user:{id:a,name:e.sub,displayName:e.sub},pubKeyCredParams:[{alg:-7,type:"public-key"}],authenticatorSelection:{userVerification:"required"}}});if(c===null)throw new r;let{alg:A}=await l({method:"POST",ressource:"/webauthn/passkeys",data:{client_data:i(c.response.clientDataJSON),attestation_object:i(c.response.attestationObject),access_token:n},signal:t});return{id:c.id,alg:A}}catch(e){throw e instanceof r||e instanceof u||e instanceof f||e instanceof o?e:new s(e)}},B=async n=>{let{challenge:t}=await C(n),e=await E(t,n);return await D(e,n)},C=async n=>{try{let{challenge:t,expires_at:e}=await l({method:"GET",ressource:"/webauthn/challenge",signal:n});return{challenge:t,expiresAt:e}}catch(t){throw t instanceof r||t instanceof u?t:t instanceof g?new b(t.getRetryAt()):new s(t)}},E=async function(n,t){try{if(!await x())throw new o;var e={signal:t,publicKey:{userVerification:"required",challenge:d(n)},mediation:"conditional"};let a=await navigator.credentials.get(e);if(a===null)throw new r;return{id:a.id,signature:i(a.response.signature),authenticatorData:i(a.response.authenticatorData),clientData:i(a.response.clientDataJSON)}}catch(a){throw a instanceof r||a instanceof o?a:new s(a)}},D=async(n,t)=>{try{let{access_token:e}=await l({method:"POST",ressource:"/webauthn/tokens",data:{id:n.id,signature:n.signature,authenticator_data:n.authenticatorData,client_data:n.clientData},signal:t});return e}catch(e){throw e instanceof r||e instanceof u?e:e instanceof h?new p:e instanceof w?new y:new s(e)}},x=async()=>window.PublicKeyCredential!==void 0&&window.PublicKeyCredential.isConditionalMediationAvailable?.();export{T as a,B as b,C as c,E as d,D as e,x as f};