UNPKG

otplib

Version:

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

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