UNPKG

otplib

Version:

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

2 lines • 3.94 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});let e=require("@otplib/core"),t=require("@otplib/plugin-base32-scure"),n=require("@otplib/plugin-crypto-noble"),r=require("@otplib/hotp"),i=require("@otplib/totp"),a=require("@otplib/uri");function o(r){return{secret:r.secret,strategy:r.strategy??`totp`,crypto:r.crypto??n.crypto,base32:r.base32??t.base32,algorithm:r.algorithm??`sha1`,digits:r.digits??6,period:r.period??30,epoch:r.epoch??Math.floor(Date.now()/1e3),t0:r.t0??0,counter:r.counter,guardrails:r.guardrails??(0,e.createGuardrails)(),hooks:r.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(e){let{strategy:t=`totp`,issuer:n,label:r,secret:i,algorithm:o=`sha1`,digits:s=6,period:l=30,counter:u}=e;return c(t,u,{totp:()=>(0,a.generateTOTP)({issuer:n,label:r,secret:i,algorithm:o,digits:s,period:l}),hotp:e=>(0,a.generateHOTP)({issuer:n,label:r,secret:i,algorithm:o,digits:s,counter:e})})}async function u(e){let t=o(e),{secret:n,crypto:a,base32:s,algorithm:l,digits:u,hooks:d}=t,f={secret:n,crypto:a,base32:s,algorithm:l,digits:u,hooks:d};return c(t.strategy,t.counter,{totp:()=>(0,i.generate)({...f,period:t.period,epoch:t.epoch,t0:t.t0,guardrails:t.guardrails}),hotp:e=>(0,r.generate)({...f,counter:e,guardrails:t.guardrails})})}function d(e){let t=o(e),{secret:n,crypto:a,base32:s,algorithm:l,digits:u,hooks:d}=t,f={secret:n,crypto:a,base32:s,algorithm:l,digits:u,hooks:d};return c(t.strategy,t.counter,{totp:()=>(0,i.generateSync)({...f,period:t.period,epoch:t.epoch,t0:t.t0,guardrails:t.guardrails}),hotp:e=>(0,r.generateSync)({...f,counter:e,guardrails:t.guardrails})})}async function f(e){let t=s(e),{secret:n,token:a,crypto:o,base32:l,algorithm:u,digits:d,hooks:f}=t,p={secret:n,token:a,crypto:o,base32:l,algorithm:u,digits:d,hooks:f};return c(t.strategy,t.counter,{totp:()=>(0,i.verify)({...p,period:t.period,epoch:t.epoch,t0:t.t0,epochTolerance:t.epochTolerance,afterTimeStep:t.afterTimeStep,guardrails:t.guardrails}),hotp:e=>(0,r.verify)({...p,counter:e,counterTolerance:t.counterTolerance,guardrails:t.guardrails})})}function p(e){let t=s(e),{secret:n,token:a,crypto:o,base32:l,algorithm:u,digits:d,hooks:f}=t,p={secret:n,token:a,crypto:o,base32:l,algorithm:u,digits:d,hooks:f};return c(t.strategy,t.counter,{totp:()=>(0,i.verifySync)({...p,period:t.period,epoch:t.epoch,t0:t.t0,epochTolerance:t.epochTolerance,afterTimeStep:t.afterTimeStep,guardrails:t.guardrails}),hotp:e=>(0,r.verifySync)({...p,counter:e,counterTolerance:t.counterTolerance,guardrails:t.guardrails})})}var m=class{strategy;crypto;base32;guardrails;constructor(r={}){let{strategy:i=`totp`,crypto:a=n.crypto,base32:o=t.base32,guardrails:s}=r;this.strategy=i,this.crypto=a,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 u({...e,strategy:this.strategy,crypto:this.crypto,base32:this.base32,guardrails:e.guardrails??this.guardrails})}generateSync(e){return d({...e,strategy:this.strategy,crypto:this.crypto,base32:this.base32,guardrails:e.guardrails??this.guardrails})}async verify(e){return f({...e,strategy:this.strategy,crypto:this.crypto,base32:this.base32,guardrails:e.guardrails??this.guardrails})}verifySync(e){return p({...e,strategy:this.strategy,crypto:this.crypto,base32:this.base32,guardrails:e.guardrails??this.guardrails})}generateURI(e){return l({...e,strategy:this.strategy})}};exports.OTP=m; //# sourceMappingURL=class.cjs.map