Fixed travis
- Tests against Node 4, 8, 10, 12 and newest Modernized: - Use arrow functions - Use string templates in examples and some code - Use single quote for strings
This commit is contained in:
parent
93e6b59710
commit
8f2b55a7aa
@ -1,5 +1,7 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "0.12"
|
||||
- "0.11"
|
||||
- "0.10"
|
||||
- 4
|
||||
- 8
|
||||
- 10
|
||||
- 12
|
||||
- node
|
||||
|
29
README.md
29
README.md
@ -41,9 +41,10 @@ io.sockets
|
||||
.on('connection', socketioJwt.authorize({
|
||||
secret: 'your secret or public key',
|
||||
timeout: 15000 // 15 seconds to send the authentication message
|
||||
})).on('authenticated', function(socket) {
|
||||
}))
|
||||
.on('authenticated', (socket) => {
|
||||
//this socket is authenticated, we are good to handle more events from it.
|
||||
console.log('hello! ' + socket.decoded_token.name);
|
||||
console.log(`hello! ${socket.decoded_token.name}`);
|
||||
});
|
||||
```
|
||||
|
||||
@ -53,14 +54,14 @@ io.sockets
|
||||
|
||||
```javascript
|
||||
const socket = io.connect('http://localhost:9000');
|
||||
socket.on('connect', function () {
|
||||
socket.on('connect', () => {
|
||||
socket
|
||||
.emit('authenticate', { token: jwt }) //send the jwt
|
||||
.on('authenticated', function () {
|
||||
.on('authenticated', () => {
|
||||
//do other things
|
||||
})
|
||||
.on('unauthorized', function(msg) {
|
||||
console.log("unauthorized: " + JSON.stringify(msg.data));
|
||||
.on('unauthorized', (msg) => {
|
||||
console.log(`unauthorized: ${JSON.stringify(msg.data)}`);
|
||||
throw new Error(msg.data.type);
|
||||
})
|
||||
});
|
||||
@ -83,7 +84,7 @@ io.set('authorization', socketioJwt.authorize({
|
||||
handshake: true
|
||||
}));
|
||||
|
||||
io.on('connection', function (socket) {
|
||||
io.on('connection', (socket) => {
|
||||
console.log('hello!', socket.handshake.decoded_token.name);
|
||||
});
|
||||
```
|
||||
@ -96,7 +97,7 @@ io.use(socketioJwt.authorize({
|
||||
handshake: true
|
||||
}));
|
||||
|
||||
io.on('connection', function (socket) {
|
||||
io.on('connection', (socket) => {
|
||||
console.log('hello!', socket.decoded_token.name);
|
||||
});
|
||||
```
|
||||
@ -109,7 +110,7 @@ Append the jwt token using query string:
|
||||
|
||||
```javascript
|
||||
const socket = io.connect('http://localhost:9000', {
|
||||
'query': 'token=' + your_jwt
|
||||
query: `token=${your_jwt}`
|
||||
});
|
||||
```
|
||||
|
||||
@ -117,7 +118,7 @@ Append the jwt token using 'Authorization Header' (Bearer Token):
|
||||
|
||||
```javascript
|
||||
const socket = io.connect('http://localhost:9000', {
|
||||
'extraHeaders': { Authorization: `Bearer ${your_jwt}` }
|
||||
extraHeaders: { Authorization: `Bearer ${your_jwt}` }
|
||||
});
|
||||
```
|
||||
|
||||
@ -152,7 +153,7 @@ Your client-side code should handle it as below:
|
||||
**Client side**
|
||||
|
||||
```javascript
|
||||
socket.on('unauthorized', function(error) {
|
||||
socket.on('unauthorized', (error) => {
|
||||
if (error.data.type == 'UnauthorizedError' || error.data.code == 'invalid_token') {
|
||||
// redirect user to login page perhaps?
|
||||
console.log('User token has expired');
|
||||
@ -173,7 +174,7 @@ No further configuration needed.
|
||||
Add a callback client-side to execute socket disconnect server-side.
|
||||
|
||||
```javascript
|
||||
socket.on('unauthorized', function(error, callback) {
|
||||
socket.on('unauthorized', (error, callback) => {
|
||||
if (error.data.type == 'UnauthorizedError' || error.data.code == 'invalid_token') {
|
||||
// redirect user to login page perhaps or execute callback:
|
||||
callback();
|
||||
@ -215,7 +216,7 @@ Your client-side code should handle it as below:
|
||||
**Client side**
|
||||
|
||||
```javascript
|
||||
socket.on('unauthorized', function(error, callback) {
|
||||
socket.on('unauthorized', (error, callback) => {
|
||||
if (error.data.type == 'UnauthorizedError' || error.data.code == 'invalid_token') {
|
||||
// redirect user to login page perhaps or execute callback:
|
||||
callback();
|
||||
@ -240,7 +241,7 @@ const SECRETS = {
|
||||
}
|
||||
|
||||
io.use(socketioJwt.authorize({
|
||||
secret: function(request, decodedToken, callback) {
|
||||
secret: (request, decodedToken, callback) => {
|
||||
// SECRETS[decodedToken.userId] will be used as a secret or
|
||||
// public key for connection user.
|
||||
|
||||
|
@ -5,7 +5,7 @@ function UnauthorizedError (code, error) {
|
||||
this.data = {
|
||||
message: this.message,
|
||||
code: code,
|
||||
type: "UnauthorizedError"
|
||||
type: 'UnauthorizedError'
|
||||
};
|
||||
}
|
||||
|
||||
|
33
lib/index.js
33
lib/index.js
@ -6,7 +6,7 @@ function noQsMethod (options) {
|
||||
const defaults = { required: true };
|
||||
options = xtend(defaults, options);
|
||||
|
||||
return function (socket) {
|
||||
return (socket) => {
|
||||
const server = this.server || socket.server;
|
||||
|
||||
if (!server.$emit) {
|
||||
@ -19,18 +19,18 @@ function noQsMethod (options) {
|
||||
|
||||
let auth_timeout = null;
|
||||
if (options.required) {
|
||||
auth_timeout = setTimeout(function () {
|
||||
auth_timeout = setTimeout(() => {
|
||||
socket.disconnect('unauthorized');
|
||||
}, options.timeout || 5000);
|
||||
}
|
||||
|
||||
socket.on('authenticate', function (data) {
|
||||
socket.on('authenticate', (data) => {
|
||||
if (options.required) {
|
||||
clearTimeout(auth_timeout);
|
||||
}
|
||||
|
||||
// error handler
|
||||
const onError = function (err, code) {
|
||||
const onError = (err, code) => {
|
||||
if (err) {
|
||||
code = code || 'unknown';
|
||||
const error = new UnauthorizedError(code, {
|
||||
@ -46,12 +46,13 @@ function noQsMethod (options) {
|
||||
options.callback = Math.abs(options.callback);
|
||||
}
|
||||
}
|
||||
callback_timeout = setTimeout(function () {
|
||||
|
||||
callback_timeout = setTimeout(() => {
|
||||
socket.disconnect('unauthorized');
|
||||
}, (options.callback === false ? 0 : options.callback));
|
||||
}
|
||||
|
||||
socket.emit('unauthorized', error, function() {
|
||||
socket.emit('unauthorized', error, () => {
|
||||
if (typeof options.callback === 'number') {
|
||||
clearTimeout(callback_timeout);
|
||||
}
|
||||
@ -63,20 +64,20 @@ function noQsMethod (options) {
|
||||
|
||||
const token = options.cookie ? socket.request.cookies[options.cookie] : (data ? data.token : undefined);
|
||||
|
||||
if (!token || typeof token !== "string") {
|
||||
if (!token || typeof token !== 'string') {
|
||||
return onError({ message: 'invalid token datatype' }, 'invalid_token');
|
||||
}
|
||||
|
||||
// Store encoded JWT
|
||||
socket[options.encodedPropertyName] = token;
|
||||
|
||||
const onJwtVerificationReady = function (err, decoded) {
|
||||
const onJwtVerificationReady = (err, decoded) => {
|
||||
if (err) {
|
||||
return onError(err, 'invalid_token');
|
||||
}
|
||||
|
||||
// success handler
|
||||
const onSuccess = function () {
|
||||
const onSuccess = () => {
|
||||
socket[options.decodedPropertyName] = decoded;
|
||||
socket.emit('authenticated');
|
||||
if (server.$emit) {
|
||||
@ -99,7 +100,7 @@ function noQsMethod (options) {
|
||||
}
|
||||
};
|
||||
|
||||
const onSecretReady = function (err, secret) {
|
||||
const onSecretReady = (err, secret) => {
|
||||
if (err || !secret) {
|
||||
return onError(err, 'invalid_secret');
|
||||
}
|
||||
@ -116,7 +117,7 @@ function authorize (options, onConnection) {
|
||||
options = xtend({ decodedPropertyName: 'decoded_token', encodedPropertyName: 'encoded_token' }, options);
|
||||
|
||||
if (typeof options.secret !== 'string' && typeof options.secret !== 'function') {
|
||||
throw new Error('Provided secret "' + options.secret + '" is invalid, must be of type string or function.');
|
||||
throw new Error(`Provided secret ${options.secret} is invalid, must be of type string or function.`);
|
||||
}
|
||||
|
||||
if (!options.handshake) {
|
||||
@ -124,14 +125,14 @@ function authorize (options, onConnection) {
|
||||
}
|
||||
|
||||
const defaults = {
|
||||
success: function (socket, accept) {
|
||||
success: (socket, accept) => {
|
||||
if (socket.request) {
|
||||
accept();
|
||||
} else {
|
||||
accept(null, true);
|
||||
}
|
||||
},
|
||||
fail: function (error, socket, accept) {
|
||||
fail: (error, socket, accept) => {
|
||||
if (socket.request) {
|
||||
accept(error);
|
||||
} else {
|
||||
@ -142,7 +143,7 @@ function authorize (options, onConnection) {
|
||||
|
||||
const auth = xtend(defaults, options);
|
||||
|
||||
return function (socket, accept) {
|
||||
return (socket, accept) => {
|
||||
let token, error;
|
||||
const handshake = socket.handshake;
|
||||
const req = socket.request || socket;
|
||||
@ -193,7 +194,7 @@ function authorize (options, onConnection) {
|
||||
// Store encoded JWT
|
||||
socket[options.encodedPropertyName] = token;
|
||||
|
||||
const onJwtVerificationReady = function (err, decoded) {
|
||||
const onJwtVerificationReady = (err, decoded) => {
|
||||
if (err) {
|
||||
error = new UnauthorizedError(err.code || 'invalid_token', err);
|
||||
return auth.fail(error, socket, accept);
|
||||
@ -204,7 +205,7 @@ function authorize (options, onConnection) {
|
||||
return auth.success(socket, accept);
|
||||
};
|
||||
|
||||
const onSecretReady = function (err, secret) {
|
||||
const onSecretReady = (err, secret) => {
|
||||
if (err) {
|
||||
error = new UnauthorizedError(err.code || 'invalid_secret', err);
|
||||
return auth.fail(error, socket, accept);
|
||||
|
@ -3,109 +3,109 @@ const fixture = require('./fixture');
|
||||
const request = require('request');
|
||||
const io = require('socket.io-client');
|
||||
|
||||
describe('authorizer', function() {
|
||||
describe('authorizer', () => {
|
||||
//start and stop the server
|
||||
before(function(done) { fixture.start({ }, done) });
|
||||
before((done) => { fixture.start({ }, done) });
|
||||
after(fixture.stop);
|
||||
|
||||
describe('when the user is not logged in', function () {
|
||||
it('should emit error with unauthorized handshake', function (done) {
|
||||
describe('when the user is not logged in', () => {
|
||||
it('should emit error with unauthorized handshake', (done) => {
|
||||
const socket = io.connect('http://localhost:9000?token=boooooo', {
|
||||
forceNew: true
|
||||
});
|
||||
|
||||
socket.on('error', function(err) {
|
||||
err.message.should.eql("jwt malformed");
|
||||
err.code.should.eql("invalid_token");
|
||||
socket.on('error', (err) => {
|
||||
err.message.should.eql('jwt malformed');
|
||||
err.code.should.eql('invalid_token');
|
||||
socket.close();
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when the user is logged in', function() {
|
||||
before(function (done) {
|
||||
describe('when the user is logged in', () => {
|
||||
before((done) => {
|
||||
request.post({
|
||||
url: 'http://localhost:9000/login',
|
||||
form: { username: 'jose', password: 'Pa123' },
|
||||
json: true
|
||||
}, function (err, resp, body) {
|
||||
}, (err, resp, body) => {
|
||||
this.token = body.token;
|
||||
done();
|
||||
}.bind(this));
|
||||
});
|
||||
});
|
||||
|
||||
describe('authorizer disallows query string token when specified in startup options', function() {
|
||||
before(function(done) {
|
||||
describe('authorizer disallows query string token when specified in startup options', () => {
|
||||
before((done) => {
|
||||
Q.ninvoke(fixture, 'stop')
|
||||
.then(function() { return Q.ninvoke(fixture, 'start', { auth_header_required: true })})
|
||||
.then(() => Q.ninvoke(fixture, 'start', { auth_header_required: true }))
|
||||
.done(done);
|
||||
});
|
||||
|
||||
after(function(done) {
|
||||
after((done) => {
|
||||
Q.ninvoke(fixture, 'stop')
|
||||
.then(function() { return Q.ninvoke(fixture, 'start', { })})
|
||||
.then(() => Q.ninvoke(fixture, 'start', { }))
|
||||
.done(done);
|
||||
});
|
||||
|
||||
it('auth headers are supported', function (done) {
|
||||
it('auth headers are supported', (done) => {
|
||||
const socket = io.connect('http://localhost:9000', {
|
||||
forceNew: true,
|
||||
extraHeaders: { Authorization: 'Bearer ' + this.token}
|
||||
});
|
||||
|
||||
socket
|
||||
.on('connect', function() {
|
||||
.on('connect', () => {
|
||||
socket.close();
|
||||
done();
|
||||
})
|
||||
.on('error', done);
|
||||
});
|
||||
|
||||
it('auth token in query string is disallowed', function (done) {
|
||||
it('auth token in query string is disallowed', (done) => {
|
||||
const socket = io.connect('http://localhost:9000', {
|
||||
forceNew: true,
|
||||
query: 'token=' + this.token
|
||||
});
|
||||
|
||||
socket.on('error', function(err) {
|
||||
err.message.should.eql("Server requires Authorization Header");
|
||||
err.code.should.eql("missing_authorization_header");
|
||||
socket.on('error', (err) => {
|
||||
err.message.should.eql('Server requires Authorization Header');
|
||||
err.code.should.eql('missing_authorization_header');
|
||||
socket.close();
|
||||
done();
|
||||
});
|
||||
});
|
||||
})
|
||||
|
||||
describe('authorizer all auth types allowed', function() {
|
||||
before(function(done) {
|
||||
describe('authorizer all auth types allowed', () => {
|
||||
before((done) => {
|
||||
Q.ninvoke(fixture, 'stop')
|
||||
.then(function() { return Q.ninvoke(fixture, 'start', {})})
|
||||
.then(() => Q.ninvoke(fixture, 'start', {}))
|
||||
.done(done);
|
||||
})
|
||||
|
||||
it('auth headers are supported', function (done) {
|
||||
it('auth headers are supported', (done) => {
|
||||
const socket = io.connect('http://localhost:9000', {
|
||||
forceNew: true,
|
||||
extraHeaders: { Authorization: 'Bearer ' + this.token }
|
||||
});
|
||||
|
||||
socket
|
||||
.on('connect', function() {
|
||||
.on('connect', () => {
|
||||
socket.close();
|
||||
done();
|
||||
})
|
||||
.on('error', done);
|
||||
});
|
||||
|
||||
it('should do the handshake and connect', function (done) {
|
||||
it('should do the handshake and connect', (done) => {
|
||||
const socket = io.connect('http://localhost:9000', {
|
||||
forceNew: true,
|
||||
query: 'token=' + this.token
|
||||
});
|
||||
|
||||
socket
|
||||
.on('connect', function() {
|
||||
.on('connect', () => {
|
||||
socket.close();
|
||||
done();
|
||||
})
|
||||
@ -115,25 +115,25 @@ describe('authorizer', function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe('unsigned token', function() {
|
||||
beforeEach(function () {
|
||||
describe('unsigned token', () => {
|
||||
beforeEach(() => {
|
||||
this.token = 'eyJhbGciOiJub25lIiwiY3R5IjoiSldUIn0.eyJuYW1lIjoiSm9obiBGb28ifQ.';
|
||||
});
|
||||
|
||||
it('should not do the handshake and connect', function (done) {
|
||||
it('should not do the handshake and connect', (done) => {
|
||||
const socket = io.connect('http://localhost:9000', {
|
||||
forceNew: true,
|
||||
query: 'token=' + this.token
|
||||
});
|
||||
|
||||
socket
|
||||
.on('connect', function () {
|
||||
.on('connect', () => {
|
||||
socket.close();
|
||||
done(new Error('this shouldnt happen'));
|
||||
})
|
||||
.on('error', function (err) {
|
||||
.on('error', (err) => {
|
||||
socket.close();
|
||||
err.message.should.eql("jwt signature is required");
|
||||
err.message.should.eql('jwt signature is required');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
@ -2,46 +2,43 @@ const fixture = require('./fixture/namespace');
|
||||
const request = require('request');
|
||||
const io = require('socket.io-client');
|
||||
|
||||
describe('authorizer with namespaces', function () {
|
||||
describe('authorizer with namespaces', () => {
|
||||
|
||||
//start and stop the server
|
||||
before(fixture.start);
|
||||
|
||||
after(fixture.stop);
|
||||
|
||||
describe('when the user is not logged in', function () {
|
||||
describe('when the user is not logged in', () => {
|
||||
|
||||
it('should be able to connect to the default namespace', function (done) {
|
||||
it('should be able to connect to the default namespace', (done) => {
|
||||
io.connect('http://localhost:9000')
|
||||
.once('hi', done);
|
||||
.once('hi', () => done());
|
||||
});
|
||||
|
||||
it('should not be able to connect to the admin namespace', function (done) {
|
||||
it('should not be able to connect to the admin namespace', (done) => {
|
||||
io.connect('http://localhost:9000/admin')
|
||||
.once('disconnect', function() { done(); })
|
||||
.once('hi admin', function() {
|
||||
done(new Error('unauthenticated client was able to connect to the admin namespace'));
|
||||
});
|
||||
.once('disconnect', () => done())
|
||||
.once('hi admin', () => done(new Error('unauthenticated client was able to connect to the admin namespace')));
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('when the user is logged in', function() {
|
||||
describe('when the user is logged in', () => {
|
||||
|
||||
beforeEach(function (done) {
|
||||
beforeEach((done) => {
|
||||
request.post({
|
||||
url: 'http://localhost:9000/login',
|
||||
form: { username: 'jose', password: 'Pa123' },
|
||||
json: true
|
||||
}, function (err, resp, body) {
|
||||
}, (err, resp, body) => {
|
||||
this.token = body.token;
|
||||
done();
|
||||
}.bind(this));
|
||||
});
|
||||
});
|
||||
|
||||
it('should do the handshake and connect', function (done) {
|
||||
it('should do the handshake and connect', (done) => {
|
||||
io.connect('http://localhost:9000/admin', { forceNew: true })
|
||||
.on('authenticated', done)
|
||||
.on('authenticated', () => done())
|
||||
.emit('authenticate', { token: this.token });
|
||||
});
|
||||
});
|
||||
|
@ -2,29 +2,25 @@ const fixture = require('./fixture');
|
||||
const request = require('request');
|
||||
const io = require('socket.io-client');
|
||||
|
||||
describe('authorizer without querystring', function () {
|
||||
describe('authorizer without querystring', () => {
|
||||
|
||||
//start and stop the server
|
||||
before(function (done) {
|
||||
fixture.start({
|
||||
handshake: false
|
||||
} , done);
|
||||
before((done) => {
|
||||
fixture.start({ handshake: false }, done);
|
||||
});
|
||||
|
||||
after(fixture.stop);
|
||||
|
||||
describe('when the user is not logged in', function () {
|
||||
describe('when the user is not logged in', () => {
|
||||
|
||||
it('should close the connection after a timeout if no auth message is received', function (done) {
|
||||
it('should close the connection after a timeout if no auth message is received', (done) => {
|
||||
io.connect('http://localhost:9000', { forceNew: true })
|
||||
.once('disconnect', function () { done(); });
|
||||
.once('disconnect', () => done());
|
||||
});
|
||||
|
||||
it('should not respond echo', function (done) {
|
||||
it('should not respond echo', (done) => {
|
||||
io.connect('http://localhost:9000', { forceNew: true })
|
||||
.on('echo-response', function () {
|
||||
done(new Error('this should not happen'));
|
||||
})
|
||||
.on('echo-response', () => done(new Error('this should not happen')))
|
||||
.emit('echo', { hi: 123 });
|
||||
|
||||
setTimeout(done, 1200);
|
||||
@ -32,30 +28,28 @@ describe('authorizer without querystring', function () {
|
||||
|
||||
});
|
||||
|
||||
describe('when the user is logged in', function() {
|
||||
describe('when the user is logged in', () => {
|
||||
|
||||
beforeEach(function (done) {
|
||||
beforeEach((done) => {
|
||||
request.post({
|
||||
url: 'http://localhost:9000/login',
|
||||
form: { username: 'jose', password: 'Pa123' },
|
||||
json: true
|
||||
}, function (err, resp, body) {
|
||||
}, (err, resp, body) => {
|
||||
this.token = body.token;
|
||||
done();
|
||||
}.bind(this));
|
||||
});
|
||||
});
|
||||
|
||||
it('should do the handshake and connect', function (done) {
|
||||
it('should do the handshake and connect', (done) => {
|
||||
const socket = io.connect('http://localhost:9000', { forceNew: true });
|
||||
|
||||
socket
|
||||
.on('echo-response', function () {
|
||||
.on('echo-response', () => {
|
||||
socket.close();
|
||||
done();
|
||||
})
|
||||
.on('authenticated', function () {
|
||||
socket.emit('echo');
|
||||
})
|
||||
.on('authenticated', () => { socket.emit('echo'); })
|
||||
.emit('authenticate', { token: this.token });
|
||||
});
|
||||
});
|
||||
|
@ -2,10 +2,10 @@ const fixture = require('./fixture/secret_function');
|
||||
const request = require('request');
|
||||
const io = require('socket.io-client');
|
||||
|
||||
describe('authorizer with secret function', function () {
|
||||
describe('authorizer with secret function', () => {
|
||||
|
||||
//start and stop the server
|
||||
before(function (done) {
|
||||
before((done) => {
|
||||
fixture.start({
|
||||
handshake: false
|
||||
}, done);
|
||||
@ -13,51 +13,49 @@ describe('authorizer with secret function', function () {
|
||||
|
||||
after(fixture.stop);
|
||||
|
||||
describe('when the user is not logged in', function () {
|
||||
describe('when the user is not logged in', () => {
|
||||
|
||||
describe('and when token is not valid', function() {
|
||||
beforeEach(function (done) {
|
||||
describe('and when token is not valid', () => {
|
||||
beforeEach((done) => {
|
||||
request.post({
|
||||
url: 'http://localhost:9000/login',
|
||||
json: { username: 'invalid_signature', password: 'Pa123' }
|
||||
}, function (err, resp, body) {
|
||||
}, (err, resp, body) => {
|
||||
this.invalidToken = body.token;
|
||||
done();
|
||||
}.bind(this));
|
||||
});
|
||||
});
|
||||
|
||||
it('should emit unauthorized', function (done) {
|
||||
it('should emit unauthorized', (done) => {
|
||||
io.connect('http://localhost:9000', { forceNew: true })
|
||||
.on('unauthorized', function() { done(); })
|
||||
.on('unauthorized', () => done())
|
||||
.emit('authenticate', { token: this.invalidToken + 'ass' })
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('when the user is logged in', function() {
|
||||
describe('when the user is logged in', () => {
|
||||
|
||||
beforeEach(function (done) {
|
||||
beforeEach((done) => {
|
||||
request.post({
|
||||
url: 'http://localhost:9000/login',
|
||||
json: { username: 'valid_signature', password: 'Pa123' }
|
||||
}, function (err, resp, body) {
|
||||
}, (err, resp, body) => {
|
||||
this.token = body.token;
|
||||
done();
|
||||
}.bind(this));
|
||||
});
|
||||
});
|
||||
|
||||
it('should do the handshake and connect', function (done) {
|
||||
it('should do the handshake and connect', (done) => {
|
||||
const socket = io.connect('http://localhost:9000', { forceNew: true });
|
||||
|
||||
socket
|
||||
.on('echo-response', function () {
|
||||
.on('echo-response', () => {
|
||||
socket.close();
|
||||
done();
|
||||
})
|
||||
.on('authenticated', function () {
|
||||
socket.emit('echo');
|
||||
})
|
||||
.on('authenticated', () => { socket.emit('echo'); })
|
||||
.emit('authenticate', { token: this.token });
|
||||
});
|
||||
});
|
||||
|
@ -2,20 +2,20 @@ const fixture = require('./fixture/secret_function');
|
||||
const request = require('request');
|
||||
const io = require('socket.io-client');
|
||||
|
||||
describe('authorizer with secret function', function () {
|
||||
describe('authorizer with secret function', () => {
|
||||
|
||||
//start and stop the server
|
||||
before(fixture.start);
|
||||
after(fixture.stop);
|
||||
|
||||
describe('when the user is not logged in', function () {
|
||||
describe('when the user is not logged in', () => {
|
||||
|
||||
it('should emit error with unauthorized handshake', function (done) {
|
||||
it('should emit error with unauthorized handshake', (done) => {
|
||||
const socket = io.connect('http://localhost:9000?token=boooooo', { forceNew: true });
|
||||
|
||||
socket.on('error', function(err) {
|
||||
err.message.should.eql("jwt malformed");
|
||||
err.code.should.eql("invalid_token");
|
||||
socket.on('error', (err) => {
|
||||
err.message.should.eql('jwt malformed');
|
||||
err.code.should.eql('invalid_token');
|
||||
socket.close();
|
||||
done();
|
||||
});
|
||||
@ -23,26 +23,26 @@ describe('authorizer with secret function', function () {
|
||||
|
||||
});
|
||||
|
||||
describe('when the user is logged in', function() {
|
||||
describe('when the user is logged in', () => {
|
||||
|
||||
beforeEach(function (done) {
|
||||
beforeEach((done) => {
|
||||
request.post({
|
||||
url: 'http://localhost:9000/login',
|
||||
json: { username: 'valid_signature', password: 'Pa123' }
|
||||
}, function (err, resp, body) {
|
||||
}, (err, resp, body) => {
|
||||
this.token = body.token;
|
||||
done();
|
||||
}.bind(this));
|
||||
});
|
||||
});
|
||||
|
||||
it('should do the handshake and connect', function (done) {
|
||||
it('should do the handshake and connect', (done) => {
|
||||
const socket = io.connect('http://localhost:9000', {
|
||||
forceNew: true,
|
||||
query: 'token=' + this.token
|
||||
});
|
||||
|
||||
socket
|
||||
.on('connect', function() {
|
||||
.on('connect', () => {
|
||||
socket.close();
|
||||
done();
|
||||
})
|
||||
@ -50,25 +50,25 @@ describe('authorizer with secret function', function () {
|
||||
});
|
||||
});
|
||||
|
||||
describe('unsigned token', function() {
|
||||
beforeEach(function () {
|
||||
describe('unsigned token', () => {
|
||||
beforeEach(() => {
|
||||
this.token = 'eyJhbGciOiJub25lIiwiY3R5IjoiSldUIn0.eyJuYW1lIjoiSm9obiBGb28ifQ.';
|
||||
});
|
||||
|
||||
it('should not do the handshake and connect', function (done) {
|
||||
it('should not do the handshake and connect', (done) => {
|
||||
const socket = io.connect('http://localhost:9000', {
|
||||
forceNew: true,
|
||||
query: 'token=' + this.token
|
||||
});
|
||||
|
||||
socket
|
||||
.on('connect', function () {
|
||||
.on('connect', () => {
|
||||
socket.close();
|
||||
done(new Error('this shouldnt happen'));
|
||||
})
|
||||
.on('error', function (err) {
|
||||
.on('error', (err) => {
|
||||
socket.close();
|
||||
err.message.should.eql("jwt signature is required");
|
||||
err.message.should.eql('jwt signature is required');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
@ -11,7 +11,7 @@ const enableDestroy = require('server-destroy');
|
||||
|
||||
let sio;
|
||||
|
||||
exports.start = function (options, callback) {
|
||||
exports.start = (options, callback) => {
|
||||
|
||||
if (typeof options == 'function') {
|
||||
callback = options;
|
||||
@ -29,7 +29,7 @@ exports.start = function (options, callback) {
|
||||
sio = socketIo.listen(server);
|
||||
|
||||
app.use(bodyParser.json());
|
||||
app.post('/login', function (req, res) {
|
||||
app.post('/login', (req, res) => {
|
||||
const profile = {
|
||||
first_name: 'John',
|
||||
last_name: 'Doe',
|
||||
@ -46,28 +46,28 @@ exports.start = function (options, callback) {
|
||||
if (options.handshake) {
|
||||
sio.use(socketio_jwt.authorize(options));
|
||||
|
||||
sio.sockets.on('echo', function (m) {
|
||||
sio.sockets.on('echo', (m) => {
|
||||
sio.sockets.emit('echo-response', m);
|
||||
});
|
||||
} else {
|
||||
sio.sockets
|
||||
.on('connection', socketio_jwt.authorize(options))
|
||||
.on('authenticated', function (socket) {
|
||||
socket.on('echo', function (m) {
|
||||
.on('authenticated', (socket) => {
|
||||
socket.on('echo', (m) => {
|
||||
socket.emit('echo-response', m);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
server.__sockets = [];
|
||||
server.on('connection', function (c) {
|
||||
server.on('connection', (c) => {
|
||||
server.__sockets.push(c);
|
||||
});
|
||||
server.listen(9000, callback);
|
||||
enableDestroy(server);
|
||||
};
|
||||
|
||||
exports.stop = function (callback) {
|
||||
exports.stop = (callback) => {
|
||||
sio.close();
|
||||
try {
|
||||
server.destroy();
|
||||
|
@ -15,7 +15,7 @@ let sio;
|
||||
*
|
||||
* The /admin namespace is protected by JWTs while the global namespace is public.
|
||||
*/
|
||||
exports.start = function (callback) {
|
||||
exports.start = (callback) => {
|
||||
|
||||
const options = {
|
||||
secret: 'aaafoo super sercret',
|
||||
@ -28,7 +28,7 @@ exports.start = function (callback) {
|
||||
sio = socketIo.listen(server);
|
||||
|
||||
app.use(bodyParser.json());
|
||||
app.post('/login', function (req, res) {
|
||||
app.post('/login', (req, res) => {
|
||||
const profile = {
|
||||
first_name: 'John',
|
||||
last_name: 'Doe',
|
||||
@ -43,14 +43,14 @@ exports.start = function (callback) {
|
||||
|
||||
|
||||
|
||||
sio.on('connection', function (socket) {
|
||||
sio.on('connection', (socket) => {
|
||||
socket.emit('hi');
|
||||
});
|
||||
|
||||
const admin_nsp = sio.of('/admin');
|
||||
|
||||
admin_nsp.on('connection', socketio_jwt.authorize(options))
|
||||
.on('authenticated', function (socket) {
|
||||
.on('authenticated', (socket) => {
|
||||
socket.emit('hi admin');
|
||||
});
|
||||
|
||||
@ -59,7 +59,7 @@ exports.start = function (callback) {
|
||||
enableDestroy(server);
|
||||
};
|
||||
|
||||
exports.stop = function (callback) {
|
||||
exports.stop = (callback) => {
|
||||
sio.close();
|
||||
try {
|
||||
server.destroy();
|
||||
|
@ -11,7 +11,7 @@ const enableDestroy = require('server-destroy');
|
||||
|
||||
let sio;
|
||||
|
||||
exports.start = function (options, callback) {
|
||||
exports.start = (options, callback) => {
|
||||
const SECRETS = {
|
||||
123: 'aaafoo super sercret',
|
||||
555: 'other'
|
||||
@ -23,7 +23,7 @@ exports.start = function (options, callback) {
|
||||
}
|
||||
|
||||
options = xtend({
|
||||
secret: function(request, decodedToken, callback) {
|
||||
secret: (request, decodedToken, callback) => {
|
||||
callback(null, SECRETS[decodedToken.id]);
|
||||
},
|
||||
timeout: 1000,
|
||||
@ -35,7 +35,7 @@ exports.start = function (options, callback) {
|
||||
sio = socketIo.listen(server);
|
||||
|
||||
app.use(bodyParser.json());
|
||||
app.post('/login', function (req, res) {
|
||||
app.post('/login', (req, res) => {
|
||||
const profile = {
|
||||
first_name: 'John',
|
||||
last_name: 'Doe',
|
||||
@ -51,21 +51,21 @@ exports.start = function (options, callback) {
|
||||
if (options.handshake) {
|
||||
sio.use(socketio_jwt.authorize(options));
|
||||
|
||||
sio.sockets.on('echo', function (m) {
|
||||
sio.sockets.on('echo', (m) => {
|
||||
sio.sockets.emit('echo-response', m);
|
||||
});
|
||||
} else {
|
||||
sio.sockets
|
||||
.on('connection', socketio_jwt.authorize(options))
|
||||
.on('authenticated', function (socket) {
|
||||
socket.on('echo', function (m) {
|
||||
.on('authenticated', (socket) => {
|
||||
socket.on('echo', (m) => {
|
||||
socket.emit('echo-response', m);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
server.__sockets = [];
|
||||
server.on('connection', function (c) {
|
||||
server.on('connection', (c) => {
|
||||
server.__sockets.push(c);
|
||||
});
|
||||
|
||||
@ -73,7 +73,7 @@ exports.start = function (options, callback) {
|
||||
enableDestroy(server);
|
||||
};
|
||||
|
||||
exports.stop = function (callback) {
|
||||
exports.stop = (callback) => {
|
||||
sio.close();
|
||||
try {
|
||||
server.destroy();
|
||||
|
Reference in New Issue
Block a user