UNPKG

paseto-browser

Version:

In-browser JavaScript implementation of PASETO

2 lines (1 loc) 4.46 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("tweetnacl"),require("crypto")):"function"==typeof define&&define.amd?define(["exports","tweetnacl","crypto"],t):t(((e="undefined"!=typeof globalThis?globalThis:e||self).paseto=e.paseto||{},e.paseto.v4=e.paseto.v4||{},e.paseto.v4.public={}),e.nacl,e.crypto)}(this,(function(e,t,n){"use strict";function r(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:function(){return e[n]}})}})),t.default=e,Object.freeze(t)}var o=r(t);for(let e=0;e<=255;++e)e.toString(16).padStart(2,"0");const i="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",s=new Uint8Array(256);for(let e=0;e<i.length;e++)s[i.charCodeAt(e)]=e;function c(e,t=!1){let n,r,o,i,c,a=.75*e.length,u=e.length,f=0;const l=new Uint8Array(a);for(n=0;n<u;n+=4)r=s[e.charCodeAt(n)],o=s[e.charCodeAt(n+1)],i=s[e.charCodeAt(n+2)],c=s[e.charCodeAt(n+3)],l[f++]=r<<2|o>>4,l[f++]=(15&o)<<4|i>>2,l[f++]=(3&i)<<6|63&c;return t?l:(new TextDecoder).decode(l)}function a(e){let t,n=e.length,r="";for(t=0;t<n;t+=3)r+=i[e[t]>>2],r+=i[(3&e[t])<<4|e[t+1]>>4],r+=i[(15&e[t+1])<<2|e[t+2]>>6],r+=i[63&e[t+2]];return n%3==2?r.substring(0,r.length-1):n%3==1?r.substring(0,r.length-2):r}function u(e){f(Number.isSafeInteger(e),"Number too large for JavaScript to safely process");const t=e/4294967296|0,n=4294967295&e,r=new Uint8Array(8);return r[0]=255&n,r[1]=n>>>8&255,r[2]=n>>>16&255,r[3]=n>>>24&255,r[4]=255&t,r[5]=t>>>8&255,r[6]=t>>>16&255,r[7]=t>>>24&255,r}function f(e,t="An unknown error occurred"){if(!e)throw new Error(t)}function l(...e){let t=u(e.length);for(let n of e){let e=d(n);f(e instanceof Uint8Array,"Only string and Uint8Array is allowed");let r=u(e.length);t=new Uint8Array([...t,...r,...e])}return t}function d(e,t=!1){if(e instanceof Uint8Array)return e;if("string"==typeof e)return(new TextEncoder).encode(e);if(e instanceof Number&&t)return u(e);throw new Error("Unsupported type: "+typeof e)}function y(e,t){if(void 0===n.timingSafeEqual){if(f(e instanceof Uint8Array,"Must be Uint8Array"),f(t instanceof Uint8Array,"Must be Uint8Array"),e.length!==t.length)return!1;let n=0;for(let r=0;r<e.length;r++)n|=e[r]^t[r];return 0===n}return n.timingSafeEqual(d(e),d(t))}const g="v4.public.",p=(new TextEncoder).encode(g);class h{constructor(e,t=null){f(e instanceof Uint8Array,"Input must be a Uint8Array"),f(32===e.length,"Public Key must be 32 bytes"),this.pk=e,t?(f(t instanceof Uint8Array,"Input must be a Uint8Array"),f(64===t.length,"Secret Key must be 64 bytes"),this.sk=t):this.sk=null}getSecretKey(){return this.sk}getPublicKey(){return this.pk}static generate(){const e=o.default.sign.keyPair();return new h(e.publicKey,e.secretKey)}async assertFooter(e,t){const n=e.split(".");f(4===n.length,"No footer provided");return y(c(n[3],t instanceof Uint8Array),t)}static getFooter(e,t=!1){const n=e.split(".");f(4===n.length,"No footer provided");const r=c(n[3],t);return t?JSON.parse(function(e){if("string"==typeof e)return e;if(e instanceof Uint8Array)return(new TextDecoder).decode(e);throw new Error("Unsupported type: "+typeof e)}(r)):r}async decode(e,t=""){return JSON.parse(await this.verify(e,t))}async encode(e,t="",n=""){return f(this.sk,"Cannot sign: No secret key was provided"),"object"==typeof t&&(t=JSON.stringify(t)),"string"==typeof t&&(t=d(t)),this.sign(JSON.stringify(e),t,n)}async sign(e,t="",n=""){f(this.sk,"Cannot sign: No secret key was provided");const r=d(e),i=l(p,r,d(t),d(n)),s=a(function(...e){let t=0;for(const n of e)n.length&&(t+=n.length);const n=new Uint8Array(t);let r=0;for(const t of e)n.set(t,r),r+=t.length;return n}(r,o.default.sign.detached(i,this.sk)));return t.length>0?[g.slice(0,9),s,a(t)].join("."):[g.slice(0,9),s].join(".")}async verify(e,t=""){const{msg:n,sig:r,footer:i}=await this.decompose(e),s=l(p,n,d(i),d(t));return f(o.default.sign.detached.verify(s,r,this.pk),"Invalid signature"),(new TextDecoder).decode(n)}async decompose(e){f(y(d(e.slice(0,10)),p),"Invalid token");const t=e.split("."),n=c(t[2],!0),r=n.length;return{msg:n.slice(0,r-64),sig:n.slice(r-64),footer:t.length>3?c(t[3]):new Uint8Array(0)}}}"undefined"!=typeof window&&(window.PasetoV4Public=h),e.PasetoV4Public=h,Object.defineProperty(e,"__esModule",{value:!0})}));//# sourceMappingURL=paseto.v4.public.js.map