UNPKG

otplib

Version:

TypeScript-first library for TOTP and HOTP with multi-runtime and plugin support

2 lines • 4.46 kB
import{AlgorithmError as e,AlgorithmUnsupportedError as t,ConfigurationError as n,HMACError as r,createGuardrails as i,createGuardrails as a,generateSecret as o,stringToBytes as s,wrapResult as c,wrapResultAsync as l}from"@otplib/core";import{HOTP as u,generate as d,generateSync as f,verify as p,verifySync as m}from"@otplib/hotp";import{TOTP as h,generate as g,generateSync as _,verify as v,verifySync as y}from"@otplib/totp";import{generateHOTP as b,generateTOTP as x}from"@otplib/uri";import{ScureBase32Plugin as S,base32 as C}from"@otplib/plugin-base32-scure";import{NobleCryptoPlugin as w,crypto as T}from"@otplib/plugin-crypto-noble";function E(e){return{secret:e.secret,strategy:e.strategy??`totp`,crypto:e.crypto??T,base32:e.base32??C,algorithm:e.algorithm??`sha1`,digits:e.digits??6,period:e.period??30,epoch:e.epoch??Math.floor(Date.now()/1e3),t0:e.t0??0,counter:e.counter,guardrails:e.guardrails??a(),hooks:e.hooks}}function D(e){return{...E(e),token:e.token,epochTolerance:e.epochTolerance??0,counterTolerance:e.counterTolerance??0,afterTimeStep:e.afterTimeStep}}function O(e,t,r){if(e===`totp`)return r.totp();if(e===`hotp`){if(t===void 0)throw new n(`Counter is required for HOTP strategy. Example: { strategy: 'hotp', counter: 0 }`);return r.hotp(t)}throw new n(`Unknown OTP strategy: ${e}. Valid strategies are 'totp' or 'hotp'.`)}function k(e){let{crypto:t=T,base32:n=C,length:r=20}=e||{};return o({crypto:t,base32:n,length:r})}function A(e){let{strategy:t=`totp`,issuer:n,label:r,secret:i,algorithm:a=`sha1`,digits:o=6,period:s=30,counter:c}=e;return O(t,c,{totp:()=>x({issuer:n,label:r,secret:i,algorithm:a,digits:o,period:s}),hotp:e=>b({issuer:n,label:r,secret:i,algorithm:a,digits:o,counter:e})})}async function j(e){let t=E(e),{secret:n,crypto:r,base32:i,algorithm:a,digits:o,hooks:s}=t,c={secret:n,crypto:r,base32:i,algorithm:a,digits:o,hooks:s};return O(t.strategy,t.counter,{totp:()=>g({...c,period:t.period,epoch:t.epoch,t0:t.t0,guardrails:t.guardrails}),hotp:e=>d({...c,counter:e,guardrails:t.guardrails})})}function M(e){let t=E(e),{secret:n,crypto:r,base32:i,algorithm:a,digits:o,hooks:s}=t,c={secret:n,crypto:r,base32:i,algorithm:a,digits:o,hooks:s};return O(t.strategy,t.counter,{totp:()=>_({...c,period:t.period,epoch:t.epoch,t0:t.t0,guardrails:t.guardrails}),hotp:e=>f({...c,counter:e,guardrails:t.guardrails})})}async function N(e){let t=D(e),{secret:n,token:r,crypto:i,base32:a,algorithm:o,digits:s,hooks:c}=t,l={secret:n,token:r,crypto:i,base32:a,algorithm:o,digits:s,hooks:c};return O(t.strategy,t.counter,{totp:()=>v({...l,period:t.period,epoch:t.epoch,t0:t.t0,epochTolerance:t.epochTolerance,afterTimeStep:t.afterTimeStep,guardrails:t.guardrails}),hotp:e=>p({...l,counter:e,counterTolerance:t.counterTolerance,guardrails:t.guardrails})})}function P(e){let t=D(e),{secret:n,token:r,crypto:i,base32:a,algorithm:o,digits:s,hooks:c}=t,l={secret:n,token:r,crypto:i,base32:a,algorithm:o,digits:s,hooks:c};return O(t.strategy,t.counter,{totp:()=>y({...l,period:t.period,epoch:t.epoch,t0:t.t0,epochTolerance:t.epochTolerance,afterTimeStep:t.afterTimeStep,guardrails:t.guardrails}),hotp:e=>m({...l,counter:e,counterTolerance:t.counterTolerance,guardrails:t.guardrails})})}var F=class{strategy;crypto;base32;guardrails;constructor(e={}){let{strategy:t=`totp`,crypto:n=T,base32:r=C,guardrails:i}=e;this.strategy=t,this.crypto=n,this.base32=r,this.guardrails=a(i)}getStrategy(){return this.strategy}generateSecret(e=20){return o({crypto:this.crypto,base32:this.base32,length:e})}async generate(e){return j({...e,strategy:this.strategy,crypto:this.crypto,base32:this.base32,guardrails:e.guardrails??this.guardrails})}generateSync(e){return M({...e,strategy:this.strategy,crypto:this.crypto,base32:this.base32,guardrails:e.guardrails??this.guardrails})}async verify(e){return N({...e,strategy:this.strategy,crypto:this.crypto,base32:this.base32,guardrails:e.guardrails??this.guardrails})}verifySync(e){return P({...e,strategy:this.strategy,crypto:this.crypto,base32:this.base32,guardrails:e.guardrails??this.guardrails})}generateURI(e){return A({...e,strategy:this.strategy})}};export{e as AlgorithmError,t as AlgorithmUnsupportedError,r as HMACError,u as HOTP,w as NobleCryptoPlugin,F as OTP,S as ScureBase32Plugin,h as TOTP,i as createGuardrails,j as generate,k as generateSecret,M as generateSync,A as generateURI,s as stringToBytes,N as verify,P as verifySync,c as wrapResult,l as wrapResultAsync}; //# sourceMappingURL=index.js.map