UNPKG

otplib

Version:

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

2 lines • 3.14 kB
import{ConfigurationError as e,createGuardrails as t,generateSecret as n}from"@otplib/core";import{generate as r,generateSync as i,verify as a,verifySync as o}from"@otplib/hotp";import{generate as s,generateSync as c,verify as l,verifySync as u}from"@otplib/totp";import{generateHOTP as d,generateTOTP as f}from"@otplib/uri";import{base32 as p}from"@otplib/plugin-base32-scure";import{crypto as m}from"@otplib/plugin-crypto-noble";function h(e){return{secret:e.secret,strategy:e.strategy??`totp`,crypto:e.crypto??m,base32:e.base32??p,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{crypto:t=m,base32:r=p,length:i=20}=e||{};return n({crypto:t,base32:r,length:i})}function y(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:()=>f({issuer:n,label:r,secret:i,algorithm:a,digits:o,period:s}),hotp:e=>d({issuer:n,label:r,secret:i,algorithm:a,digits:o,counter:e})})}async function b(e){let t=h(e),{secret:n,crypto:i,base32:a,algorithm:o,digits:c,hooks:l}=t,u={secret:n,crypto:i,base32:a,algorithm:o,digits:c,hooks:l};return _(t.strategy,t.counter,{totp:()=>s({...u,period:t.period,epoch:t.epoch,t0:t.t0,guardrails:t.guardrails}),hotp:e=>r({...u,counter:e,guardrails:t.guardrails})})}function x(e){let t=h(e),{secret:n,crypto:r,base32:a,algorithm:o,digits:s,hooks:l}=t,u={secret:n,crypto:r,base32:a,algorithm:o,digits:s,hooks:l};return _(t.strategy,t.counter,{totp:()=>c({...u,period:t.period,epoch:t.epoch,t0:t.t0,guardrails:t.guardrails}),hotp:e=>i({...u,counter:e,guardrails:t.guardrails})})}async function S(e){let t=g(e),{secret:n,token:r,crypto:i,base32:o,algorithm:s,digits:c,hooks:u}=t,d={secret:n,token:r,crypto:i,base32:o,algorithm:s,digits:c,hooks:u};return _(t.strategy,t.counter,{totp:()=>l({...d,period:t.period,epoch:t.epoch,t0:t.t0,epochTolerance:t.epochTolerance,afterTimeStep:t.afterTimeStep,guardrails:t.guardrails}),hotp:e=>a({...d,counter:e,counterTolerance:t.counterTolerance,guardrails:t.guardrails})})}function C(e){let t=g(e),{secret:n,token:r,crypto:i,base32:a,algorithm:s,digits:c,hooks:l}=t,d={secret:n,token:r,crypto:i,base32:a,algorithm:s,digits:c,hooks:l};return _(t.strategy,t.counter,{totp:()=>u({...d,period:t.period,epoch:t.epoch,t0:t.t0,epochTolerance:t.epochTolerance,afterTimeStep:t.afterTimeStep,guardrails:t.guardrails}),hotp:e=>o({...d,counter:e,counterTolerance:t.counterTolerance,guardrails:t.guardrails})})}export{b as generate,v as generateSecret,x as generateSync,y as generateURI,S as verify,C as verifySync}; //# sourceMappingURL=functional.js.map