otplib
Version:
TypeScript-first library for TOTP and HOTP with multi-runtime and plugin support
2 lines • 3.24 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0});let e=require("@otplib/core"),t=require("@otplib/hotp"),n=require("@otplib/totp"),r=require("@otplib/uri"),i=require("@otplib/plugin-base32-scure"),a=require("@otplib/plugin-crypto-noble");function o(t){return{secret:t.secret,strategy:t.strategy??`totp`,crypto:t.crypto??a.crypto,base32:t.base32??i.base32,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,guardrails:t.guardrails??(0,e.createGuardrails)(),hooks:t.hooks}}function s(e){return{...o(e),token:e.token,epochTolerance:e.epochTolerance??0,counterTolerance:e.counterTolerance??0,afterTimeStep:e.afterTimeStep}}function c(t,n,r){if(t===`totp`)return r.totp();if(t===`hotp`){if(n===void 0)throw new e.ConfigurationError(`Counter is required for HOTP strategy. Example: { strategy: 'hotp', counter: 0 }`);return r.hotp(n)}throw new e.ConfigurationError(`Unknown OTP strategy: ${t}. Valid strategies are 'totp' or 'hotp'.`)}function l(t){let{crypto:n=a.crypto,base32:r=i.base32,length:o=20}=t||{};return(0,e.generateSecret)({crypto:n,base32:r,length:o})}function u(e){let{strategy:t=`totp`,issuer:n,label:i,secret:a,algorithm:o=`sha1`,digits:s=6,period:l=30,counter:u}=e;return c(t,u,{totp:()=>(0,r.generateTOTP)({issuer:n,label:i,secret:a,algorithm:o,digits:s,period:l}),hotp:e=>(0,r.generateHOTP)({issuer:n,label:i,secret:a,algorithm:o,digits:s,counter:e})})}async function d(e){let r=o(e),{secret:i,crypto:a,base32:s,algorithm:l,digits:u,hooks:d}=r,f={secret:i,crypto:a,base32:s,algorithm:l,digits:u,hooks:d};return c(r.strategy,r.counter,{totp:()=>(0,n.generate)({...f,period:r.period,epoch:r.epoch,t0:r.t0,guardrails:r.guardrails}),hotp:e=>(0,t.generate)({...f,counter:e,guardrails:r.guardrails})})}function f(e){let r=o(e),{secret:i,crypto:a,base32:s,algorithm:l,digits:u,hooks:d}=r,f={secret:i,crypto:a,base32:s,algorithm:l,digits:u,hooks:d};return c(r.strategy,r.counter,{totp:()=>(0,n.generateSync)({...f,period:r.period,epoch:r.epoch,t0:r.t0,guardrails:r.guardrails}),hotp:e=>(0,t.generateSync)({...f,counter:e,guardrails:r.guardrails})})}async function p(e){let r=s(e),{secret:i,token:a,crypto:o,base32:l,algorithm:u,digits:d,hooks:f}=r,p={secret:i,token:a,crypto:o,base32:l,algorithm:u,digits:d,hooks:f};return c(r.strategy,r.counter,{totp:()=>(0,n.verify)({...p,period:r.period,epoch:r.epoch,t0:r.t0,epochTolerance:r.epochTolerance,afterTimeStep:r.afterTimeStep,guardrails:r.guardrails}),hotp:e=>(0,t.verify)({...p,counter:e,counterTolerance:r.counterTolerance,guardrails:r.guardrails})})}function m(e){let r=s(e),{secret:i,token:a,crypto:o,base32:l,algorithm:u,digits:d,hooks:f}=r,p={secret:i,token:a,crypto:o,base32:l,algorithm:u,digits:d,hooks:f};return c(r.strategy,r.counter,{totp:()=>(0,n.verifySync)({...p,period:r.period,epoch:r.epoch,t0:r.t0,epochTolerance:r.epochTolerance,afterTimeStep:r.afterTimeStep,guardrails:r.guardrails}),hotp:e=>(0,t.verifySync)({...p,counter:e,counterTolerance:r.counterTolerance,guardrails:r.guardrails})})}exports.generate=d,exports.generateSecret=l,exports.generateSync=f,exports.generateURI=u,exports.verify=p,exports.verifySync=m;
//# sourceMappingURL=functional.cjs.map