Complexity in auth.login by basically cutting it in half and throwing the bad half out

 auth.init is now the more complex site since it literally has 1 chance to fire
This commit is contained in:
shockrah 2021-03-08 19:51:57 -08:00
parent c4e3fa72ce
commit ef0bc70f90

View File

@ -1,7 +1,4 @@
const { ipcRenderer } = require('electron')
const { Request } = require('./request.js')
const jsonwebtoken = require('jsonwebtoken')
const got = require('got')
@ -12,31 +9,19 @@ const got = require('got')
* @param {String} user.secret
* @param {Number} user.id
*
* @returns {String|null} JWT
*/
async function login(protocol, host, port, user) {
let pass = false
const url = `${protocol}://${host}:${port}/login`
const response = await got(url, {
searchParams: { id: user['id'], secret: user['secret'] },
responseType: 'json'
})
const jwt = response.body['jwt']
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
responseType: 'json',
// If DNS is seriously taking this long we honestly should just give up
timeout: {
lookup: 500
}
}
})
return response.body['jwt']
}
exports.login = login
@ -44,11 +29,25 @@ exports.login = login
/**
* @returns {Config} whole config to use in ui building back in DOM-land
*/
exports.init = async function(config) {
for(let conf of config['servers']) {
const remote = conf['server']
const user = conf['user']
exports.init = async function() {
// First we fetch the in-memnory configuration
const config = await ipcRenderer.invoke('config-request')
await login(remote['protocol'], remote['hostname'], remote['port'], user)
for(const index in config.data['servers']) {
console.log('Initial login', config.data['servers'][index])
const remote = config.data['servers'][index]['server']
const user = config.data['servers'][index]['user']
try {
// update the jwt first
const jwt = await login(remote['protocol'], remote['hostname'], remote['port'], user)
config.data['servers'][index]['user']['jwt'] = jwt
} catch (err) {
continue
}
}
console.log('after init', config)
await ipcRenderer.invoke('config-update', config)
return await ipcRenderer.invoke('config-request')
}