2019-10-14 04:13:25 +02:00
|
|
|
'use strict'; // Node 4.x workaround
|
|
|
|
|
2019-10-13 15:52:14 +02:00
|
|
|
const express = require('express');
|
|
|
|
const http = require('http');
|
2015-11-18 21:36:24 +01:00
|
|
|
|
2019-10-13 15:52:14 +02:00
|
|
|
const socketIo = require('socket.io');
|
|
|
|
const socketio_jwt = require('../../lib');
|
2015-11-18 21:36:24 +01:00
|
|
|
|
2019-10-13 15:52:14 +02:00
|
|
|
const jwt = require('jsonwebtoken');
|
|
|
|
const xtend = require('xtend');
|
|
|
|
const bodyParser = require('body-parser');
|
|
|
|
const enableDestroy = require('server-destroy');
|
2015-11-18 21:36:24 +01:00
|
|
|
|
2019-10-13 15:52:14 +02:00
|
|
|
let sio;
|
2015-11-18 21:36:24 +01:00
|
|
|
|
2019-10-14 01:46:30 +02:00
|
|
|
exports.start = (options, callback) => {
|
2019-10-13 15:52:14 +02:00
|
|
|
const SECRETS = {
|
2015-11-18 21:36:24 +01:00
|
|
|
123: 'aaafoo super sercret',
|
|
|
|
555: 'other'
|
|
|
|
};
|
|
|
|
|
2019-10-14 01:46:30 +02:00
|
|
|
if (typeof options == 'function') {
|
2015-11-18 21:36:24 +01:00
|
|
|
callback = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
options = xtend({
|
2019-10-14 01:46:30 +02:00
|
|
|
secret: (request, decodedToken, callback) => {
|
2015-11-18 21:36:24 +01:00
|
|
|
callback(null, SECRETS[decodedToken.id]);
|
|
|
|
},
|
|
|
|
timeout: 1000,
|
|
|
|
handshake: true
|
|
|
|
}, options);
|
|
|
|
|
2019-10-13 15:52:14 +02:00
|
|
|
const app = express();
|
|
|
|
const server = http.createServer(app);
|
|
|
|
sio = socketIo.listen(server);
|
2015-11-18 21:36:24 +01:00
|
|
|
|
|
|
|
app.use(bodyParser.json());
|
2019-10-14 01:46:30 +02:00
|
|
|
app.post('/login', (req, res) => {
|
2019-10-13 15:52:14 +02:00
|
|
|
const profile = {
|
2015-11-18 21:36:24 +01:00
|
|
|
first_name: 'John',
|
|
|
|
last_name: 'Doe',
|
|
|
|
email: 'john@doe.com',
|
|
|
|
id: req.body.username === 'valid_signature' ? 123 : 555
|
|
|
|
};
|
|
|
|
|
|
|
|
// We are sending the profile inside the token
|
2019-10-13 15:52:14 +02:00
|
|
|
const token = jwt.sign(profile, SECRETS[123], { expiresIn: 60*60*5 });
|
2015-11-18 21:36:24 +01:00
|
|
|
res.json({token: token});
|
|
|
|
});
|
|
|
|
|
|
|
|
if (options.handshake) {
|
|
|
|
sio.use(socketio_jwt.authorize(options));
|
|
|
|
|
2019-10-14 01:46:30 +02:00
|
|
|
sio.sockets.on('echo', (m) => {
|
2015-11-18 21:36:24 +01:00
|
|
|
sio.sockets.emit('echo-response', m);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
sio.sockets
|
|
|
|
.on('connection', socketio_jwt.authorize(options))
|
2019-10-14 01:46:30 +02:00
|
|
|
.on('authenticated', (socket) => {
|
|
|
|
socket.on('echo', (m) => {
|
2015-11-18 21:36:24 +01:00
|
|
|
socket.emit('echo-response', m);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
server.__sockets = [];
|
2019-10-14 01:46:30 +02:00
|
|
|
server.on('connection', (c) => {
|
2015-11-18 21:36:24 +01:00
|
|
|
server.__sockets.push(c);
|
|
|
|
});
|
|
|
|
|
|
|
|
server.listen(9000, callback);
|
|
|
|
enableDestroy(server);
|
|
|
|
};
|
|
|
|
|
2019-10-14 01:46:30 +02:00
|
|
|
exports.stop = (callback) => {
|
2015-11-18 21:36:24 +01:00
|
|
|
sio.close();
|
|
|
|
try {
|
|
|
|
server.destroy();
|
|
|
|
} catch (er) {}
|
|
|
|
|
|
|
|
callback();
|
|
|
|
};
|