Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('Should still work if token verification is asynchronous', function (done) {
var port = 8511;
server = socketClusterServer.listen(port, {
authKey: serverOptions.authKey,
authVerifyAsync: false
});
server.once('connection', connectionHandler);
server.once('ready', function () {
client = socketClusterClient.create({
hostname: clientOptions.hostname,
port: port,
multiplex: false
});
client.once('connect', function (statusA) {
client.emit('login', {username: 'bob'});
client.once('authenticate', function (newSignedToken) {
client.once('disconnect', function () {
client.once('connect', function (statusB) {
assert.equal(statusB.isAuthenticated, true);
it('Token should be available inside login callback if token engine signing is synchronous', function (done) {
var port = 8509;
server = socketClusterServer.listen(port, {
authKey: serverOptions.authKey,
authSignAsync: false
});
server.once('connection', connectionHandler);
server.once('ready', function () {
client = socketClusterClient.create({
hostname: clientOptions.hostname,
port: port,
multiplex: false
});
client.once('connect', function (statusA) {
client.emit('login', {username: 'bob'}, function (err) {
assert.equal(client.authState, 'authenticated');
assert.notEqual(client.authToken, null);
assert.equal(client.authToken.username, 'bob');
done();
it('If token engine signing is asynchronous, authentication can be captured using the authenticate event', function (done) {
var port = 8510;
server = socketClusterServer.listen(port, {
authKey: serverOptions.authKey,
authSignAsync: true
});
server.once('connection', connectionHandler);
server.once('ready', function () {
client = socketClusterClient.create({
hostname: clientOptions.hostname,
port: port,
multiplex: false
});
client.once('connect', function (statusA) {
client.emit('login', {username: 'bob'});
client.once('authenticate', function (newSignedToken) {
assert.equal(client.authState, 'authenticated');
assert.notEqual(client.authToken, null);
assert.equal(client.authToken.username, 'bob');
beforeEach('Run the server before start', function (done) {
serverOptions = {
authKey: 'testkey',
ackTimeout: 200
};
server = socketClusterServer.listen(portNumber, serverOptions);
server.on('connection', connectionHandler);
server.addMiddleware(server.MIDDLEWARE_AUTHENTICATE, function (req, next) {
if (req.authToken.username === 'alice') {
var err = new Error('Blocked by MIDDLEWARE_AUTHENTICATE');
err.name = 'AuthenticateMiddlewareError';
next(err);
} else {
next();
}
});
clientOptions = {
hostname: '127.0.0.1',
port: portNumber,
multiplex: false,