UNPKG

@sexycoders/libauth.js

Version:

A full service for asymetric passwordless authentication.

211 lines (186 loc) 5.68 kB
function MOVE(location,param,cors_flag) { if(cors_flag) cors_flag=0; else { var t=window.__auth_system; t.pRSA="FORBIDEN"; t=btoa(JSON.stringify(t.user)); t=new URLSearchParams(t).toString() var dest=location+"?__auth_move="+t; if(param) window.open(dest); else window.location.replace(dest); } } function POST(system,post_data) { var seed=Math.floor(Math.random()*1000000000)+1; //var signature=Sign(seed,user.rsa); var signature=Sign(seed,system.pRSA); var json_send=new Object(); json_send.command="message"; json_send.user=new Object(); json_send.user.id=system.user; json_send.user.seed=seed; json_send.user.sign=btoa(signature); var t=new Object(); t.md = forge.md.md5.create(); t.md.update(system.hash); var IV=t.md.digest().bytes(); delete t.md; t.md = forge.md.sha256.create(); t.md.update(system.hash); var cipher = forge.cipher.createCipher('AES-CBC',t.md.digest().bytes()); cipher.start({iv:IV}); cipher.update(forge.util.createBuffer(JSON.stringify(post_data))); cipher.finish(); delete t.md; json_send.data=btoa(cipher.output.bytes()); console.log("Json Is: "+JSON.stringify(json_send)); var to_return; var send=btoa(JSON.stringify(json_send)); $.ajax({ type: 'POST', headers: {"Access-Control-Allow-Origin":"localhost:56083"}, url: window.system.auth_server, data: send, success: function(response) { var data=JSON.parse(atob(response)); var T=new Object(); T.md = forge.md.md5.create(); T.md.update(system.hash); var IV=T.md.digest().bytes(); delete T.md; T.md = forge.md.sha256.create(); T.md.update(system.hash); var cipher = forge.cipher.createDecipher('AES-CBC',T.md.digest().bytes()); cipher.start({iv:IV}); cipher.update(forge.util.createBuffer(atob(data.data))); cipher.finish(); delete T.md; to_return=JSON.parse(cipher.output.bytes()); }, async:false }); return to_return; } function handshake(system) { //if(typeof window.system == 'undefined') //{ //var auth_page="http://localhost:56082/login.html"; //window.location.replace(auth_page); //} //var system=window.system; //if(system.KEY_SET!=1) //MOVE(system.auth_page); var seed=Math.floor(Math.random()*1000000000)+1; //var signature=Sign(seed,user.rsa); var signature=Sign(seed,system.pRSA); var json_send=new Object(); json_send.command="request_handshake"; json_send.user=new Object(); json_send.user.id=system.user; json_send.user.seed=seed; json_send.user.sign=btoa(signature); console.log("Json Is: "+JSON.stringify(json_send)); var send=btoa(JSON.stringify(json_send)); $.ajax({ type: 'POST', headers: {"Access-Control-Allow-Origin":"localhost:56083"}, url: window.system.auth_server, data: send, success: function(response) { var data=JSON.parse(atob(response)); console.log("server response: "+JSON.stringify(data)); if(data.message=="connection_refused") { MOVE(window.system.auth_page,false); return 1; } var privateKey=forge.pki.privateKeyFromPem(system.pRSA); var decrypted = privateKey.decrypt(atob(data.hash),"RSAES-PKCS1-V1_5"); system.hash=decrypted; console.log(decrypted); }, async:false }); } class AuthSystem { //public vars for use in funcions INIT_FLAG=0; //auth properties pRSA; hash; user; //general properties domain_location=window.location.href; protocol=window.location.protocol; domain=window.document.domain; port=window.location.port; parent=null; home="http://test.uniclient.localhost:8088"; auth_server_domain="http://auth.localhost"; auth_page=this.auth_server_domain+"/login.html" auth_domain="http://auth-serve.localhost"; auth_server=this.auth_domain+"/php/src/main.php"; //auth_server="http://localhost:7890/php/src/main.php"; //flags REDIRECT_FLAG=0; //temp storage for stuff //dont use in functions _enc_prsa; _rsa; //flags KEY_SET=0; //functions setpRSA(T) { this.pRSA=T; } setHash(T) { this.hash=T; } setUser(T) { this.user=T; } setDomain(T) { this.domain=T; } setDomainLocation(T) { this.domain_location=T; } setParent(T) { this.parent=T; } setAuthServer(T) { this.auth_server=T; } set_enc_prsa(T) { this._enc_prsa=T; this.KEY_SET=1; } }; function init(){ var t=new AuthSystem(); console.log(window.location.href); console.log(t.auth_page); if(window.location.href==t.auth_page || window.location.href+"login.html"==t.auth_page) window.__auth_system=new AuthSystem(); } window.addEventListener("load",init());