From 6cfb7e7e4d3fa0ab469c5caed77022433c704a44 Mon Sep 17 00:00:00 2001 From: shockrah Date: Sun, 7 Mar 2021 23:53:09 -0800 Subject: [PATCH] =?UTF-8?q?=E2=9E=96=20No=20more=20callback=20trickery=20w?= =?UTF-8?q?ith=20auth.init=20=E2=9E=96=20Even=20more=20fluff=20removed=20?= =?UTF-8?q?=E2=9D=97=20From=20this=20point=20forward=20the=20codebase=20is?= =?UTF-8?q?=20mature=20enough=20for=20slimming=20down=20fearlessly?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- freechat-client/src/auth.js | 87 +++++++++++++------------------------ 1 file changed, 29 insertions(+), 58 deletions(-) diff --git a/freechat-client/src/auth.js b/freechat-client/src/auth.js index c8a7627..1dda481 100644 --- a/freechat-client/src/auth.js +++ b/freechat-client/src/auth.js @@ -1,83 +1,54 @@ -const ipcRenderer = require('electron') +const { ipcRenderer } = require('electron') const { Request } = require('./request.js') + const jsonwebtoken = require('jsonwebtoken') const got = require('got') -/** - * Checks to see if the jwt is not expired basically - * - * @param {String} jwt token string to verify - * @returns Boolean - */ -exports.old_jwt = function (jwt) { - const result = jsonwebtoken.decode(jwt) - if(result) { - const now = Math.floor(Date.now() / 1000) - return result['exp'] > now - } else { - return false; - } -} /** - * @param {Object} id + * @param {String} protocol http|https + * @param {String} host Just the hostname one.two.xyz + * @param {u16} port Port to use + * @param {String} user.secret + * @param {Number} user.id * */ -exports.login = function(protocol, host, port, params) { +async function login(protocol, host, port, user) { let pass = false const url = `${protocol}://${host}:${port}/login` - got(url, { - searchParams: { id: params['id'], secret: params['secret'] }, + const response = await got(url, { + searchParams: { id: user['id'], secret: user['secret'] }, responseType: 'json' }) - .then(response => { - const jwt = response.body['jwt'] - let config = ipcRenderer.sendSync('config-request', server) - for(const idx in config['servers']) { - // 'tism: once we find the right server conf updated it and jam it back in - // to disk, this high level of granularity is ugly but the alternatives - // so much worse - if(config['servers'][idx]['server']['host'] == host) { - config['servers'][idx]['server']['jwt'] = jwt - ipcRenderer.sendSync('config-update', update) + const jwt = response.body['jwt'] - pass = true - break - } + let config = await ipcRenderer.invoke('config-request') + + for(const idx in config['servers']) { + // 'tism: once we find the right server conf updated it and jam it back in + // to disk, this high level of granularity is ugly but the alternatives + // so much worse + if(config['servers'][idx]['server']['host'] == host) { + config['servers'][idx]['server']['jwt'] = jwt + await ipcRenderer.invoke('config-update', config) + break } - }) - .catch(err => { - pass = false - }) - - return pass + } } +exports.login = login + /** - * @param {Config} config Use this to /login to each server - * Without this we might end up using busted JWT's so it's best to just refresh them now - * Use this to refresh _all_ the jwt's in all the servers configs + * @returns {Config} whole config to use in ui building back in DOM-land */ - exports.init = async function(config) { - const servers = config['servers'] - for(const idx in servers) { - const remote = servers[idx]['server'] - const user = servers[idx]['user'] + for(let conf of config['servers']) { + const remote = conf['server'] + const user = conf['user'] - const url = `${remote['protocol']}://${remote['hostname']}:${remote['port']}/login` - - const response = await got(url, { - searchParams: {id: user['id'], secret: user['secret']}, - responseType: 'json' - }) - - const jwt = response.body['jwt'] - - config['servers'][idx]['user']['jwt'] = jwt + await login(remote['protocol'], remote['hostname'], remote['port'], user) } - return config }