UNPKG

librecast-live

Version:

Live Streaming Video Platform with IPv6 Multicast

2 lines (1 loc) 2.96 kB
const util=LIBRECAST.util;class Auth{constructor(e,t,s,i){console.log("auth class constructor"),this.lctx=e,this.replyCallback=i,this.sock=[],this.chan=[],this.event=[],this.authKey=new Key(t),this.keypair=void 0!==s?s:sodium.crypto_box_keypair(),this.ready=new Promise(((e,t)=>{this.lctx.onconnect.then((()=>{this.sock.auth=new LIBRECAST.Socket(this.lctx),this.sock.repl=new LIBRECAST.Socket(this.lctx),this.chan.auth=new LIBRECAST.Channel(this.lctx,this.authKey.combo),this.chan.repl=new LIBRECAST.Channel(this.lctx,sodium.to_hex(this.keypair.publicKey));const t=[];t.push(this.sock.auth.oncreate),t.push(this.sock.repl.oncreate),t.push(this.chan.auth.oncreate),t.push(this.chan.repl.oncreate),Promise.all(t).then((()=>{console.log("auth sockets and channels ready"),this.chan.auth.bind(this.sock.auth);const t=this.chan.auth.bind(this.sock.auth),s=this.chan.repl.bind(this.sock.repl);Promise.all([t,s]).then((()=>{this.chan.repl.join().then((()=>{console.log("reply channel joined, auth ready"),e()}))})),this.sock.repl.listen((e=>{console.log("message received on reply channel");const t=this.decodePacket(e.payload);this.replyCallback(...t)}))}))}))}))}close(){console.log("auth.close()"),this.sock.auth.close(),this.sock.repl.close()}checkSignature(e){return sodium.crypto_sign_open(e,this.authKey.sign)}decodePacket(e,t){const s=util.wireUnpack(e),i=s[0],o=s[1];s[2];let h,c,n;if([h,c,n]=s[2],void 0===t&&(t=0),!this.keysEqual(h,this.authKey.crypt))throw"bad auth key received";const r=sodium.crypto_box_open_easy(n,c,h,this.keypair.privateKey);console.log("packet decoded");return[i,o,util.wireUnpack7Bit(r.buffer,t),r.buffer]}keysEqual(e,t){const s=e.byteLength;if(s!==t.byteLength)return!1;for(let i=0;i<s;i++)if(e[i]!==t[i])return!1;return!0}send(e,t,s){const i=sodium.randombytes_buf(sodium.crypto_secretbox_NONCEBYTES),o=sodium.to_string(t),h=sodium.crypto_box_easy(o,i,this.authKey.crypt,this.keypair.privateKey),c=[this.keypair.publicKey,i,h],n=util.wirePack(e,s,c);this.chan.auth.send(n)}login(e,t){const s=[sodium.to_hex(this.keypair.publicKey),"",e,t,"service"],i=util.wirePackPre([],s);this.send(8,i,0)}setPassword(e,t){const s=[sodium.to_hex(this.keypair.publicKey),e,t],i=util.wirePackPre([],s);this.send(4,i,0)}signup(e,t){console.log("signing up with email "+e);const s=[sodium.to_hex(this.keypair.publicKey),"",e,t,""],i=util.wirePackPre([],s);this.send(1,i,7)}}class Key{constructor(e){this.comboKeyHex=e}get combo(){return this.comboKeyHex}get cryptHex(){return void 0===this.cryptKeyHex&&(this.cryptKeyHex=this.comboKeyHex.slice(0,2*sodium.crypto_box_PUBLICKEYBYTES)),this.cryptKeyHex}get signHex(){return void 0===this.signKeyHex&&(this.signKeyHex=this.comboKeyHex.slice(2*sodium.crypto_box_PUBLICKEYBYTES)),this.signKeyHex}get crypt(){return void 0===this.cryptKey&&(this.cryptKey=sodium.from_hex(this.cryptHex)),this.cryptKey}get sign(){return void 0===this.signKey&&(this.signKey=sodium.from_hex(this.signHex)),this.signKey}}