UNPKG

otplib

Version:

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

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