@4players/odin-tokens
Version:
A lightweight token generator for 4Players ODIN
2 lines (1 loc) • 5.22 kB
JavaScript
var z=new TextEncoder;var A={Base32:new TextEncoder().encode("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"),Base32Hex:new TextEncoder().encode("0123456789ABCDEFGHIJKLMNOPQRSTUV"),Base32Crockford:new TextEncoder().encode("0123456789ABCDEFGHJKMNPQRSTVWXYZ")},g={Base32:new Uint8Array(128).fill(32),Base32Hex:new Uint8Array(128).fill(32),Base32Crockford:new Uint8Array(128).fill(32)};A.Base32.forEach((e,t)=>g.Base32[e]=t);A.Base32Hex.forEach((e,t)=>g.Base32Hex[e]=t);A.Base32Crockford.forEach((e,t)=>g.Base32Crockford[e]=t);function y(e,t){let n=e.length;if(e.byteOffset){let r=new Uint8Array(e.buffer);r.set(e),e=r.subarray(0,n)}return e=new Uint8Array(e.buffer.transfer(t)),e.set(e.subarray(0,n),t-n),[e,t-n]}var V=new TextEncoder().encode("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"),O=new Uint8Array(128).fill(32);V.forEach((e,t)=>O[e]=t);var te="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".split("");var U={Base64:new TextEncoder().encode("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"),Base64Url:new TextEncoder().encode("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_")},h={Base64:new Uint8Array(128).fill(64),Base64Url:new Uint8Array(128).fill(64)};U.Base64.forEach((e,t)=>h.Base64[e]=t);U.Base64Url.forEach((e,t)=>h.Base64Url[e]=t);function d(e){return((e+2)/3|0)*4}function l(e,t,n,r,s){for(t+=2;t<e.length;t+=3){let o=e[t-2]<<16|e[t-1]<<8|e[t];e[n++]=r[o>>18],e[n++]=r[o>>12&63],e[n++]=r[o>>6&63],e[n++]=r[o&63]}switch(t){case e.length+1:{let o=e[t-2]<<16;e[n++]=r[o>>18],e[n++]=r[o>>12&63],e[n++]=s,e[n++]=s;break}case e.length:{let o=e[t-2]<<16|e[t-1]<<8;e[n++]=r[o>>18],e[n++]=r[o>>12&63],e[n++]=r[o>>6&63],e[n++]=s;break}}return n}function m(e,t,n,r,s){for(let o=e.length-2;o<e.length;++o)if(e[o]===s){for(let c=o+1;c<e.length;++c)if(e[c]!==s)throw new TypeError(`Cannot decode input as base64: Invalid character (${String.fromCharCode(e[c])})`);e=e.subarray(0,o);break}if((e.length-n)%4===1)throw new RangeError(`Cannot decode input as base64: Length (${e.length-n}), excluding padding, must not have a remainder of 1 when divided by 4`);for(t+=3;t<e.length;t+=4){let o=i(e[t-3],r)<<18|i(e[t-2],r)<<12|i(e[t-1],r)<<6|i(e[t],r);e[n++]=o>>16,e[n++]=o>>8&255,e[n++]=o&255}switch(t){case e.length+1:{let o=i(e[t-3],r)<<18|i(e[t-2],r)<<12;e[n++]=o>>16;break}case e.length:{let o=i(e[t-3],r)<<18|i(e[t-2],r)<<12|i(e[t-1],r)<<6;e[n++]=o>>16,e[n++]=o>>8&255;break}}return n}function i(e,t){let n=t[e]??64;if(n===64)throw new TypeError(`Cannot decode input as base64: Invalid character (${String.fromCharCode(e)})`);return n}var B=61,F=new TextEncoder().encode("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"),E=new Uint8Array(128).fill(64);F.forEach((e,t)=>E[e]=t);function p(e){typeof e=="string"?e=new TextEncoder().encode(e):e instanceof ArrayBuffer?e=new Uint8Array(e).slice():e=e.slice();let[t,n]=y(e,d(e.length));return l(t,n,0,F,B),new TextDecoder().decode(t)}function _(e){let t=new TextEncoder().encode(e);return new Uint8Array(t.buffer.transfer(m(t,0,0,E,B)))}var u=61,C=new TextEncoder().encode("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"),H=new Uint8Array(128).fill(64);C.forEach((e,t)=>H[e]=t);function x(e){typeof e=="string"?e=new TextEncoder().encode(e):e instanceof ArrayBuffer?e=new Uint8Array(e).slice():e=e.slice();let[t,n]=y(e,d(e.length)),r=l(t,n,0,C,u);return r=t.indexOf(u,r-2),new TextDecoder().decode(r>0?new Uint8Array(t.buffer.transfer(r)):t)}var L=new TextEncoder().encode("0123456789abcdef"),T=new Uint8Array(128).fill(16);L.forEach((e,t)=>T[e]=t);new TextEncoder().encode("ABCDEF").forEach((e,t)=>T[e]=t+10);var P=new TextEncoder().encode("0123456789abcdef"),k=new Uint8Array(128).fill(16);P.forEach((e,t)=>k[e]=t);new TextEncoder().encode("ABCDEF").forEach((e,t)=>k[e]=t+10);var v=new ArrayBuffer(8),we=new Uint32Array(v),Ue=new BigUint64Array(v);import*as a from"@noble/ed25519";function Se(){let e=new Uint8Array(33);return e[0]=1,crypto.getRandomValues(e.subarray(1,32)),e[32]=R(e.subarray(1,32)),p(e)}function $(e){let t=_(e);if(t[0]!==1||t.length!==33||R(t.subarray(1))!==0)throw new TypeError("invalid access key");return Promise.resolve(t.subarray(1))}async function j(e){let t=await a.getPublicKeyAsync(e),n=await a.etc.sha512Async(t),r=new Uint8Array(9);r[0]=1;for(let s=0,o=0;s<8;s++)for(let c=0;c<8;c++,o++)r[1+c]^=n[o];return p(r)}async function Me(e){let t=await a.getPublicKeyAsync(e);return x(t)}var S=class{keyId;secretKey;constructor(t){let n=this.secretKey=(async()=>typeof t=="string"?await $(t):t)();this.keyId=(async()=>j(await n))()}async createToken(t,n,r){let s=Math.floor(Date.now()/1e3),o={rid:t,uid:n,adr:r?.address,tgs:r?.tags,ups:r?.upstream,cid:r?.customer,sub:r?.subject??"connect",aud:r?.audience,exp:s+(r?.lifetime??300),nbf:s,internal:r?.internal},c={alg:"EdDSA",kid:await this.keyId},w=`${M(c)}.${M(o)}`,K=new TextEncoder().encode(w),I=await a.signAsync(K,await this.secretKey);return`${w}.${x(I)}`}};function R(e){let t=255;for(let n=0;n<e.length;n++){t^=e[n];for(let r=0;r<8;r++)(t&128)!==0?t=t<<1^49:t<<=1;t=255&t}return t}function M(e){return x(JSON.stringify(e))}export{S as TokenGenerator,Se as generateAccessKey,j as getKeyId,Me as getPublicKey,$ as loadAccessKey};