UNPKG

otplib

Version:

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

2 lines • 5.32 kB
"use strict";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})})}var h=class{strategy;crypto;base32;guardrails;constructor(t={}){let{strategy:n=`totp`,crypto:r=a.crypto,base32:o=i.base32,guardrails:s}=t;this.strategy=n,this.crypto=r,this.base32=o,this.guardrails=(0,e.createGuardrails)(s)}getStrategy(){return this.strategy}generateSecret(t=20){return(0,e.generateSecret)({crypto:this.crypto,base32:this.base32,length:t})}async generate(e){return d({...e,strategy:this.strategy,crypto:this.crypto,base32:this.base32,guardrails:e.guardrails??this.guardrails})}generateSync(e){return f({...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 m({...e,strategy:this.strategy,crypto:this.crypto,base32:this.base32,guardrails:e.guardrails??this.guardrails})}generateURI(e){return u({...e,strategy:this.strategy})}};Object.defineProperty(exports,"AlgorithmError",{enumerable:!0,get:function(){return e.AlgorithmError}}),Object.defineProperty(exports,"AlgorithmUnsupportedError",{enumerable:!0,get:function(){return e.AlgorithmUnsupportedError}}),Object.defineProperty(exports,"HMACError",{enumerable:!0,get:function(){return e.HMACError}}),Object.defineProperty(exports,"HOTP",{enumerable:!0,get:function(){return t.HOTP}}),Object.defineProperty(exports,"NobleCryptoPlugin",{enumerable:!0,get:function(){return a.NobleCryptoPlugin}}),exports.OTP=h,Object.defineProperty(exports,"ScureBase32Plugin",{enumerable:!0,get:function(){return i.ScureBase32Plugin}}),Object.defineProperty(exports,"TOTP",{enumerable:!0,get:function(){return n.TOTP}}),Object.defineProperty(exports,"createGuardrails",{enumerable:!0,get:function(){return e.createGuardrails}}),exports.generate=d,exports.generateSecret=l,exports.generateSync=f,exports.generateURI=u,Object.defineProperty(exports,"stringToBytes",{enumerable:!0,get:function(){return e.stringToBytes}}),exports.verify=p,exports.verifySync=m,Object.defineProperty(exports,"wrapResult",{enumerable:!0,get:function(){return e.wrapResult}}),Object.defineProperty(exports,"wrapResultAsync",{enumerable:!0,get:function(){return e.wrapResultAsync}}); //# sourceMappingURL=index.cjs.map