UNPKG

otplib

Version:

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

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