@thream/socketio-jwt
Advanced tools
Comparing version 2.2.1 to 3.0.0
@@ -1,1 +0,1 @@ | ||
export {}; | ||
export declare const api: import("axios").AxiosInstance; |
@@ -1,82 +0,11 @@ | ||
"use strict"; | ||
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)=>{ | ||
(0, _indexJs).fixtureStart(async ()=>{ | ||
const response = await _axios.default.post('http://localhost:9000/login'); | ||
token = response.data.token; | ||
}).then(done).catch((error)=>{ | ||
done(error); | ||
}); | ||
}); | ||
afterEach((done)=>{ | ||
(0, _indexJs).fixtureStop(done); | ||
}); | ||
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(); | ||
done(); | ||
}); | ||
}); | ||
it('should emit error with bad token format', (done)=>{ | ||
const socket = (0, _socketIoClient).io('http://localhost:9000', { | ||
auth: { | ||
token: 'testing' | ||
} | ||
}); | ||
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(); | ||
done(); | ||
}); | ||
}); | ||
it('should emit error with unauthorized handshake', (done)=>{ | ||
const socket = (0, _socketIoClient).io('http://localhost:9000', { | ||
auth: { | ||
token: 'Bearer testing' | ||
} | ||
}); | ||
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(); | ||
done(); | ||
}); | ||
}); | ||
it('should connect the user', (done)=>{ | ||
const socket = (0, _socketIoClient).io('http://localhost:9000', { | ||
auth: { | ||
token: `Bearer ${token}` | ||
} | ||
}); | ||
socket.on('connect', ()=>{ | ||
socket.close(); | ||
done(); | ||
}); | ||
socket.on('connect_error', (error)=>{ | ||
done(error); | ||
}); | ||
}); | ||
import tap from 'tap'; | ||
import axios from 'axios'; | ||
import { io } from 'socket.io-client'; | ||
import { isUnauthorizedError } from '../UnauthorizedError.js'; | ||
import { API_URL, fixtureStart, fixtureStop, getSocket, basicProfile } from './fixture/index.js'; | ||
export const api = axios.create({ | ||
baseURL: API_URL, | ||
headers: { | ||
'Content-Type': 'application/json' | ||
} | ||
}); | ||
@@ -86,189 +15,293 @@ const secretCallback = async ()=>{ | ||
}; | ||
describe('authorize - with secret as callback in options', ()=>{ | ||
let token = ''; | ||
beforeEach((done)=>{ | ||
(0, _indexJs).fixtureStart(async ()=>{ | ||
const response = await _axios.default.post('http://localhost:9000/login'); | ||
await tap.test('authorize', async (t1)=>{ | ||
await t1.test('with secret as string in options', async (t2)=>{ | ||
let token = ''; | ||
let socket = null; | ||
t2.beforeEach(async ()=>{ | ||
await fixtureStart(); | ||
const response = await api.post('/login', {}); | ||
token = response.data.token; | ||
}, { | ||
secret: secretCallback | ||
}).then(done).catch((error)=>{ | ||
done(error); | ||
}); | ||
}); | ||
afterEach((done)=>{ | ||
(0, _indexJs).fixtureStop(done); | ||
}); | ||
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(); | ||
done(); | ||
t2.afterEach(async ()=>{ | ||
socket?.disconnect(); | ||
await fixtureStop(); | ||
}); | ||
}); | ||
it('should emit error with bad token format', (done)=>{ | ||
const socket = (0, _socketIoClient).io('http://localhost:9000', { | ||
auth: { | ||
token: 'testing' | ||
} | ||
await t2.test('should emit error with no token provided', (t)=>{ | ||
t.plan(4); | ||
socket = io(API_URL); | ||
socket.on('connect_error', async (error)=>{ | ||
t.equal(isUnauthorizedError(error), true); | ||
if (isUnauthorizedError(error)) { | ||
t.equal(error.data.message, 'no token provided'); | ||
t.equal(error.data.code, 'credentials_required'); | ||
} | ||
t.pass(); | ||
}); | ||
socket.on('connect', async ()=>{ | ||
t.fail(); | ||
}); | ||
}); | ||
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(); | ||
done(); | ||
await t2.test('should emit error with bad token format', (t)=>{ | ||
t.plan(4); | ||
socket = io(API_URL, { | ||
auth: { | ||
token: 'testing' | ||
} | ||
}); | ||
socket.on('connect_error', async (error)=>{ | ||
t.equal(isUnauthorizedError(error), true); | ||
if (isUnauthorizedError(error)) { | ||
t.equal(error.data.message, 'Format is Authorization: Bearer [token]'); | ||
t.equal(error.data.code, 'credentials_bad_format'); | ||
} | ||
t.pass(); | ||
}); | ||
socket.on('connect', async ()=>{ | ||
t.fail(); | ||
}); | ||
}); | ||
}); | ||
it('should emit error with unauthorized handshake', (done)=>{ | ||
const socket = (0, _socketIoClient).io('http://localhost:9000', { | ||
auth: { | ||
token: 'Bearer testing' | ||
} | ||
await t2.test('should emit error with unauthorized handshake', (t)=>{ | ||
t.plan(4); | ||
socket = io(API_URL, { | ||
auth: { | ||
token: 'Bearer testing' | ||
} | ||
}); | ||
socket.on('connect_error', async (error)=>{ | ||
t.equal(isUnauthorizedError(error), true); | ||
if (isUnauthorizedError(error)) { | ||
t.equal(error.data.message, 'Unauthorized: Token is missing or invalid Bearer'); | ||
t.equal(error.data.code, 'invalid_token'); | ||
} | ||
t.pass(); | ||
}); | ||
socket.on('connect', async ()=>{ | ||
t.fail(); | ||
}); | ||
}); | ||
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(); | ||
done(); | ||
await t2.test('should connect the user', (t)=>{ | ||
t.plan(1); | ||
socket = io(API_URL, { | ||
auth: { | ||
token: `Bearer ${token}` | ||
} | ||
}); | ||
socket.on('connect', async ()=>{ | ||
t.pass(); | ||
}); | ||
socket.on('connect_error', async (error)=>{ | ||
t.fail(error.message); | ||
}); | ||
}); | ||
}); | ||
it('should connect the user', (done)=>{ | ||
const socket = (0, _socketIoClient).io('http://localhost:9000', { | ||
auth: { | ||
token: `Bearer ${token}` | ||
} | ||
await t1.test('with secret as callback in options', async (t3)=>{ | ||
let token = ''; | ||
let socket = null; | ||
t3.beforeEach(async ()=>{ | ||
await fixtureStart({ | ||
secret: secretCallback | ||
}); | ||
const response = await api.post('/login', {}); | ||
token = response.data.token; | ||
}); | ||
socket.on('connect', ()=>{ | ||
socket.close(); | ||
done(); | ||
t3.afterEach(async ()=>{ | ||
socket?.disconnect(); | ||
await fixtureStop(); | ||
}); | ||
socket.on('connect_error', (error)=>{ | ||
done(error); | ||
await t3.test('should emit error with no token provided', (t)=>{ | ||
t.plan(4); | ||
socket = io(API_URL); | ||
socket.on('connect_error', async (error)=>{ | ||
t.equal(isUnauthorizedError(error), true); | ||
if (isUnauthorizedError(error)) { | ||
t.equal(error.data.message, 'no token provided'); | ||
t.equal(error.data.code, 'credentials_required'); | ||
} | ||
t.pass(); | ||
}); | ||
socket.on('connect', async ()=>{ | ||
t.fail(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('authorize - with onAuthentication callback in options', ()=>{ | ||
let token = ''; | ||
let wrongToken = ''; | ||
beforeEach((done)=>{ | ||
(0, _indexJs).fixtureStart(async ()=>{ | ||
const response = await _axios.default.post('http://localhost:9000/login'); | ||
token = response.data.token; | ||
const responseWrong = await _axios.default.post('http://localhost:9000/login-wrong'); | ||
wrongToken = responseWrong.data.token; | ||
}, { | ||
secret: secretCallback, | ||
onAuthentication: (decodedToken)=>{ | ||
if (!decodedToken.checkField) { | ||
throw new Error('Check Field validation failed'); | ||
await t3.test('should emit error with bad token format', (t)=>{ | ||
t.plan(4); | ||
socket = io(API_URL, { | ||
auth: { | ||
token: 'testing' | ||
} | ||
return { | ||
email: decodedToken.email | ||
}; | ||
} | ||
}).then(done).catch((error)=>{ | ||
done(error); | ||
}); | ||
socket.on('connect_error', async (error)=>{ | ||
t.equal(isUnauthorizedError(error), true); | ||
if (isUnauthorizedError(error)) { | ||
t.equal(error.data.message, 'Format is Authorization: Bearer [token]'); | ||
t.equal(error.data.code, 'credentials_bad_format'); | ||
} | ||
t.pass(); | ||
}); | ||
socket.on('connect', async ()=>{ | ||
t.fail(); | ||
}); | ||
}); | ||
}); | ||
afterEach((done)=>{ | ||
(0, _indexJs).fixtureStop(done); | ||
}); | ||
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(); | ||
done(); | ||
await t3.test('should emit error with unauthorized handshake', (t)=>{ | ||
t.plan(4); | ||
socket = io(API_URL, { | ||
auth: { | ||
token: 'Bearer testing' | ||
} | ||
}); | ||
socket.on('connect_error', async (error)=>{ | ||
t.equal(isUnauthorizedError(error), true); | ||
if (isUnauthorizedError(error)) { | ||
t.equal(error.data.message, 'Unauthorized: Token is missing or invalid Bearer'); | ||
t.equal(error.data.code, 'invalid_token'); | ||
} | ||
t.pass(); | ||
}); | ||
socket.on('connect', async ()=>{ | ||
t.fail(); | ||
}); | ||
}); | ||
}); | ||
it('should emit error with bad token format', (done)=>{ | ||
const socket = (0, _socketIoClient).io('http://localhost:9000', { | ||
auth: { | ||
token: 'testing' | ||
} | ||
await t3.test('should connect the user', (t)=>{ | ||
t.plan(1); | ||
socket = io(API_URL, { | ||
auth: { | ||
token: `Bearer ${token}` | ||
} | ||
}); | ||
socket.on('connect', async ()=>{ | ||
t.pass(); | ||
}); | ||
socket.on('connect_error', async (error)=>{ | ||
t.fail(error.message); | ||
}); | ||
}); | ||
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(); | ||
done(); | ||
}); | ||
}); | ||
it('should emit error with unauthorized handshake', (done)=>{ | ||
const socket = (0, _socketIoClient).io('http://localhost:9000', { | ||
auth: { | ||
token: 'Bearer testing' | ||
} | ||
await t1.test('with onAuthentication callback in options', async (t4)=>{ | ||
let token = ''; | ||
let wrongToken = ''; | ||
let socket = null; | ||
t4.beforeEach(async ()=>{ | ||
await fixtureStart({ | ||
secret: secretCallback, | ||
onAuthentication: (decodedToken)=>{ | ||
if (!decodedToken.checkField) { | ||
throw new Error('Check Field validation failed'); | ||
} | ||
return { | ||
email: decodedToken.email | ||
}; | ||
} | ||
}); | ||
const response = await api.post('/login', {}); | ||
token = response.data.token; | ||
const responseWrong = await api.post('/login-wrong', {}); | ||
wrongToken = responseWrong.data.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(); | ||
done(); | ||
t4.afterEach(async ()=>{ | ||
socket?.disconnect(); | ||
await fixtureStop(); | ||
}); | ||
}); | ||
it('should connect the user', (done)=>{ | ||
const socket = (0, _socketIoClient).io('http://localhost:9000', { | ||
auth: { | ||
token: `Bearer ${token}` | ||
} | ||
await t4.test('should emit error with no token provided', (t)=>{ | ||
t.plan(4); | ||
socket = io(API_URL); | ||
socket.on('connect_error', async (error)=>{ | ||
t.equal(isUnauthorizedError(error), true); | ||
if (isUnauthorizedError(error)) { | ||
t.equal(error.data.message, 'no token provided'); | ||
t.equal(error.data.code, 'credentials_required'); | ||
} | ||
t.pass(); | ||
}); | ||
socket.on('connect', async ()=>{ | ||
t.fail(); | ||
}); | ||
}); | ||
socket.on('connect', ()=>{ | ||
socket.close(); | ||
done(); | ||
await t4.test('should emit error with bad token format', (t)=>{ | ||
t.plan(4); | ||
socket = io(API_URL, { | ||
auth: { | ||
token: 'testing' | ||
} | ||
}); | ||
socket.on('connect_error', async (error)=>{ | ||
t.equal(isUnauthorizedError(error), true); | ||
if (isUnauthorizedError(error)) { | ||
t.equal(error.data.message, 'Format is Authorization: Bearer [token]'); | ||
t.equal(error.data.code, 'credentials_bad_format'); | ||
} | ||
t.pass(); | ||
}); | ||
socket.on('connect', async ()=>{ | ||
t.fail(); | ||
}); | ||
}); | ||
}); | ||
it('should contain user property', (done)=>{ | ||
const socketServer = (0, _indexJs).getSocket(); | ||
socketServer?.on('connection', (client)=>{ | ||
expect(client.user.email).toEqual('john@doe.com'); | ||
await t4.test('should emit error with unauthorized handshake', (t)=>{ | ||
t.plan(4); | ||
socket = io(API_URL, { | ||
auth: { | ||
token: 'Bearer testing' | ||
} | ||
}); | ||
socket.on('connect_error', async (error)=>{ | ||
t.equal(isUnauthorizedError(error), true); | ||
if (isUnauthorizedError(error)) { | ||
t.equal(error.data.message, 'Unauthorized: Token is missing or invalid Bearer'); | ||
t.equal(error.data.code, 'invalid_token'); | ||
} | ||
t.pass(); | ||
}); | ||
socket.on('connect', async ()=>{ | ||
t.fail(); | ||
}); | ||
}); | ||
const socket = (0, _socketIoClient).io('http://localhost:9000', { | ||
auth: { | ||
token: `Bearer ${token}` | ||
} | ||
await t4.test('should connect the user', (t)=>{ | ||
t.plan(1); | ||
socket = io(API_URL, { | ||
auth: { | ||
token: `Bearer ${token}` | ||
} | ||
}); | ||
socket.on('connect', async ()=>{ | ||
t.pass(); | ||
}); | ||
socket.on('connect_error', async (error)=>{ | ||
t.fail(error.message); | ||
}); | ||
}); | ||
socket.on('connect', ()=>{ | ||
socket.close(); | ||
done(); | ||
await t4.test('should contains user properties', (t)=>{ | ||
t.plan(2); | ||
const socketServer = getSocket(); | ||
socketServer?.on('connection', (client)=>{ | ||
t.equal(client.user.email, basicProfile.email); | ||
t.pass(); | ||
}); | ||
socket = io(API_URL, { | ||
auth: { | ||
token: `Bearer ${token}` | ||
} | ||
}); | ||
socket.on('connect_error', async (error)=>{ | ||
t.fail(error.message); | ||
}); | ||
}); | ||
}); | ||
it('should emit error when user validation fails', (done)=>{ | ||
const socket = (0, _socketIoClient).io('http://localhost:9000', { | ||
auth: { | ||
token: `Bearer ${wrongToken}` | ||
} | ||
await t4.test('should emit error when user validation fails', (t)=>{ | ||
t.plan(2); | ||
socket = io(API_URL, { | ||
auth: { | ||
token: `Bearer ${wrongToken}` | ||
} | ||
}); | ||
socket.on('connect_error', async (error)=>{ | ||
try { | ||
t.equal(error.message, 'Check Field validation failed'); | ||
t.pass(); | ||
} catch { | ||
t.fail(); | ||
} | ||
}); | ||
socket.on('connect', async ()=>{ | ||
t.fail(); | ||
}); | ||
}); | ||
socket.on('connect_error', (err)=>{ | ||
try { | ||
expect(err.message).toEqual('Check Field validation failed'); | ||
} catch (err1) { | ||
socket.close(); | ||
done(err1); | ||
} | ||
socket.close(); | ||
done(); | ||
}); | ||
}); | ||
}); |
import { Server as SocketIoServer } from 'socket.io'; | ||
import { AuthorizeOptions } from '../../index.js'; | ||
export interface Profile { | ||
interface FastifyIo { | ||
instance: SocketIoServer; | ||
} | ||
declare module 'fastify' { | ||
interface FastifyInstance { | ||
io: FastifyIo; | ||
} | ||
} | ||
export interface BasicProfile { | ||
email: string; | ||
id: number; | ||
} | ||
export interface Profile extends BasicProfile { | ||
checkField: boolean; | ||
} | ||
export declare const fixtureStart: (done: any, options?: AuthorizeOptions) => Promise<void>; | ||
export declare const fixtureStop: (callback: Function) => void; | ||
export declare const getSocket: () => SocketIoServer | null; | ||
export declare const PORT = 9000; | ||
export declare const API_URL: string; | ||
export declare const basicProfile: BasicProfile; | ||
export declare const fixtureStart: (options?: AuthorizeOptions) => Promise<void>; | ||
export declare const fixtureStop: () => Promise<void>; | ||
export declare const getSocket: () => SocketIoServer | undefined; | ||
export {}; |
@@ -1,29 +0,17 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.getSocket = exports.fixtureStop = exports.fixtureStart = void 0; | ||
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 _socketIo.Server(httpServer); | ||
} | ||
import jwt from 'jsonwebtoken'; | ||
import { Server as SocketIoServer } from 'socket.io'; | ||
import fastify from 'fastify'; | ||
import { authorize } from '../../index.js'; | ||
export const PORT = 9000; | ||
export const API_URL = `http://localhost:${PORT}`; | ||
export const basicProfile = { | ||
email: 'john@doe.com', | ||
id: 123 | ||
}; | ||
let server = null; | ||
const fixtureStart = async (done, options = { | ||
let application = null; | ||
export const fixtureStart = async (options = { | ||
secret: 'super secret' | ||
})=>{ | ||
const profile = { | ||
email: 'john@doe.com', | ||
id: 123, | ||
...basicProfile, | ||
checkField: true | ||
@@ -42,38 +30,37 @@ }; | ||
} | ||
const app = (0, _express).default(); | ||
app.use(_express.default.json()); | ||
app.post('/login', (_req, res)=>{ | ||
const token = _jsonwebtoken.default.sign(profile, keySecret, { | ||
application = fastify(); | ||
application.post('/login', async (_request, reply)=>{ | ||
const token = jwt.sign(profile, keySecret, { | ||
expiresIn: 60 * 60 * 5 | ||
}); | ||
return res.json({ | ||
reply.statusCode = 201; | ||
return { | ||
token | ||
}); | ||
}; | ||
}); | ||
app.post('/login-wrong', (_req, res)=>{ | ||
application.post('/login-wrong', async (_request, reply)=>{ | ||
profile.checkField = false; | ||
const token = _jsonwebtoken.default.sign(profile, keySecret, { | ||
const token = jwt.sign(profile, keySecret, { | ||
expiresIn: 60 * 60 * 5 | ||
}); | ||
return res.json({ | ||
reply.statusCode = 201; | ||
return { | ||
token | ||
}); | ||
}; | ||
}); | ||
server = app.listen(9000, done); | ||
socket.init(server); | ||
socket.io?.use((0, _indexJs).authorize(options)); | ||
(0, _serverDestroy).default(server); | ||
const instance = new SocketIoServer(application.server); | ||
instance.use(authorize(options)); | ||
application.decorate('io', { | ||
instance | ||
}); | ||
application.addHook('onClose', (fastify1)=>{ | ||
fastify1.io.instance.close(); | ||
}); | ||
await application.listen(PORT); | ||
}; | ||
exports.fixtureStart = fixtureStart; | ||
const fixtureStop = (callback)=>{ | ||
socket.io?.close(); | ||
try { | ||
server?.destroy(); | ||
} catch {} | ||
callback(); | ||
export const fixtureStop = async ()=>{ | ||
await application?.close(); | ||
}; | ||
exports.fixtureStop = fixtureStop; | ||
const getSocket = ()=>{ | ||
return socket.io; | ||
export const getSocket = ()=>{ | ||
return application?.io.instance; | ||
}; | ||
exports.getSocket = getSocket; |
@@ -1,14 +0,4 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.authorize = void 0; | ||
var _jsonwebtoken = _interopRequireDefault(require("jsonwebtoken")); | ||
var _unauthorizedErrorJs = require("./UnauthorizedError.js"); | ||
function _interopRequireDefault(obj) { | ||
return obj && obj.__esModule ? obj : { | ||
default: obj | ||
}; | ||
} | ||
const authorize = (options)=>{ | ||
import jwt from 'jsonwebtoken'; | ||
import { UnauthorizedError } from './UnauthorizedError.js'; | ||
export const authorize = (options)=>{ | ||
const { secret , algorithms =[ | ||
@@ -23,3 +13,3 @@ 'HS256' | ||
if (tokenSplitted.length !== 2 || tokenSplitted[0] !== 'Bearer') { | ||
return next(new _unauthorizedErrorJs.UnauthorizedError('credentials_bad_format', { | ||
return next(new UnauthorizedError('credentials_bad_format', { | ||
message: 'Format is Authorization: Bearer [token]' | ||
@@ -31,3 +21,3 @@ })); | ||
if (encodedToken == null) { | ||
return next(new _unauthorizedErrorJs.UnauthorizedError('credentials_required', { | ||
return next(new UnauthorizedError('credentials_required', { | ||
message: 'no token provided' | ||
@@ -42,3 +32,3 @@ })); | ||
} else { | ||
const completeDecodedToken = _jsonwebtoken.default.decode(encodedToken, { | ||
const completeDecodedToken = jwt.decode(encodedToken, { | ||
complete: true | ||
@@ -49,7 +39,7 @@ }); | ||
try { | ||
decodedToken = _jsonwebtoken.default.verify(encodedToken, keySecret, { | ||
decodedToken = jwt.verify(encodedToken, keySecret, { | ||
algorithms | ||
}); | ||
} catch { | ||
return next(new _unauthorizedErrorJs.UnauthorizedError('invalid_token', { | ||
return next(new UnauthorizedError('invalid_token', { | ||
message: 'Unauthorized: Token is missing or invalid Bearer' | ||
@@ -69,2 +59,1 @@ })); | ||
}; | ||
exports.authorize = authorize; |
@@ -1,50 +0,2 @@ | ||
"use strict"; | ||
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; | ||
} | ||
} | ||
export * from './authorize.js'; | ||
export * from './UnauthorizedError.js'; |
@@ -1,10 +0,5 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.isUnauthorizedError = void 0; | ||
class UnauthorizedError extends Error { | ||
export class UnauthorizedError extends Error { | ||
constructor(code, error){ | ||
super(error.message); | ||
this.message = error.message; | ||
this.name = 'UnauthorizedError'; | ||
this.inner = error; | ||
@@ -19,6 +14,4 @@ this.data = { | ||
} | ||
exports.UnauthorizedError = UnauthorizedError; | ||
const isUnauthorizedError = (error)=>{ | ||
export const isUnauthorizedError = (error)=>{ | ||
return error.data.type === 'UnauthorizedError'; | ||
}; | ||
exports.isUnauthorizedError = isUnauthorizedError; |
{ | ||
"name": "@thream/socketio-jwt", | ||
"version": "2.2.1", | ||
"version": "3.0.0", | ||
"type": "module", | ||
"public": true, | ||
@@ -13,3 +14,3 @@ "description": "Authenticate socket.io incoming connections with JWTs.", | ||
"engines": { | ||
"node": ">=12.0.0" | ||
"node": ">=16.0.0" | ||
}, | ||
@@ -35,2 +36,3 @@ "publishConfig": { | ||
"build": "rimraf ./build && swc ./src --out-dir ./build && tsc", | ||
"build:dev": "swc ./src --out-dir ./build --watch", | ||
"lint:commit": "commitlint", | ||
@@ -42,3 +44,3 @@ "lint:editorconfig": "editorconfig-checker", | ||
"lint:staged": "lint-staged", | ||
"test": "jest", | ||
"test": "c8 tap", | ||
"release": "semantic-release", | ||
@@ -56,38 +58,34 @@ "_postinstall": "husky install", | ||
"devDependencies": { | ||
"@commitlint/cli": "16.2.1", | ||
"@commitlint/cli": "16.2.3", | ||
"@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": "27.4.0", | ||
"@swc/cli": "0.1.57", | ||
"@swc/core": "1.2.164", | ||
"@types/jsonwebtoken": "8.5.8", | ||
"@types/node": "17.0.18", | ||
"@types/server-destroy": "1.0.1", | ||
"@typescript-eslint/eslint-plugin": "4.32.0", | ||
"axios": "0.26.0", | ||
"@types/node": "17.0.23", | ||
"@types/tap": "15.0.6", | ||
"@typescript-eslint/eslint-plugin": "5.18.0", | ||
"@typescript-eslint/parser": "5.18.0", | ||
"axios": "0.26.1", | ||
"c8": "7.11.0", | ||
"editorconfig-checker": "4.0.2", | ||
"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": "8.12.0", | ||
"eslint-config-conventions": "2.0.0", | ||
"eslint-config-prettier": "8.5.0", | ||
"eslint-plugin-import": "2.26.0", | ||
"eslint-plugin-prettier": "4.0.0", | ||
"eslint-plugin-promise": "5.2.0", | ||
"eslint-plugin-unicorn": "40.1.0", | ||
"express": "4.17.3", | ||
"eslint-plugin-promise": "6.0.0", | ||
"eslint-plugin-unicorn": "42.0.0", | ||
"fastify": "3.28.0", | ||
"husky": "7.0.4", | ||
"jest": "27.5.1", | ||
"jest-ts-webcompat-resolver": "1.0.0", | ||
"lint-staged": "12.3.4", | ||
"lint-staged": "12.3.7", | ||
"markdownlint-cli": "0.31.1", | ||
"pinst": "2.1.6", | ||
"pinst": "3.0.0", | ||
"prettier": "2.6.2", | ||
"rimraf": "3.0.2", | ||
"semantic-release": "19.0.2", | ||
"server-destroy": "1.0.1", | ||
"prettier": "2.5.1", | ||
"socket.io": "4.4.1", | ||
"socket.io-client": "4.4.1", | ||
"typescript": "4.5.5" | ||
"tap": "16.0.1", | ||
"typescript": "4.6.3" | ||
} | ||
} |
@@ -30,2 +30,6 @@ <h1 align="center">Thream/socketio-jwt</h1> | ||
## Prerequisites | ||
- [Node.js](https://nodejs.org/) >= 16.0.0 | ||
## 💾 Install | ||
@@ -32,0 +36,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
31
165
Yes
26355
512
1
0