@thream/socketio-jwt
Advanced tools
Comparing version 2.1.1 to 2.2.0
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const axios_1 = __importDefault(require("axios")); | ||
const socket_io_client_1 = require("socket.io-client"); | ||
const fixture_1 = require("./fixture"); | ||
describe('authorize - with secret as string in options', () => { | ||
var _axios = _interopRequireDefault(require("axios")); | ||
var _socketIoClient = require("socket.io-client"); | ||
var _unauthorizedErrorJs = require("../UnauthorizedError.js"); | ||
var _indexJs = require("./fixture/index.js"); | ||
function _interopRequireDefault(obj) { | ||
return obj && obj.__esModule ? obj : { | ||
default: obj | ||
}; | ||
} | ||
describe('authorize - with secret as string in options', ()=>{ | ||
let token = ''; | ||
beforeEach((done) => { | ||
fixture_1.fixtureStart(async () => { | ||
const response = await axios_1.default.post('http://localhost:9000/login'); | ||
beforeEach((done)=>{ | ||
(0, _indexJs).fixtureStart(async ()=>{ | ||
const response = await _axios.default.post('http://localhost:9000/login'); | ||
token = response.data.token; | ||
}) | ||
.then(done) | ||
.catch((error) => { | ||
}).then(done).catch((error)=>{ | ||
done(error); | ||
}); | ||
}); | ||
afterEach((done) => { | ||
fixture_1.fixtureStop(done); | ||
afterEach((done)=>{ | ||
(0, _indexJs).fixtureStop(done); | ||
}); | ||
it('should emit error with no token provided', (done) => { | ||
const socket = socket_io_client_1.io('http://localhost:9000'); | ||
socket.on('connect_error', (err) => { | ||
expect(err.data.message).toEqual('no token provided'); | ||
expect(err.data.code).toEqual('credentials_required'); | ||
it('should emit error with no token provided', (done)=>{ | ||
const socket = (0, _socketIoClient).io('http://localhost:9000'); | ||
socket.on('connect_error', (error)=>{ | ||
expect((0, _unauthorizedErrorJs).isUnauthorizedError(error)).toBeTruthy(); | ||
if ((0, _unauthorizedErrorJs).isUnauthorizedError(error)) { | ||
expect(error.data.message).toEqual('no token provided'); | ||
expect(error.data.code).toEqual('credentials_required'); | ||
} | ||
socket.close(); | ||
@@ -33,9 +36,14 @@ done(); | ||
}); | ||
it('should emit error with bad token format', (done) => { | ||
const socket = socket_io_client_1.io('http://localhost:9000', { | ||
auth: { token: 'testing' } | ||
it('should emit error with bad token format', (done)=>{ | ||
const socket = (0, _socketIoClient).io('http://localhost:9000', { | ||
auth: { | ||
token: 'testing' | ||
} | ||
}); | ||
socket.on('connect_error', (err) => { | ||
expect(err.data.message).toEqual('Format is Authorization: Bearer [token]'); | ||
expect(err.data.code).toEqual('credentials_bad_format'); | ||
socket.on('connect_error', (error)=>{ | ||
expect((0, _unauthorizedErrorJs).isUnauthorizedError(error)).toBeTruthy(); | ||
if ((0, _unauthorizedErrorJs).isUnauthorizedError(error)) { | ||
expect(error.data.message).toEqual('Format is Authorization: Bearer [token]'); | ||
expect(error.data.code).toEqual('credentials_bad_format'); | ||
} | ||
socket.close(); | ||
@@ -45,9 +53,14 @@ done(); | ||
}); | ||
it('should emit error with unauthorized handshake', (done) => { | ||
const socket = socket_io_client_1.io('http://localhost:9000', { | ||
auth: { token: 'Bearer testing' } | ||
it('should emit error with unauthorized handshake', (done)=>{ | ||
const socket = (0, _socketIoClient).io('http://localhost:9000', { | ||
auth: { | ||
token: 'Bearer testing' | ||
} | ||
}); | ||
socket.on('connect_error', (err) => { | ||
expect(err.data.message).toEqual('Unauthorized: Token is missing or invalid Bearer'); | ||
expect(err.data.code).toEqual('invalid_token'); | ||
socket.on('connect_error', (error)=>{ | ||
expect((0, _unauthorizedErrorJs).isUnauthorizedError(error)).toBeTruthy(); | ||
if ((0, _unauthorizedErrorJs).isUnauthorizedError(error)) { | ||
expect(error.data.message).toEqual('Unauthorized: Token is missing or invalid Bearer'); | ||
expect(error.data.code).toEqual('invalid_token'); | ||
} | ||
socket.close(); | ||
@@ -57,38 +70,43 @@ done(); | ||
}); | ||
it('should connect the user', (done) => { | ||
const socket = socket_io_client_1.io('http://localhost:9000', { | ||
auth: { token: `Bearer ${token}` } | ||
it('should connect the user', (done)=>{ | ||
const socket = (0, _socketIoClient).io('http://localhost:9000', { | ||
auth: { | ||
token: `Bearer ${token}` | ||
} | ||
}); | ||
socket.on('connect', () => { | ||
socket.on('connect', ()=>{ | ||
socket.close(); | ||
done(); | ||
}); | ||
socket.on('connect_error', (err) => { | ||
done(err); | ||
socket.on('connect_error', (error)=>{ | ||
done(error); | ||
}); | ||
}); | ||
}); | ||
const secretCallback = async () => { | ||
const secretCallback = async ()=>{ | ||
return 'somesecret'; | ||
}; | ||
describe('authorize - with secret as callback in options', () => { | ||
describe('authorize - with secret as callback in options', ()=>{ | ||
let token = ''; | ||
beforeEach((done) => { | ||
fixture_1.fixtureStart(async () => { | ||
const response = await axios_1.default.post('http://localhost:9000/login'); | ||
beforeEach((done)=>{ | ||
(0, _indexJs).fixtureStart(async ()=>{ | ||
const response = await _axios.default.post('http://localhost:9000/login'); | ||
token = response.data.token; | ||
}, { secret: secretCallback }) | ||
.then(done) | ||
.catch((error) => { | ||
}, { | ||
secret: secretCallback | ||
}).then(done).catch((error)=>{ | ||
done(error); | ||
}); | ||
}); | ||
afterEach((done) => { | ||
fixture_1.fixtureStop(done); | ||
afterEach((done)=>{ | ||
(0, _indexJs).fixtureStop(done); | ||
}); | ||
it('should emit error with no token provided', (done) => { | ||
const socket = socket_io_client_1.io('http://localhost:9000'); | ||
socket.on('connect_error', (err) => { | ||
expect(err.data.message).toEqual('no token provided'); | ||
expect(err.data.code).toEqual('credentials_required'); | ||
it('should emit error with no token provided', (done)=>{ | ||
const socket = (0, _socketIoClient).io('http://localhost:9000'); | ||
socket.on('connect_error', (error)=>{ | ||
expect((0, _unauthorizedErrorJs).isUnauthorizedError(error)).toBeTruthy(); | ||
if ((0, _unauthorizedErrorJs).isUnauthorizedError(error)) { | ||
expect(error.data.message).toEqual('no token provided'); | ||
expect(error.data.code).toEqual('credentials_required'); | ||
} | ||
socket.close(); | ||
@@ -98,9 +116,14 @@ done(); | ||
}); | ||
it('should emit error with bad token format', (done) => { | ||
const socket = socket_io_client_1.io('http://localhost:9000', { | ||
auth: { token: 'testing' } | ||
it('should emit error with bad token format', (done)=>{ | ||
const socket = (0, _socketIoClient).io('http://localhost:9000', { | ||
auth: { | ||
token: 'testing' | ||
} | ||
}); | ||
socket.on('connect_error', (err) => { | ||
expect(err.data.message).toEqual('Format is Authorization: Bearer [token]'); | ||
expect(err.data.code).toEqual('credentials_bad_format'); | ||
socket.on('connect_error', (error)=>{ | ||
expect((0, _unauthorizedErrorJs).isUnauthorizedError(error)).toBeTruthy(); | ||
if ((0, _unauthorizedErrorJs).isUnauthorizedError(error)) { | ||
expect(error.data.message).toEqual('Format is Authorization: Bearer [token]'); | ||
expect(error.data.code).toEqual('credentials_bad_format'); | ||
} | ||
socket.close(); | ||
@@ -110,9 +133,14 @@ done(); | ||
}); | ||
it('should emit error with unauthorized handshake', (done) => { | ||
const socket = socket_io_client_1.io('http://localhost:9000', { | ||
auth: { token: 'Bearer testing' } | ||
it('should emit error with unauthorized handshake', (done)=>{ | ||
const socket = (0, _socketIoClient).io('http://localhost:9000', { | ||
auth: { | ||
token: 'Bearer testing' | ||
} | ||
}); | ||
socket.on('connect_error', (err) => { | ||
expect(err.data.message).toEqual('Unauthorized: Token is missing or invalid Bearer'); | ||
expect(err.data.code).toEqual('invalid_token'); | ||
socket.on('connect_error', (error)=>{ | ||
expect((0, _unauthorizedErrorJs).isUnauthorizedError(error)).toBeTruthy(); | ||
if ((0, _unauthorizedErrorJs).isUnauthorizedError(error)) { | ||
expect(error.data.message).toEqual('Unauthorized: Token is missing or invalid Bearer'); | ||
expect(error.data.code).toEqual('invalid_token'); | ||
} | ||
socket.close(); | ||
@@ -122,27 +150,29 @@ done(); | ||
}); | ||
it('should connect the user', (done) => { | ||
const socket = socket_io_client_1.io('http://localhost:9000', { | ||
auth: { token: `Bearer ${token}` } | ||
it('should connect the user', (done)=>{ | ||
const socket = (0, _socketIoClient).io('http://localhost:9000', { | ||
auth: { | ||
token: `Bearer ${token}` | ||
} | ||
}); | ||
socket.on('connect', () => { | ||
socket.on('connect', ()=>{ | ||
socket.close(); | ||
done(); | ||
}); | ||
socket.on('connect_error', (err) => { | ||
done(err); | ||
socket.on('connect_error', (error)=>{ | ||
done(error); | ||
}); | ||
}); | ||
}); | ||
describe('authorize - with onAuthentication callback in options', () => { | ||
describe('authorize - with onAuthentication callback in options', ()=>{ | ||
let token = ''; | ||
let wrongToken = ''; | ||
beforeEach((done) => { | ||
fixture_1.fixtureStart(async () => { | ||
const response = await axios_1.default.post('http://localhost:9000/login'); | ||
beforeEach((done)=>{ | ||
(0, _indexJs).fixtureStart(async ()=>{ | ||
const response = await _axios.default.post('http://localhost:9000/login'); | ||
token = response.data.token; | ||
const responseWrong = await axios_1.default.post('http://localhost:9000/login-wrong'); | ||
const responseWrong = await _axios.default.post('http://localhost:9000/login-wrong'); | ||
wrongToken = responseWrong.data.token; | ||
}, { | ||
secret: secretCallback, | ||
onAuthentication: (decodedToken) => { | ||
onAuthentication: (decodedToken)=>{ | ||
if (!decodedToken.checkField) { | ||
@@ -155,16 +185,17 @@ throw new Error('Check Field validation failed'); | ||
} | ||
}) | ||
.then(done) | ||
.catch((error) => { | ||
}).then(done).catch((error)=>{ | ||
done(error); | ||
}); | ||
}); | ||
afterEach((done) => { | ||
fixture_1.fixtureStop(done); | ||
afterEach((done)=>{ | ||
(0, _indexJs).fixtureStop(done); | ||
}); | ||
it('should emit error with no token provided', (done) => { | ||
const socket = socket_io_client_1.io('http://localhost:9000'); | ||
socket.on('connect_error', (err) => { | ||
expect(err.data.message).toEqual('no token provided'); | ||
expect(err.data.code).toEqual('credentials_required'); | ||
it('should emit error with no token provided', (done)=>{ | ||
const socket = (0, _socketIoClient).io('http://localhost:9000'); | ||
socket.on('connect_error', (error)=>{ | ||
expect((0, _unauthorizedErrorJs).isUnauthorizedError(error)).toBeTruthy(); | ||
if ((0, _unauthorizedErrorJs).isUnauthorizedError(error)) { | ||
expect(error.data.message).toEqual('no token provided'); | ||
expect(error.data.code).toEqual('credentials_required'); | ||
} | ||
socket.close(); | ||
@@ -174,9 +205,14 @@ done(); | ||
}); | ||
it('should emit error with bad token format', (done) => { | ||
const socket = socket_io_client_1.io('http://localhost:9000', { | ||
auth: { token: 'testing' } | ||
it('should emit error with bad token format', (done)=>{ | ||
const socket = (0, _socketIoClient).io('http://localhost:9000', { | ||
auth: { | ||
token: 'testing' | ||
} | ||
}); | ||
socket.on('connect_error', (err) => { | ||
expect(err.data.message).toEqual('Format is Authorization: Bearer [token]'); | ||
expect(err.data.code).toEqual('credentials_bad_format'); | ||
socket.on('connect_error', (error)=>{ | ||
expect((0, _unauthorizedErrorJs).isUnauthorizedError(error)).toBeTruthy(); | ||
if ((0, _unauthorizedErrorJs).isUnauthorizedError(error)) { | ||
expect(error.data.message).toEqual('Format is Authorization: Bearer [token]'); | ||
expect(error.data.code).toEqual('credentials_bad_format'); | ||
} | ||
socket.close(); | ||
@@ -186,9 +222,14 @@ done(); | ||
}); | ||
it('should emit error with unauthorized handshake', (done) => { | ||
const socket = socket_io_client_1.io('http://localhost:9000', { | ||
auth: { token: 'Bearer testing' } | ||
it('should emit error with unauthorized handshake', (done)=>{ | ||
const socket = (0, _socketIoClient).io('http://localhost:9000', { | ||
auth: { | ||
token: 'Bearer testing' | ||
} | ||
}); | ||
socket.on('connect_error', (err) => { | ||
expect(err.data.message).toEqual('Unauthorized: Token is missing or invalid Bearer'); | ||
expect(err.data.code).toEqual('invalid_token'); | ||
socket.on('connect_error', (error)=>{ | ||
expect((0, _unauthorizedErrorJs).isUnauthorizedError(error)).toBeTruthy(); | ||
if ((0, _unauthorizedErrorJs).isUnauthorizedError(error)) { | ||
expect(error.data.message).toEqual('Unauthorized: Token is missing or invalid Bearer'); | ||
expect(error.data.code).toEqual('invalid_token'); | ||
} | ||
socket.close(); | ||
@@ -198,7 +239,9 @@ done(); | ||
}); | ||
it('should connect the user', (done) => { | ||
const socket = socket_io_client_1.io('http://localhost:9000', { | ||
auth: { token: `Bearer ${token}` } | ||
it('should connect the user', (done)=>{ | ||
const socket = (0, _socketIoClient).io('http://localhost:9000', { | ||
auth: { | ||
token: `Bearer ${token}` | ||
} | ||
}); | ||
socket.on('connect', () => { | ||
socket.on('connect', ()=>{ | ||
socket.close(); | ||
@@ -208,11 +251,13 @@ done(); | ||
}); | ||
it('should contain user property', (done) => { | ||
const socketServer = fixture_1.getSocket(); | ||
socketServer?.on('connection', (client) => { | ||
it('should contain user property', (done)=>{ | ||
const socketServer = (0, _indexJs).getSocket(); | ||
socketServer?.on('connection', (client)=>{ | ||
expect(client.user.email).toEqual('john@doe.com'); | ||
}); | ||
const socket = socket_io_client_1.io('http://localhost:9000', { | ||
auth: { token: `Bearer ${token}` } | ||
const socket = (0, _socketIoClient).io('http://localhost:9000', { | ||
auth: { | ||
token: `Bearer ${token}` | ||
} | ||
}); | ||
socket.on('connect', () => { | ||
socket.on('connect', ()=>{ | ||
socket.close(); | ||
@@ -222,13 +267,14 @@ done(); | ||
}); | ||
it('should emit error when user validation fails', (done) => { | ||
const socket = socket_io_client_1.io('http://localhost:9000', { | ||
auth: { token: `Bearer ${wrongToken}` } | ||
it('should emit error when user validation fails', (done)=>{ | ||
const socket = (0, _socketIoClient).io('http://localhost:9000', { | ||
auth: { | ||
token: `Bearer ${wrongToken}` | ||
} | ||
}); | ||
socket.on('connect_error', (err) => { | ||
socket.on('connect_error', (err)=>{ | ||
try { | ||
expect(err.message).toEqual('Check Field validation failed'); | ||
} | ||
catch (err) { | ||
} catch (err1) { | ||
socket.close(); | ||
done(err); | ||
done(err1); | ||
} | ||
@@ -235,0 +281,0 @@ socket.close(); |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.getSocket = exports.fixtureStop = exports.fixtureStart = void 0; | ||
const express_1 = __importDefault(require("express")); | ||
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken")); | ||
const socket_io_1 = require("socket.io"); | ||
const server_destroy_1 = __importDefault(require("server-destroy")); | ||
const index_1 = require("../../index"); | ||
var _express = _interopRequireDefault(require("express")); | ||
var _jsonwebtoken = _interopRequireDefault(require("jsonwebtoken")); | ||
var _socketIo = require("socket.io"); | ||
var _serverDestroy = _interopRequireDefault(require("server-destroy")); | ||
var _indexJs = require("../../index.js"); | ||
function _interopRequireDefault(obj) { | ||
return obj && obj.__esModule ? obj : { | ||
default: obj | ||
}; | ||
} | ||
const socket = { | ||
io: null, | ||
init(httpServer) { | ||
socket.io = new socket_io_1.Server(httpServer); | ||
init (httpServer) { | ||
socket.io = new _socketIo.Server(httpServer); | ||
} | ||
}; | ||
let server = null; | ||
const fixtureStart = async (done, options = { secret: 'super secret' }) => { | ||
const fixtureStart = async (done, options = { | ||
secret: 'super secret' | ||
})=>{ | ||
const profile = { | ||
@@ -28,39 +34,46 @@ email: 'john@doe.com', | ||
keySecret = options.secret; | ||
} else { | ||
keySecret = await options.secret({ | ||
header: { | ||
alg: 'HS256' | ||
}, | ||
payload: profile | ||
}); | ||
} | ||
else { | ||
keySecret = await options.secret({ header: { alg: 'HS256' }, payload: profile }); | ||
} | ||
const app = express_1.default(); | ||
app.use(express_1.default.json()); | ||
app.post('/login', (_req, res) => { | ||
const token = jsonwebtoken_1.default.sign(profile, keySecret, { | ||
const app = (0, _express).default(); | ||
app.use(_express.default.json()); | ||
app.post('/login', (_req, res)=>{ | ||
const token = _jsonwebtoken.default.sign(profile, keySecret, { | ||
expiresIn: 60 * 60 * 5 | ||
}); | ||
return res.json({ token }); | ||
return res.json({ | ||
token | ||
}); | ||
}); | ||
app.post('/login-wrong', (_req, res) => { | ||
app.post('/login-wrong', (_req, res)=>{ | ||
profile.checkField = false; | ||
const token = jsonwebtoken_1.default.sign(profile, keySecret, { | ||
const token = _jsonwebtoken.default.sign(profile, keySecret, { | ||
expiresIn: 60 * 60 * 5 | ||
}); | ||
return res.json({ token }); | ||
return res.json({ | ||
token | ||
}); | ||
}); | ||
server = app.listen(9000, done); | ||
socket.init(server); | ||
socket.io?.use(index_1.authorize(options)); | ||
server_destroy_1.default(server); | ||
socket.io?.use((0, _indexJs).authorize(options)); | ||
(0, _serverDestroy).default(server); | ||
}; | ||
exports.fixtureStart = fixtureStart; | ||
const fixtureStop = (callback) => { | ||
const fixtureStop = (callback)=>{ | ||
socket.io?.close(); | ||
try { | ||
server?.destroy(); | ||
} | ||
catch { } | ||
} catch {} | ||
callback(); | ||
}; | ||
exports.fixtureStop = fixtureStop; | ||
const getSocket = () => { | ||
const getSocket = ()=>{ | ||
return socket.io; | ||
}; | ||
exports.getSocket = getSocket; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.authorize = void 0; | ||
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken")); | ||
const UnauthorizedError_1 = require("./UnauthorizedError"); | ||
const authorize = (options) => { | ||
const { secret, algorithms = ['HS256'], onAuthentication } = options; | ||
return async (socket, next) => { | ||
var _jsonwebtoken = _interopRequireDefault(require("jsonwebtoken")); | ||
var _unauthorizedErrorJs = require("./UnauthorizedError.js"); | ||
function _interopRequireDefault(obj) { | ||
return obj && obj.__esModule ? obj : { | ||
default: obj | ||
}; | ||
} | ||
const authorize = (options)=>{ | ||
const { secret , algorithms =[ | ||
'HS256' | ||
] , onAuthentication } = options; | ||
return async (socket, next)=>{ | ||
let encodedToken = null; | ||
const { token } = socket.handshake.auth; | ||
const { token } = socket.handshake.auth; | ||
if (token != null) { | ||
const tokenSplitted = token.split(' '); | ||
if (tokenSplitted.length !== 2 || tokenSplitted[0] !== 'Bearer') { | ||
return next(new UnauthorizedError_1.UnauthorizedError('credentials_bad_format', { | ||
return next(new _unauthorizedErrorJs.UnauthorizedError('credentials_bad_format', { | ||
message: 'Format is Authorization: Bearer [token]' | ||
@@ -24,3 +30,3 @@ })); | ||
if (encodedToken == null) { | ||
return next(new UnauthorizedError_1.UnauthorizedError('credentials_required', { | ||
return next(new _unauthorizedErrorJs.UnauthorizedError('credentials_required', { | ||
message: 'no token provided' | ||
@@ -34,12 +40,14 @@ })); | ||
keySecret = secret; | ||
} | ||
else { | ||
const completeDecodedToken = jsonwebtoken_1.default.decode(encodedToken, { complete: true }); | ||
} else { | ||
const completeDecodedToken = _jsonwebtoken.default.decode(encodedToken, { | ||
complete: true | ||
}); | ||
keySecret = await secret(completeDecodedToken); | ||
} | ||
try { | ||
decodedToken = jsonwebtoken_1.default.verify(encodedToken, keySecret, { algorithms }); | ||
} | ||
catch { | ||
return next(new UnauthorizedError_1.UnauthorizedError('invalid_token', { | ||
decodedToken = _jsonwebtoken.default.verify(encodedToken, keySecret, { | ||
algorithms | ||
}); | ||
} catch { | ||
return next(new _unauthorizedErrorJs.UnauthorizedError('invalid_token', { | ||
message: 'Unauthorized: Token is missing or invalid Bearer' | ||
@@ -52,6 +60,5 @@ })); | ||
socket.user = await onAuthentication(decodedToken); | ||
} catch (error) { | ||
return next(error); | ||
} | ||
catch (err) { | ||
return next(err); | ||
} | ||
} | ||
@@ -58,0 +65,0 @@ return next(); |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./authorize"), exports); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _exportNames = {}; | ||
var _authorizeJs = _interopRequireWildcard(require("./authorize.js")); | ||
Object.keys(_authorizeJs).forEach(function(key) { | ||
if (key === "default" || key === "__esModule") return; | ||
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return; | ||
if (key in exports && exports[key] === _authorizeJs[key]) return; | ||
Object.defineProperty(exports, key, { | ||
enumerable: true, | ||
get: function() { | ||
return _authorizeJs[key]; | ||
} | ||
}); | ||
}); | ||
var _unauthorizedErrorJs = _interopRequireWildcard(require("./UnauthorizedError.js")); | ||
Object.keys(_unauthorizedErrorJs).forEach(function(key) { | ||
if (key === "default" || key === "__esModule") return; | ||
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return; | ||
if (key in exports && exports[key] === _unauthorizedErrorJs[key]) return; | ||
Object.defineProperty(exports, key, { | ||
enumerable: true, | ||
get: function() { | ||
return _unauthorizedErrorJs[key]; | ||
} | ||
}); | ||
}); | ||
function _interopRequireWildcard(obj) { | ||
if (obj && obj.__esModule) { | ||
return obj; | ||
} else { | ||
var newObj = {}; | ||
if (obj != null) { | ||
for(var key in obj){ | ||
if (Object.prototype.hasOwnProperty.call(obj, key)) { | ||
var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; | ||
if (desc.get || desc.set) { | ||
Object.defineProperty(newObj, key, desc); | ||
} else { | ||
newObj[key] = obj[key]; | ||
} | ||
} | ||
} | ||
} | ||
newObj.default = obj; | ||
return newObj; | ||
} | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.UnauthorizedError = void 0; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.isUnauthorizedError = void 0; | ||
class UnauthorizedError extends Error { | ||
inner; | ||
data; | ||
constructor(code, error) { | ||
constructor(code, error){ | ||
super(error.message); | ||
@@ -20,1 +20,5 @@ this.message = error.message; | ||
exports.UnauthorizedError = UnauthorizedError; | ||
const isUnauthorizedError = (error)=>{ | ||
return error.data.type === 'UnauthorizedError'; | ||
}; | ||
exports.isUnauthorizedError = isUnauthorizedError; |
{ | ||
"name": "@thream/socketio-jwt", | ||
"version": "2.1.1", | ||
"version": "2.2.0", | ||
"public": true, | ||
"description": "Authenticate socket.io incoming connections with JWTs.", | ||
@@ -14,2 +15,5 @@ "license": "MIT", | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"keywords": [ | ||
@@ -23,3 +27,3 @@ "socket", | ||
"type": "git", | ||
"url": "git+https://github.com/Thream/socketio-jwt" | ||
"url": "https://github.com/Thream/socketio-jwt" | ||
}, | ||
@@ -30,22 +34,12 @@ "bugs": { | ||
"homepage": "https://github.com/Thream/socketio-jwt#readme", | ||
"ts-standard": { | ||
"ignore": [ | ||
"build", | ||
"coverage", | ||
"node_modules" | ||
], | ||
"envs": [ | ||
"node", | ||
"jest" | ||
], | ||
"report": "stylish" | ||
}, | ||
"scripts": { | ||
"build": "rimraf ./build && tsc", | ||
"build": "rimraf ./build && swc ./src --out-dir ./build && tsc", | ||
"lint:commit": "commitlint", | ||
"lint:editorconfig": "editorconfig-checker", | ||
"lint:markdown": "markdownlint '**/*.md' --dot --ignore node_modules", | ||
"lint:typescript": "ts-standard", | ||
"lint:markdown": "markdownlint \"**/*.md\" --dot --ignore-path \".gitignore\"", | ||
"lint:typescript": "eslint \"**/*.{js,jsx,ts,tsx}\" --ignore-path \".gitignore\"", | ||
"lint:prettier": "prettier \".\" --check --ignore-path \".gitignore\"", | ||
"lint:staged": "lint-staged", | ||
"test": "jest", | ||
"release": "semantic-release", | ||
"test": "jest", | ||
"_postinstall": "husky install", | ||
@@ -62,25 +56,38 @@ "prepublishOnly": "pinst --disable", | ||
"devDependencies": { | ||
"@commitlint/cli": "12.1.4", | ||
"@commitlint/config-conventional": "12.1.4", | ||
"@commitlint/cli": "16.2.1", | ||
"@commitlint/config-conventional": "16.2.1", | ||
"@swc/cli": "0.1.55", | ||
"@swc/core": "1.2.141", | ||
"@swc/jest": "0.2.17", | ||
"@types/express": "4.17.13", | ||
"@types/jest": "26.0.24", | ||
"@types/jsonwebtoken": "8.5.4", | ||
"@types/node": "16.4.1", | ||
"@types/jest": "27.4.0", | ||
"@types/jsonwebtoken": "8.5.8", | ||
"@types/node": "17.0.18", | ||
"@types/server-destroy": "1.0.1", | ||
"axios": "0.21.1", | ||
"@typescript-eslint/eslint-plugin": "4.32.0", | ||
"axios": "0.26.0", | ||
"editorconfig-checker": "4.0.2", | ||
"express": "4.17.1", | ||
"husky": "7.0.1", | ||
"jest": "27.0.6", | ||
"markdownlint-cli": "0.28.1", | ||
"eslint": "7.32.0", | ||
"eslint-config-prettier": "8.3.0", | ||
"eslint-config-standard-with-typescript": "21.0.1", | ||
"eslint-plugin-import": "2.25.4", | ||
"eslint-plugin-node": "11.1.0", | ||
"eslint-plugin-prettier": "4.0.0", | ||
"eslint-plugin-promise": "5.2.0", | ||
"eslint-plugin-unicorn": "40.1.0", | ||
"express": "4.17.3", | ||
"husky": "7.0.4", | ||
"jest": "27.5.1", | ||
"jest-ts-webcompat-resolver": "1.0.0", | ||
"lint-staged": "12.3.4", | ||
"markdownlint-cli": "0.31.1", | ||
"pinst": "2.1.6", | ||
"rimraf": "3.0.2", | ||
"semantic-release": "17.4.4", | ||
"semantic-release": "19.0.2", | ||
"server-destroy": "1.0.1", | ||
"socket.io": "4.1.3", | ||
"socket.io-client": "4.1.3", | ||
"ts-jest": "27.0.4", | ||
"ts-standard": "10.0.0", | ||
"typescript": "4.3.5" | ||
"prettier": "2.5.1", | ||
"socket.io": "4.4.1", | ||
"socket.io-client": "4.4.1", | ||
"typescript": "4.5.5" | ||
} | ||
} |
@@ -11,3 +11,2 @@ <h1 align="center">Thream/socketio-jwt</h1> | ||
<a href="./CODE_OF_CONDUCT.md"><img src="https://img.shields.io/badge/Contributor%20Covenant-v2.0%20adopted-ff69b4.svg" alt="Contributor Covenant" /></a> | ||
<a href="https://dependabot.com/"><img src="https://badgen.net/github/dependabot/Thream/socketio-jwt?icon=dependabot" alt="Dependabot badge" /></a> | ||
<br/> | ||
@@ -34,2 +33,4 @@ <a href="https://github.com/Thream/socketio-jwt/actions/workflows/build.yml"><img src="https://github.com/Thream/socketio-jwt/actions/workflows/build.yml/badge.svg?branch=develop" /></a> | ||
**Note:** It is a package that is recommended to use/install on both the client and server sides. | ||
```sh | ||
@@ -107,3 +108,3 @@ npm install --save @thream/socketio-jwt | ||
secret: 'your secret or public key', | ||
onAuthentication: async decodedToken => { | ||
onAuthentication: async (decodedToken) => { | ||
// return the object that you want to add to the user property | ||
@@ -134,2 +135,3 @@ // or throw an error if the token is unauthorized | ||
import { io } from 'socket.io-client' | ||
import { isUnauthorizedError } from '@thream/socketio-jwt' | ||
@@ -143,3 +145,3 @@ // Require Bearer Token | ||
socket.on('connect_error', (error) => { | ||
if (error.data.type === 'UnauthorizedError') { | ||
if (isUnauthorizedError(error)) { | ||
console.log('User token has expired') | ||
@@ -146,0 +148,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26264
489
161
35
8
1