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