mirror of
https://github.com/Thream/socketio-jwt.git
synced 2024-07-21 09:38:31 +02:00
add support for socket.io 1.0
This commit is contained in:
parent
0577d07b47
commit
e8380c10b8
10
README.md
10
README.md
@ -41,11 +41,19 @@ The previous approach uses a second roundtrip to send the jwt, there is a way yo
|
|||||||
var io = require("socket.io")(server);
|
var io = require("socket.io")(server);
|
||||||
var socketioJwt = require("socketio-jwt");
|
var socketioJwt = require("socketio-jwt");
|
||||||
|
|
||||||
// set authorization for socket.io
|
//// With socket.io < 1.0 ////
|
||||||
io.set('authorization', socketioJwt.authorize({
|
io.set('authorization', socketioJwt.authorize({
|
||||||
secret: 'your secret or public key',
|
secret: 'your secret or public key',
|
||||||
handshake: true
|
handshake: true
|
||||||
}));
|
}));
|
||||||
|
//////////////////////////////
|
||||||
|
|
||||||
|
//// With socket.io >= 1.0 ////
|
||||||
|
io.use(socketioJwt.authorize({
|
||||||
|
secret: 'your secret or public key',
|
||||||
|
handshake: true
|
||||||
|
}));
|
||||||
|
///////////////////////////////
|
||||||
|
|
||||||
io.on('connection', function (socket) {
|
io.on('connection', function (socket) {
|
||||||
console.log('hello! ', socket.handshake.decoded_token.name);
|
console.log('hello! ', socket.handshake.decoded_token.name);
|
||||||
|
19
lib/index.js
19
lib/index.js
@ -29,11 +29,19 @@ function noQsMethod(options) {
|
|||||||
function authorize(options, onConnection) {
|
function authorize(options, onConnection) {
|
||||||
var defaults = {
|
var defaults = {
|
||||||
success: function(data, accept){
|
success: function(data, accept){
|
||||||
|
if (data.request) {
|
||||||
|
accept();
|
||||||
|
} else {
|
||||||
accept(null, true);
|
accept(null, true);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
fail: function(error, data, accept){
|
fail: function(error, data, accept){
|
||||||
|
if (data.request) {
|
||||||
|
accept();
|
||||||
|
} else {
|
||||||
accept(null, false);
|
accept(null, false);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var auth = xtend(defaults, options);
|
var auth = xtend(defaults, options);
|
||||||
@ -45,8 +53,10 @@ function authorize(options, onConnection) {
|
|||||||
return function(data, accept){
|
return function(data, accept){
|
||||||
var token, error;
|
var token, error;
|
||||||
|
|
||||||
if (data.headers && data.headers.authorization) {
|
var authorization_header = ((data.request || data).headers || {}).authorization;
|
||||||
var parts = data.headers.authorization.split(' ');
|
|
||||||
|
if (authorization_header) {
|
||||||
|
var parts = authorization_header.split(' ');
|
||||||
if (parts.length == 2) {
|
if (parts.length == 2) {
|
||||||
var scheme = parts[0],
|
var scheme = parts[0],
|
||||||
credentials = parts[1];
|
credentials = parts[1];
|
||||||
@ -62,9 +72,8 @@ function authorize(options, onConnection) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.query.token) {
|
//get the token from query string
|
||||||
token = data.query.token;
|
token = (data.request || data).query.token;
|
||||||
}
|
|
||||||
|
|
||||||
if (!token) {
|
if (!token) {
|
||||||
error = new UnauthorizedError('credentials_required', {
|
error = new UnauthorizedError('credentials_required', {
|
||||||
|
Loading…
Reference in New Issue
Block a user