UNPKG

otplib

Version:

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

2 lines • 3.85 kB
import{ConfigurationError as e,createGuardrails as t,generateSecret as n}from"@otplib/core";import{base32 as r}from"@otplib/plugin-base32-scure";import{crypto as i}from"@otplib/plugin-crypto-noble";import{generate as a,generateSync as o,verify as s,verifySync as c}from"@otplib/hotp";import{generate as l,generateSync as u,verify as d,verifySync as f}from"@otplib/totp";import{generateHOTP as p,generateTOTP as m}from"@otplib/uri";function h(e){return{secret:e.secret,strategy:e.strategy??`totp`,crypto:e.crypto??i,base32:e.base32??r,algorithm:e.algorithm??`sha1`,digits:e.digits??6,period:e.period??30,epoch:e.epoch??Math.floor(Date.now()/1e3),t0:e.t0??0,counter:e.counter,guardrails:e.guardrails??t(),hooks:e.hooks}}function g(e){return{...h(e),token:e.token,epochTolerance:e.epochTolerance??0,counterTolerance:e.counterTolerance??0,afterTimeStep:e.afterTimeStep}}function _(t,n,r){if(t===`totp`)return r.totp();if(t===`hotp`){if(n===void 0)throw new e(`Counter is required for HOTP strategy. Example: { strategy: 'hotp', counter: 0 }`);return r.hotp(n)}throw new e(`Unknown OTP strategy: ${t}. Valid strategies are 'totp' or 'hotp'.`)}function v(e){let{strategy:t=`totp`,issuer:n,label:r,secret:i,algorithm:a=`sha1`,digits:o=6,period:s=30,counter:c}=e;return _(t,c,{totp:()=>m({issuer:n,label:r,secret:i,algorithm:a,digits:o,period:s}),hotp:e=>p({issuer:n,label:r,secret:i,algorithm:a,digits:o,counter:e})})}async function y(e){let t=h(e),{secret:n,crypto:r,base32:i,algorithm:o,digits:s,hooks:c}=t,u={secret:n,crypto:r,base32:i,algorithm:o,digits:s,hooks:c};return _(t.strategy,t.counter,{totp:()=>l({...u,period:t.period,epoch:t.epoch,t0:t.t0,guardrails:t.guardrails}),hotp:e=>a({...u,counter:e,guardrails:t.guardrails})})}function b(e){let t=h(e),{secret:n,crypto:r,base32:i,algorithm:a,digits:s,hooks:c}=t,l={secret:n,crypto:r,base32:i,algorithm:a,digits:s,hooks:c};return _(t.strategy,t.counter,{totp:()=>u({...l,period:t.period,epoch:t.epoch,t0:t.t0,guardrails:t.guardrails}),hotp:e=>o({...l,counter:e,guardrails:t.guardrails})})}async function x(e){let t=g(e),{secret:n,token:r,crypto:i,base32:a,algorithm:o,digits:c,hooks:l}=t,u={secret:n,token:r,crypto:i,base32:a,algorithm:o,digits:c,hooks:l};return _(t.strategy,t.counter,{totp:()=>d({...u,period:t.period,epoch:t.epoch,t0:t.t0,epochTolerance:t.epochTolerance,afterTimeStep:t.afterTimeStep,guardrails:t.guardrails}),hotp:e=>s({...u,counter:e,counterTolerance:t.counterTolerance,guardrails:t.guardrails})})}function S(e){let t=g(e),{secret:n,token:r,crypto:i,base32:a,algorithm:o,digits:s,hooks:l}=t,u={secret:n,token:r,crypto:i,base32:a,algorithm:o,digits:s,hooks:l};return _(t.strategy,t.counter,{totp:()=>f({...u,period:t.period,epoch:t.epoch,t0:t.t0,epochTolerance:t.epochTolerance,afterTimeStep:t.afterTimeStep,guardrails:t.guardrails}),hotp:e=>c({...u,counter:e,counterTolerance:t.counterTolerance,guardrails:t.guardrails})})}var C=class{strategy;crypto;base32;guardrails;constructor(e={}){let{strategy:n=`totp`,crypto:a=i,base32:o=r,guardrails:s}=e;this.strategy=n,this.crypto=a,this.base32=o,this.guardrails=t(s)}getStrategy(){return this.strategy}generateSecret(e=20){return n({crypto:this.crypto,base32:this.base32,length:e})}async generate(e){return y({...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 x({...e,strategy:this.strategy,crypto:this.crypto,base32:this.base32,guardrails:e.guardrails??this.guardrails})}verifySync(e){return S({...e,strategy:this.strategy,crypto:this.crypto,base32:this.base32,guardrails:e.guardrails??this.guardrails})}generateURI(e){return v({...e,strategy:this.strategy})}};export{C as OTP}; //# sourceMappingURL=class.js.map