otplib
Version:
TypeScript-first library for TOTP and HOTP with multi-runtime and plugin support
2 lines • 3.29 kB
JavaScript
import{generateSecret as w}from"@otplib/core";import{generateTOTP as B}from"@otplib/uri";import{ScureBase32Plugin as d}from"@otplib/plugin-base32-scure";import{NobleCryptoPlugin as S}from"@otplib/plugin-crypto-noble";var c=Object.freeze(new S),y=Object.freeze(new d);function g(t){return{secret:t.secret,strategy:t.strategy??"totp",crypto:t.crypto??c,base32:t.base32??y,algorithm:t.algorithm??"sha1",digits:t.digits??6,period:t.period??30,epoch:t.epoch??Math.floor(Date.now()/1e3),t0:t.t0??0,counter:t.counter}}function f(t){return{...g(t),token:t.token,epochTolerance:t.epochTolerance??0,counterTolerance:t.counterTolerance??0}}import{generateSecret as A,ConfigurationError as m}from"@otplib/core";import{generate as V,generateSync as R,verify as x,verifySync as v}from"@otplib/hotp";import{generate as H,generateSync as G,verify as C,verifySync as D}from"@otplib/totp";import{generateTOTP as q}from"@otplib/uri";function u(t,e,r){if(t==="totp")return r.totp();if(t==="hotp"){if(e===void 0)throw new m("Counter is required for HOTP strategy. Example: { strategy: 'hotp', counter: 0 }");return r.hotp(e)}throw new m(`Unknown OTP strategy: ${t}. Valid strategies are 'totp' or 'hotp'.`)}async function O(t){let e=g(t),{secret:r,crypto:o,base32:n,algorithm:s,digits:a}=e,i={secret:r,crypto:o,base32:n,algorithm:s,digits:a};return u(e.strategy,e.counter,{totp:()=>H({...i,period:e.period,epoch:e.epoch,t0:e.t0}),hotp:p=>V({...i,counter:p})})}function h(t){let e=g(t),{secret:r,crypto:o,base32:n,algorithm:s,digits:a}=e,i={secret:r,crypto:o,base32:n,algorithm:s,digits:a};return u(e.strategy,e.counter,{totp:()=>G({...i,period:e.period,epoch:e.epoch,t0:e.t0}),hotp:p=>R({...i,counter:p})})}async function T(t){let e=f(t),{secret:r,token:o,crypto:n,base32:s,algorithm:a,digits:i}=e,p={secret:r,token:o,crypto:n,base32:s,algorithm:a,digits:i};return u(e.strategy,e.counter,{totp:()=>C({...p,period:e.period,epoch:e.epoch,t0:e.t0,epochTolerance:e.epochTolerance}),hotp:l=>x({...p,counter:l,counterTolerance:e.counterTolerance})})}function P(t){let e=f(t),{secret:r,token:o,crypto:n,base32:s,algorithm:a,digits:i}=e,p={secret:r,token:o,crypto:n,base32:s,algorithm:a,digits:i};return u(e.strategy,e.counter,{totp:()=>D({...p,period:e.period,epoch:e.epoch,t0:e.t0,epochTolerance:e.epochTolerance}),hotp:l=>v({...p,counter:l,counterTolerance:e.counterTolerance})})}var b=class{strategy;crypto;base32;constructor(e={}){let{strategy:r="totp",crypto:o=c,base32:n=y}=e;this.strategy=r,this.crypto=o,this.base32=n}getStrategy(){return this.strategy}generateSecret(e=20){return w({crypto:this.crypto,base32:this.base32,length:e})}async generate(e){return O({...e,strategy:this.strategy,crypto:this.crypto,base32:this.base32})}generateSync(e){return h({...e,strategy:this.strategy,crypto:this.crypto,base32:this.base32})}async verify(e){return T({...e,strategy:this.strategy,crypto:this.crypto,base32:this.base32})}verifySync(e){return P({...e,strategy:this.strategy,crypto:this.crypto,base32:this.base32})}generateURI(e){if(this.strategy==="hotp")throw new Error("generateURI is not available for HOTP strategy");let{issuer:r,label:o,secret:n,algorithm:s="sha1",digits:a=6,period:i=30}=e;return B({issuer:r,label:o,secret:n,algorithm:s,digits:a,period:i})}};export{b as OTP};
//# sourceMappingURL=class.js.map