Browse Source

socket auth

master
zeus 4 years ago
parent
commit
b05590ff7f
  1. 114
      swarmlab-app/src/run/app.js

114
swarmlab-app/src/run/app.js

@ -44,6 +44,61 @@ const corsOptions = {
}
}
// ***************************************************
// checktoken
// ***************************************************
async function checkToken(token) {
const agent = new https.Agent({
rejectUnauthorized: false,
});
const instance = axios.create({
baseURL: 'https://api.swarmlab.io',
withCredentials: true,
rejectUnauthorized: false,
crossdomain: true,
httpsAgent: agent,
headers: {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data',
'Authorization': 'Bearer '+token
}
})
try {
var pipelines = {
"source":'ssologin'
}
var params = {
pipeline: pipelines
}
var options = {
headers: { 'content-type': 'application/x-www-form-urlencoded',Authorization: `Bearer ${token}` },
};
instance.defaults.timeout = 30000;
const res = await instance.post('/istokenvalidsso',params,options);
if(res.status == 200){
return res.data
}else{
console.log("noerror: " + res)
return res.status
}
}
catch (err) {
console.error("error: "+err);
var error = new Object();
error.action = '401'
return error
}
}
// ***************************************************
// rest get
// ***************************************************
app.get('/run', [
//check('access_token').isLength({ min: 40 }),
//check('llo').isBase64()
@ -59,6 +114,10 @@ cors(corsOptions), (req, res, next) => {
});
// ***************************************************
// rest post
// ***************************************************
app.post('/run', [
//check('access_token').isLength({ min: 40 }),
//check('llo').isBase64()
@ -93,14 +152,59 @@ cors(corsOptions), (req, res, next) => {
res.json(RES)
});
// ***************************************************
// rocket
// ***************************************************
io.origins('*:*') // for latest version
io.on('connection', s => {
console.error('socket connection');
var id = s.id
s.on('log', obj => {
console.error('from client '+ s.id + ' obj ' + obj);
});
console.error('socket connection');
// ------------------------------
// --- set
// ------------------------------
var global = new Object();
global.SOCKET = {};
global.SOCKET.error = {};
console.error('socket ...');
s.auth = false;
// ------------------------------
// --- authenticate
// ------------------------------
s.on('authenticate', function(data){
const token = data
console.log('invalid 1 ' + token);
(async() => {
var isvalid = await checkToken(token);
if(isvalid.action == 'ok'){
console.log("Authserver ok ", s.id + ' - ' + token);
global.SOCKET.user = isvalid.user
global.SOCKET.scope = isvalid.scope // space delimeter
global.SOCKET.token = isvalid.token
s.auth = true;
}else{
console.log("Authserver no ", s.id + ' - ' + token);
s.auth = false;
}
})()
});
setTimeout(function(){
if (!s.auth) {
console.log("Disconnecting timeout socket ", s.id);
s.disconnect('unauthorized');
}
}, 30000);
var id = s.id
s.on('log', obj => {
console.error('from client '+ s.id + ' obj ' + obj);
});
});

Loading…
Cancel
Save