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