No more callback trickery with auth.init

 Even more fluff removed
 From this point forward the codebase is mature enough for slimming down fearlessly
This commit is contained in:
shockrah 2021-03-07 23:53:09 -08:00
parent 7d3325fdc6
commit 6cfb7e7e4d

View File

@ -1,83 +1,54 @@
const ipcRenderer = require('electron') const { ipcRenderer } = require('electron')
const { Request } = require('./request.js') const { Request } = require('./request.js')
const jsonwebtoken = require('jsonwebtoken') const jsonwebtoken = require('jsonwebtoken')
const got = require('got') 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 let pass = false
const url = `${protocol}://${host}:${port}/login` const url = `${protocol}://${host}:${port}/login`
got(url, { const response = await got(url, {
searchParams: { id: params['id'], secret: params['secret'] }, searchParams: { id: user['id'], secret: user['secret'] },
responseType: 'json' responseType: 'json'
}) })
.then(response => {
const jwt = response.body['jwt']
let config = ipcRenderer.sendSync('config-request', server)
for(const idx in config['servers']) { const jwt = response.body['jwt']
// '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)
pass = true let config = await ipcRenderer.invoke('config-request')
break
} 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 * @returns {Config} whole config to use in ui building back in DOM-land
* 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
*/ */
exports.init = async function(config) { exports.init = async function(config) {
const servers = config['servers'] for(let conf of config['servers']) {
for(const idx in servers) { const remote = conf['server']
const remote = servers[idx]['server'] const user = conf['user']
const user = servers[idx]['user']
const url = `${remote['protocol']}://${remote['hostname']}:${remote['port']}/login` await login(remote['protocol'], remote['hostname'], remote['port'], user)
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
} }
return config
} }