new func for requesting jwt tokens from a server
This commit is contained in:
parent
687b514d0d
commit
d7e57c0751
@ -1,3 +1,5 @@
|
|||||||
|
const ipcRenderer = require('electron')
|
||||||
|
const { Request } = require('./request.js')
|
||||||
const jsonwebtoken = require('jsonwebtoken')
|
const jsonwebtoken = require('jsonwebtoken')
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -6,12 +8,38 @@ const jsonwebtoken = require('jsonwebtoken')
|
|||||||
* @param {String} jwt token string to verify
|
* @param {String} jwt token string to verify
|
||||||
* @returns Boolean
|
* @returns Boolean
|
||||||
*/
|
*/
|
||||||
exports.valid_jwt = function (jwt) {
|
exports.old_jwt = function (jwt) {
|
||||||
const result = jsonwebtoken.decode(jwt)
|
const result = jsonwebtoken.decode(jwt)
|
||||||
if(result) {
|
if(result) {
|
||||||
const now = Math.floor(Date.now() / 1000)
|
const now = Math.floor(Date.now() / 1000)
|
||||||
return result['exp'] > now // does it expire later?
|
return result['exp'] > now
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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']
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
reason => {
|
||||||
|
console.log('Failure to login: ', reason)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user