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