UNPKG

@4players/odin-tokens

Version:

A lightweight token generator for 4Players ODIN

3 lines (2 loc) 12.6 kB
var We=new TextEncoder;var Z={base32:new TextEncoder().encode("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"),base32hex:new TextEncoder().encode("0123456789ABCDEFGHIJKLMNOPQRSTUV"),base32crockford:new TextEncoder().encode("0123456789ABCDEFGHJKMNPQRSTVWXYZ")},J={base32:new Uint8Array(128).fill(32),base32hex:new Uint8Array(128).fill(32),base32crockford:new Uint8Array(128).fill(32)};Z.base32.forEach((e,t)=>J.base32[e]=t);Z.base32hex.forEach((e,t)=>J.base32hex[e]=t);Z.base32crockford.forEach((e,t)=>J.base32crockford[e]=t);function S(e,t){let r=e.length;if(e.byteOffset){let n=new Uint8Array(e.buffer);n.set(e),e=n.subarray(0,r)}return e=new Uint8Array(e.buffer.transfer(t)),e.set(e.subarray(0,r),t-r),[e,t-r]}var ke=new TextEncoder().encode("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"),De=new Uint8Array(128).fill(32);ke.forEach((e,t)=>De[e]=t);var ct="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".split("");var ce={base64:new TextEncoder().encode("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"),base64url:new TextEncoder().encode("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_")},se={base64:new Uint8Array(128).fill(64),base64url:new Uint8Array(128).fill(64)};ce.base64.forEach((e,t)=>se.base64[e]=t);ce.base64url.forEach((e,t)=>se.base64url[e]=t);function M(e){return((e+2)/3|0)*4}function R(e,t,r,n,o){for(t+=2;t<e.length;t+=3){let c=e[t-2]<<16|e[t-1]<<8|e[t];e[r++]=n[c>>18],e[r++]=n[c>>12&63],e[r++]=n[c>>6&63],e[r++]=n[c&63]}switch(t){case e.length+1:{let c=e[t-2]<<16;e[r++]=n[c>>18],e[r++]=n[c>>12&63],e[r++]=o,e[r++]=o;break}case e.length:{let c=e[t-2]<<16|e[t-1]<<8;e[r++]=n[c>>18],e[r++]=n[c>>12&63],e[r++]=n[c>>6&63],e[r++]=o;break}}return r}function X(e,t,r,n,o){for(let c=e.length-2;c<e.length;++c)if(e[c]===o){for(let s=c+1;s<e.length;++s)if(e[s]!==o)throw new TypeError(`Cannot decode input as base64: Invalid character (${String.fromCharCode(e[s])})`);e=e.subarray(0,c);break}if((e.length-r)%4===1)throw new RangeError(`Cannot decode input as base64: Length (${e.length-r}), excluding padding, must not have a remainder of 1 when divided by 4`);for(t+=3;t<e.length;t+=4){let c=U(e[t-3],n)<<18|U(e[t-2],n)<<12|U(e[t-1],n)<<6|U(e[t],n);e[r++]=c>>16,e[r++]=c>>8&255,e[r++]=c&255}switch(t){case e.length+1:{let c=U(e[t-3],n)<<18|U(e[t-2],n)<<12;e[r++]=c>>16;break}case e.length:{let c=U(e[t-3],n)<<18|U(e[t-2],n)<<12|U(e[t-1],n)<<6;e[r++]=c>>16,e[r++]=c>>8&255;break}}return r}function U(e,t){let r=t[e]??64;if(r===64)throw new TypeError(`Cannot decode input as base64: Invalid character (${String.fromCharCode(e)})`);return r}var ae=61,ie=new TextEncoder().encode("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"),de=new Uint8Array(128).fill(64);ie.forEach((e,t)=>de[e]=t);function Q(e){typeof e=="string"?e=new TextEncoder().encode(e):e instanceof ArrayBuffer?e=new Uint8Array(e).slice():e=e.slice();let[t,r]=S(e,M(e.length));return R(t,r,0,ie,ae),new TextDecoder().decode(t)}function ue(e){let t=new TextEncoder().encode(e);return new Uint8Array(t.buffer.transfer(X(t,0,0,de,ae)))}var be=61,me=new TextEncoder().encode("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"),Oe=new Uint8Array(128).fill(64);me.forEach((e,t)=>Oe[e]=t);function K(e){typeof e=="string"?e=new TextEncoder().encode(e):e instanceof ArrayBuffer?e=new Uint8Array(e).slice():e=e.slice();let[t,r]=S(e,M(e.length)),n=R(t,r,0,me,be);return n=t.indexOf(be,n-2),new TextDecoder().decode(n>0?new Uint8Array(t.buffer.transfer(n)):t)}var Ve=new TextEncoder().encode("0123456789abcdef"),xe=new Uint8Array(128).fill(16);Ve.forEach((e,t)=>xe[e]=t);new TextEncoder().encode("ABCDEF").forEach((e,t)=>xe[e]=t+10);var Me=new TextEncoder().encode("0123456789abcdef"),le=new Uint8Array(128).fill(16);Me.forEach((e,t)=>le[e]=t);new TextEncoder().encode("ABCDEF").forEach((e,t)=>le[e]=t+10);var pe=new ArrayBuffer(8),wt=new Uint32Array(pe),Ut=new BigUint64Array(pe);import*as F from"@noble/ed25519";function Re(e){return e instanceof Uint8Array||ArrayBuffer.isView(e)&&e.constructor.name==="Uint8Array"}function Y(e,t,r=""){let n=Re(e),o=e?.length,c=t!==void 0;if(!n||c&&o!==t){let s=r&&`"${r}" `,i=c?` of length ${t}`:"",u=n?`length=${o}`:`type=${typeof e}`;throw new Error(s+"expected Uint8Array"+i+", got "+u)}return e}function q(e,t=!0){if(e.destroyed)throw new Error("Hash instance has been destroyed");if(t&&e.finished)throw new Error("Hash#digest() has already been called")}function he(e,t){Y(e,void 0,"digestInto() output");let r=t.outputLen;if(e.length<r)throw new Error('"digestInto() output" expected to be of length >='+r)}function I(...e){for(let t=0;t<e.length;t++)e[t].fill(0)}function G(e){return new DataView(e.buffer,e.byteOffset,e.byteLength)}function ye(e,t={}){let r=(o,c)=>e(c).update(o).digest(),n=e(void 0);return r.outputLen=n.outputLen,r.blockLen=n.blockLen,r.create=o=>e(o),Object.assign(r,t),Object.freeze(r)}var Ae=e=>({oid:Uint8Array.from([6,9,96,134,72,1,101,3,4,2,e])});var j=class{blockLen;outputLen;padOffset;isLE;buffer;view;finished=!1;length=0;pos=0;destroyed=!1;constructor(t,r,n,o){this.blockLen=t,this.outputLen=r,this.padOffset=n,this.isLE=o,this.buffer=new Uint8Array(t),this.view=G(this.buffer)}update(t){q(this),Y(t);let{view:r,buffer:n,blockLen:o}=this,c=t.length;for(let s=0;s<c;){let i=Math.min(o-this.pos,c-s);if(i===o){let u=G(t);for(;o<=c-s;s+=o)this.process(u,s);continue}n.set(t.subarray(s,s+i),this.pos),this.pos+=i,s+=i,this.pos===o&&(this.process(r,0),this.pos=0)}return this.length+=t.length,this.roundClean(),this}digestInto(t){q(this),he(t,this),this.finished=!0;let{buffer:r,view:n,blockLen:o,isLE:c}=this,{pos:s}=this;r[s++]=128,I(this.buffer.subarray(s)),this.padOffset>o-s&&(this.process(n,0),s=0);for(let a=s;a<o;a++)r[a]=0;n.setBigUint64(o-8,BigInt(this.length*8),c),this.process(n,0);let i=G(t),u=this.outputLen;if(u%4)throw new Error("_sha2: outputLen must be aligned to 32bit");let x=u/4,l=this.get();if(x>l.length)throw new Error("_sha2: outputLen bigger than state");for(let a=0;a<x;a++)i.setUint32(4*a,l[a],c)}digest(){let{buffer:t,outputLen:r}=this;this.digestInto(t);let n=t.slice(0,r);return this.destroy(),n}_cloneInto(t){t||=new this.constructor,t.set(...this.get());let{blockLen:r,buffer:n,length:o,finished:c,destroyed:s,pos:i}=this;return t.destroyed=s,t.finished=c,t.length=o,t.pos=i,o%r&&t.buffer.set(n),t}clone(){return this._cloneInto()}};var m=Uint32Array.from([1779033703,4089235720,3144134277,2227873595,1013904242,4271175723,2773480762,1595750129,1359893119,2917565137,2600822924,725511199,528734635,4215389547,1541459225,327033209]);var $=BigInt(4294967295),fe=BigInt(32);function Ke(e,t=!1){return t?{h:Number(e&$),l:Number(e>>fe&$)}:{h:Number(e>>fe&$)|0,l:Number(e&$)|0}}function ge(e,t=!1){let r=e.length,n=new Uint32Array(r),o=new Uint32Array(r);for(let c=0;c<r;c++){let{h:s,l:i}=Ke(e[c],t);[n[c],o[c]]=[s,i]}return[n,o]}var ee=(e,t,r)=>e>>>r,te=(e,t,r)=>e<<32-r|t>>>r,_=(e,t,r)=>e>>>r|t<<32-r,C=(e,t,r)=>e<<32-r|t>>>r,k=(e,t,r)=>e<<64-r|t>>>r-32,D=(e,t,r)=>e>>>r-32|t<<64-r;function f(e,t,r,n){let o=(t>>>0)+(n>>>0);return{h:e+r+(o/2**32|0)|0,l:o|0}}var we=(e,t,r)=>(e>>>0)+(t>>>0)+(r>>>0),Ue=(e,t,r,n)=>t+r+n+(e/2**32|0)|0,He=(e,t,r,n)=>(e>>>0)+(t>>>0)+(r>>>0)+(n>>>0),Be=(e,t,r,n,o)=>t+r+n+o+(e/2**32|0)|0,Ee=(e,t,r,n,o)=>(e>>>0)+(t>>>0)+(r>>>0)+(n>>>0)+(o>>>0),Te=(e,t,r,n,o,c)=>t+r+n+o+c+(e/2**32|0)|0;var _e=ge(["0x428a2f98d728ae22","0x7137449123ef65cd","0xb5c0fbcfec4d3b2f","0xe9b5dba58189dbbc","0x3956c25bf348b538","0x59f111f1b605d019","0x923f82a4af194f9b","0xab1c5ed5da6d8118","0xd807aa98a3030242","0x12835b0145706fbe","0x243185be4ee4b28c","0x550c7dc3d5ffb4e2","0x72be5d74f27b896f","0x80deb1fe3b1696b1","0x9bdc06a725c71235","0xc19bf174cf692694","0xe49b69c19ef14ad2","0xefbe4786384f25e3","0x0fc19dc68b8cd5b5","0x240ca1cc77ac9c65","0x2de92c6f592b0275","0x4a7484aa6ea6e483","0x5cb0a9dcbd41fbd4","0x76f988da831153b5","0x983e5152ee66dfab","0xa831c66d2db43210","0xb00327c898fb213f","0xbf597fc7beef0ee4","0xc6e00bf33da88fc2","0xd5a79147930aa725","0x06ca6351e003826f","0x142929670a0e6e70","0x27b70a8546d22ffc","0x2e1b21385c26c926","0x4d2c6dfc5ac42aed","0x53380d139d95b3df","0x650a73548baf63de","0x766a0abb3c77b2a8","0x81c2c92e47edaee6","0x92722c851482353b","0xa2bfe8a14cf10364","0xa81a664bbc423001","0xc24b8b70d0f89791","0xc76c51a30654be30","0xd192e819d6ef5218","0xd69906245565a910","0xf40e35855771202a","0x106aa07032bbd1b8","0x19a4c116b8d2d0c8","0x1e376c085141ab53","0x2748774cdf8eeb99","0x34b0bcb5e19b48a8","0x391c0cb3c5c95a63","0x4ed8aa4ae3418acb","0x5b9cca4f7763e373","0x682e6ff3d6b2b8a3","0x748f82ee5defb2fc","0x78a5636f43172f60","0x84c87814a1f0ab72","0x8cc702081a6439ec","0x90befffa23631e28","0xa4506cebde82bde9","0xbef9a3f7b2c67915","0xc67178f2e372532b","0xca273eceea26619c","0xd186b8c721c0c207","0xeada7dd6cde0eb1e","0xf57d4f7fee6ed178","0x06f067aa72176fba","0x0a637dc5a2c898a6","0x113f9804bef90dae","0x1b710b35131c471b","0x28db77f523047d84","0x32caab7b40c72493","0x3c9ebe0a15c9bebc","0x431d67c49c100d4c","0x4cc5d4becb3e42b6","0x597f299cfc657e2a","0x5fcb6fab3ad6faec","0x6c44198c4a475817"].map(e=>BigInt(e))),je=_e[0],$e=_e[1],E=new Uint32Array(80),T=new Uint32Array(80),re=class extends j{constructor(t){super(128,t,16,!1)}get(){let{Ah:t,Al:r,Bh:n,Bl:o,Ch:c,Cl:s,Dh:i,Dl:u,Eh:x,El:l,Fh:a,Fl:b,Gh:p,Gl:h,Hh:y,Hl:A}=this;return[t,r,n,o,c,s,i,u,x,l,a,b,p,h,y,A]}set(t,r,n,o,c,s,i,u,x,l,a,b,p,h,y,A){this.Ah=t|0,this.Al=r|0,this.Bh=n|0,this.Bl=o|0,this.Ch=c|0,this.Cl=s|0,this.Dh=i|0,this.Dl=u|0,this.Eh=x|0,this.El=l|0,this.Fh=a|0,this.Fl=b|0,this.Gh=p|0,this.Gl=h|0,this.Hh=y|0,this.Hl=A|0}process(t,r){for(let d=0;d<16;d++,r+=4)E[d]=t.getUint32(r),T[d]=t.getUint32(r+=4);for(let d=16;d<80;d++){let H=E[d-15]|0,B=T[d-15]|0,N=_(H,B,1)^_(H,B,8)^ee(H,B,7),P=C(H,B,1)^C(H,B,8)^te(H,B,7),g=E[d-2]|0,w=T[d-2]|0,O=_(g,w,19)^k(g,w,61)^ee(g,w,6),W=C(g,w,19)^D(g,w,61)^te(g,w,6),V=He(P,W,T[d-7],T[d-16]),z=Be(V,N,O,E[d-7],E[d-16]);E[d]=z|0,T[d]=V|0}let{Ah:n,Al:o,Bh:c,Bl:s,Ch:i,Cl:u,Dh:x,Dl:l,Eh:a,El:b,Fh:p,Fl:h,Gh:y,Gl:A,Hh:L,Hl:v}=this;for(let d=0;d<80;d++){let H=_(a,b,14)^_(a,b,18)^k(a,b,41),B=C(a,b,14)^C(a,b,18)^D(a,b,41),N=a&p^~a&y,P=b&h^~b&A,g=Ee(v,B,P,$e[d],T[d]),w=Te(g,L,H,N,je[d],E[d]),O=g|0,W=_(n,o,28)^k(n,o,34)^k(n,o,39),V=C(n,o,28)^D(n,o,34)^D(n,o,39),z=n&c^n&i^c&i,ve=o&s^o&u^s&u;L=y|0,v=A|0,y=p|0,A=h|0,p=a|0,h=b|0,{h:a,l:b}=f(x|0,l|0,w|0,O|0),x=i|0,l=u|0,i=c|0,u=s|0,c=n|0,s=o|0;let oe=we(O,V,ve);n=Ue(oe,w,W,z),o=oe|0}({h:n,l:o}=f(this.Ah|0,this.Al|0,n|0,o|0)),{h:c,l:s}=f(this.Bh|0,this.Bl|0,c|0,s|0),{h:i,l:u}=f(this.Ch|0,this.Cl|0,i|0,u|0),{h:x,l}=f(this.Dh|0,this.Dl|0,x|0,l|0),{h:a,l:b}=f(this.Eh|0,this.El|0,a|0,b|0),{h:p,l:h}=f(this.Fh|0,this.Fl|0,p|0,h|0),{h:y,l:A}=f(this.Gh|0,this.Gl|0,y|0,A|0),{h:L,l:v}=f(this.Hh|0,this.Hl|0,L|0,v|0),this.set(n,o,c,s,i,u,x,l,a,b,p,h,y,A,L,v)}roundClean(){I(E,T)}destroy(){I(this.buffer),this.set(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)}},ne=class extends re{Ah=m[0]|0;Al=m[1]|0;Bh=m[2]|0;Bl=m[3]|0;Ch=m[4]|0;Cl=m[5]|0;Dh=m[6]|0;Dl=m[7]|0;Eh=m[8]|0;El=m[9]|0;Fh=m[10]|0;Fl=m[11]|0;Gh=m[12]|0;Gl=m[13]|0;Hh=m[14]|0;Hl=m[15]|0;constructor(){super(64)}};var Ce=ye(()=>new ne,Ae(3));F.hashes.sha512=Ce;function Wt(){let e=new Uint8Array(33);return e[0]=1,crypto.getRandomValues(e.subarray(1,32)),e[32]=Le(e.subarray(1,32)),Q(e)}function Ne(e){let t=ue(e);if(t[0]!==1||t.length!==33||Le(t.subarray(1))!==0)throw new TypeError("invalid access key");return Promise.resolve(t.subarray(1))}async function Pe(e){let t=await F.getPublicKeyAsync(e),r=new Uint8Array(await crypto.subtle.digest("SHA-512",t.slice().buffer)),n=new Uint8Array(9);n[0]=1;for(let o=0,c=0;o<8;o++)for(let s=0;s<8;s++,c++)n[1+s]^=r[c];return Q(n)}async function zt(e){let t=await F.getPublicKeyAsync(e);return K(t)}var Fe=class{keyId;secretKey;constructor(t){let r=this.secretKey=(async()=>typeof t=="string"?await Ne(t):t)();this.keyId=(async()=>Pe(await r))()}async sign(t){let r={alg:"EdDSA",kid:await this.keyId},n=`${Se(r)}.${Se(t)}`,o=new TextEncoder().encode(n),c=await F.signAsync(o,await this.secretKey);return`${n}.${K(c)}`}createToken(t,r,n){let o=Math.floor(Date.now()/1e3);return this.sign({rid:t,uid:r,adr:n?.address,tgs:n?.tags,ups:n?.upstream,cid:n?.customer,sub:n?.subject??"connect",aud:n?.audience,exp:o+(n?.lifetime??300),nbf:o,internal:n?.internal})}};function Le(e){let t=255;for(let r=0;r<e.length;r++){t^=e[r];for(let n=0;n<8;n++)(t&128)!==0?t=t<<1^49:t<<=1;t=255&t}return t}function Se(e){return K(JSON.stringify(e))}export{Fe as TokenGenerator,Wt as generateAccessKey,Pe as getKeyId,zt as getPublicKey,Ne as loadAccessKey}; /*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */