otplib
Version:
TypeScript-first library for TOTP and HOTP with multi-runtime and plugin support
2 lines • 4.51 kB
JavaScript
;var x=Object.defineProperty;var A=Object.getOwnPropertyDescriptor;var k=Object.getOwnPropertyNames;var z=Object.prototype.hasOwnProperty;var W=(t,e)=>{for(var r in e)x(t,r,{get:e[r],enumerable:!0})},j=(t,e,r,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of k(e))!z.call(t,o)&&o!==r&&x(t,o,{get:()=>e[o],enumerable:!(n=A(e,o))||n.enumerable});return t};var E=t=>j(x({},"__esModule",{value:!0}),t);var F={};W(F,{HOTP:()=>D.HOTP,NobleCryptoPlugin:()=>w.NobleCryptoPlugin,OTP:()=>V,ScureBase32Plugin:()=>I.ScureBase32Plugin,TOTP:()=>U.TOTP,generate:()=>h,generateSecret:()=>C,generateSync:()=>b,generateURI:()=>P,verify:()=>d,verifySync:()=>S,wrapResult:()=>R.wrapResult,wrapResultAsync:()=>R.wrapResultAsync});module.exports=E(F);var f=require("@otplib/core"),y=require("@otplib/hotp"),c=require("@otplib/totp"),T=require("@otplib/uri");var G=require("@otplib/plugin-base32-scure"),v=require("@otplib/plugin-crypto-noble"),u=Object.freeze(new v.NobleCryptoPlugin),l=Object.freeze(new G.ScureBase32Plugin);function m(t){return{secret:t.secret,strategy:t.strategy??"totp",crypto:t.crypto??u,base32:t.base32??l,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 H(t){return{...m(t),token:t.token,epochTolerance:t.epochTolerance??0,counterTolerance:t.counterTolerance??0}}function O(t,e,r){if(t==="totp")return r.totp();if(t==="hotp"){if(e===void 0)throw new f.ConfigurationError("Counter is required for HOTP strategy. Example: { strategy: 'hotp', counter: 0 }");return r.hotp(e)}throw new f.ConfigurationError(`Unknown OTP strategy: ${t}. Valid strategies are 'totp' or 'hotp'.`)}function C(t){let{crypto:e=u,base32:r=l,length:n=20}=t||{};return(0,f.generateSecret)({crypto:e,base32:r,length:n})}function P(t){let{strategy:e="totp",issuer:r,label:n,secret:o,algorithm:a="sha1",digits:p=6,period:i=30,counter:s}=t;return O(e,s,{totp:()=>(0,T.generateTOTP)({issuer:r,label:n,secret:o,algorithm:a,digits:p,period:i}),hotp:g=>(0,T.generateHOTP)({issuer:r,label:n,secret:o,algorithm:a,digits:p,counter:g})})}async function h(t){let e=m(t),{secret:r,crypto:n,base32:o,algorithm:a,digits:p}=e,i={secret:r,crypto:n,base32:o,algorithm:a,digits:p};return O(e.strategy,e.counter,{totp:()=>(0,c.generate)({...i,period:e.period,epoch:e.epoch,t0:e.t0}),hotp:s=>(0,y.generate)({...i,counter:s})})}function b(t){let e=m(t),{secret:r,crypto:n,base32:o,algorithm:a,digits:p}=e,i={secret:r,crypto:n,base32:o,algorithm:a,digits:p};return O(e.strategy,e.counter,{totp:()=>(0,c.generateSync)({...i,period:e.period,epoch:e.epoch,t0:e.t0}),hotp:s=>(0,y.generateSync)({...i,counter:s})})}async function d(t){let e=H(t),{secret:r,token:n,crypto:o,base32:a,algorithm:p,digits:i}=e,s={secret:r,token:n,crypto:o,base32:a,algorithm:p,digits:i};return O(e.strategy,e.counter,{totp:()=>(0,c.verify)({...s,period:e.period,epoch:e.epoch,t0:e.t0,epochTolerance:e.epochTolerance}),hotp:g=>(0,y.verify)({...s,counter:g,counterTolerance:e.counterTolerance})})}function S(t){let e=H(t),{secret:r,token:n,crypto:o,base32:a,algorithm:p,digits:i}=e,s={secret:r,token:n,crypto:o,base32:a,algorithm:p,digits:i};return O(e.strategy,e.counter,{totp:()=>(0,c.verifySync)({...s,period:e.period,epoch:e.epoch,t0:e.t0,epochTolerance:e.epochTolerance}),hotp:g=>(0,y.verifySync)({...s,counter:g,counterTolerance:e.counterTolerance})})}var B=require("@otplib/core");var V=class{strategy;crypto;base32;constructor(e={}){let{strategy:r="totp",crypto:n=u,base32:o=l}=e;this.strategy=r,this.crypto=n,this.base32=o}getStrategy(){return this.strategy}generateSecret(e=20){return(0,B.generateSecret)({crypto:this.crypto,base32:this.base32,length:e})}async generate(e){return h({...e,strategy:this.strategy,crypto:this.crypto,base32:this.base32})}generateSync(e){return b({...e,strategy:this.strategy,crypto:this.crypto,base32:this.base32})}async verify(e){return d({...e,strategy:this.strategy,crypto:this.crypto,base32:this.base32})}verifySync(e){return S({...e,strategy:this.strategy,crypto:this.crypto,base32:this.base32})}generateURI(e){return P({...e,strategy:this.strategy})}};var D=require("@otplib/hotp"),U=require("@otplib/totp"),R=require("@otplib/core"),w=require("@otplib/plugin-crypto-noble"),I=require("@otplib/plugin-base32-scure");0&&(module.exports={HOTP,NobleCryptoPlugin,OTP,ScureBase32Plugin,TOTP,generate,generateSecret,generateSync,generateURI,verify,verifySync,wrapResult,wrapResultAsync});
//# sourceMappingURL=index.cjs.map