* Adapting auth module to new config structure

+ Explicit login route handler now in takes care of logging in to 1 specific domain on request

+ auth.init which just hits /login for all available servers it can find
This commit is contained in:
shockrah 2021-03-03 16:28:55 -08:00
parent 12c3d300ba
commit 25876f90e4

View File

@ -1,6 +1,7 @@
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
@ -22,24 +23,61 @@ exports.old_jwt = function (jwt) {
* @param {Object} id
*
*/
exports.request_new_token = function(server) {
const domain = server['domain']
const id = server['id']
const secret = server['secret']
const port = server['port']
exports.login = function(protocol, host, port, params) {
Request('post', domain, port, '/login', {id: id, secret: secret}).then(
response => {
if(response.status_code == 200) {
const body = JSON.parse(response.body)
server['jwt'] = body
// finally notify the ipc of the config change
ipcRenderer.sendSync('config-update', server)
let pass = false
const url = `${protocol}://${host}:${port}/login`
got(url, {
searchParams: { id: params['id'], secret: params['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)
pass = true
break
}
},
reason => {
console.log('Failure to login: ', reason)
}
)
})
.catch(err => {
pass = false
})
return pass
}
/**
* @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
*/
exports.init = async function(config) {
const servers = config['servers']
for(const idx in servers) {
const remote = servers[idx]['server']
const user = servers[idx]['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
}
return config
}