@thream/socketio-jwt
Advanced tools
Comparing version 3.1.3 to 3.1.4
@@ -1,7 +0,7 @@ | ||
import { Server as SocketIoServer } from 'socket.io'; | ||
import type { AuthorizeOptions } from '../../index.js'; | ||
import { Server as SocketIoServer } from "socket.io"; | ||
import type { AuthorizeOptions } from "../../index.js"; | ||
interface FastifyIo { | ||
instance: SocketIoServer; | ||
} | ||
declare module 'fastify' { | ||
declare module "fastify" { | ||
interface FastifyInstance { | ||
@@ -8,0 +8,0 @@ io: FastifyIo; |
@@ -1,9 +0,9 @@ | ||
import jwt from 'jsonwebtoken'; | ||
import { Server as SocketIoServer } from 'socket.io'; | ||
import fastify from 'fastify'; | ||
import { authorize } from '../../index.js'; | ||
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', | ||
email: "john@doe.com", | ||
id: 123 | ||
@@ -13,3 +13,3 @@ }; | ||
export const fixtureStart = async (options = { | ||
secret: 'super secret' | ||
secret: "super secret" | ||
})=>{ | ||
@@ -20,4 +20,4 @@ const profile = { | ||
}; | ||
let keySecret = ''; | ||
if (typeof options.secret === 'string') { | ||
let keySecret = ""; | ||
if (typeof options.secret === "string") { | ||
keySecret = options.secret; | ||
@@ -27,3 +27,3 @@ } else { | ||
header: { | ||
alg: 'HS256' | ||
alg: "HS256" | ||
}, | ||
@@ -34,3 +34,3 @@ payload: profile | ||
application = fastify(); | ||
application.post('/login', async (_request, reply)=>{ | ||
application.post("/login", async (_request, reply)=>{ | ||
const token = jwt.sign(profile, keySecret, { | ||
@@ -44,3 +44,3 @@ expiresIn: 60 * 60 * 5 | ||
}); | ||
application.post('/login-wrong', async (_request, reply)=>{ | ||
application.post("/login-wrong", async (_request, reply)=>{ | ||
profile.checkField = false; | ||
@@ -57,6 +57,6 @@ const token = jwt.sign(profile, keySecret, { | ||
instance.use(authorize(options)); | ||
application.decorate('io', { | ||
application.decorate("io", { | ||
instance | ||
}); | ||
application.addHook('onClose', (fastify)=>{ | ||
application.addHook("onClose", (fastify)=>{ | ||
fastify.io.instance.close(); | ||
@@ -63,0 +63,0 @@ }); |
@@ -1,5 +0,5 @@ | ||
import type { Algorithm } from 'jsonwebtoken'; | ||
import type { Socket } from 'socket.io'; | ||
import { UnauthorizedError } from './UnauthorizedError.js'; | ||
declare module 'socket.io' { | ||
import type { Algorithm } from "jsonwebtoken"; | ||
import type { Socket } from "socket.io"; | ||
import { UnauthorizedError } from "./UnauthorizedError.js"; | ||
declare module "socket.io" { | ||
interface Socket extends ExtendedSocket { | ||
@@ -6,0 +6,0 @@ } |
@@ -1,6 +0,6 @@ | ||
import jwt from 'jsonwebtoken'; | ||
import { UnauthorizedError } from './UnauthorizedError.js'; | ||
import jwt from "jsonwebtoken"; | ||
import { UnauthorizedError } from "./UnauthorizedError.js"; | ||
export const authorize = (options)=>{ | ||
const { secret, algorithms = [ | ||
'HS256' | ||
"HS256" | ||
], onAuthentication } = options; | ||
@@ -11,6 +11,6 @@ return async (socket, next)=>{ | ||
if (token != null) { | ||
const tokenSplitted = token.split(' '); | ||
if (tokenSplitted.length !== 2 || tokenSplitted[0] !== 'Bearer') { | ||
return next(new UnauthorizedError('credentials_bad_format', { | ||
message: 'Format is Authorization: Bearer [token]' | ||
const tokenSplitted = token.split(" "); | ||
if (tokenSplitted.length !== 2 || tokenSplitted[0] !== "Bearer") { | ||
return next(new UnauthorizedError("credentials_bad_format", { | ||
message: "Format is Authorization: Bearer [token]" | ||
})); | ||
@@ -21,4 +21,4 @@ } | ||
if (encodedToken == null) { | ||
return next(new UnauthorizedError('credentials_required', { | ||
message: 'no token provided' | ||
return next(new UnauthorizedError("credentials_required", { | ||
message: "no token provided" | ||
})); | ||
@@ -29,3 +29,3 @@ } | ||
let decodedToken = null; | ||
if (typeof secret === 'string') { | ||
if (typeof secret === "string") { | ||
keySecret = secret; | ||
@@ -43,4 +43,4 @@ } else { | ||
} catch { | ||
return next(new UnauthorizedError('invalid_token', { | ||
message: 'Unauthorized: Token is missing or invalid Bearer' | ||
return next(new UnauthorizedError("invalid_token", { | ||
message: "Unauthorized: Token is missing or invalid Bearer" | ||
})); | ||
@@ -47,0 +47,0 @@ } |
@@ -1,2 +0,2 @@ | ||
export * from './authorize.js'; | ||
export * from './UnauthorizedError.js'; | ||
export * from "./authorize.js"; | ||
export * from "./UnauthorizedError.js"; |
@@ -1,4 +0,4 @@ | ||
export * from './authorize.js'; | ||
export * from './UnauthorizedError.js'; | ||
export * from "./authorize.js"; | ||
export * from "./UnauthorizedError.js"; | ||
//# sourceMappingURL=index.js.map |
@@ -8,3 +8,3 @@ export declare class UnauthorizedError extends Error { | ||
code: string; | ||
type: 'UnauthorizedError'; | ||
type: "UnauthorizedError"; | ||
}; | ||
@@ -11,0 +11,0 @@ constructor(code: string, error: { |
@@ -6,3 +6,3 @@ export class UnauthorizedError extends Error { | ||
super(error.message); | ||
this.name = 'UnauthorizedError'; | ||
this.name = "UnauthorizedError"; | ||
this.inner = error; | ||
@@ -12,3 +12,3 @@ this.data = { | ||
code, | ||
type: 'UnauthorizedError' | ||
type: "UnauthorizedError" | ||
}; | ||
@@ -19,5 +19,5 @@ Object.setPrototypeOf(this, UnauthorizedError.prototype); | ||
export const isUnauthorizedError = (error)=>{ | ||
return typeof error === 'object' && error != null && 'data' in error && typeof error.data === 'object' && error.data != null && 'type' in error.data && error.data.type === 'UnauthorizedError'; | ||
return typeof error === "object" && error != null && "data" in error && typeof error.data === "object" && error.data != null && "type" in error.data && error.data.type === "UnauthorizedError"; | ||
}; | ||
//# sourceMappingURL=UnauthorizedError.js.map |
{ | ||
"name": "@thream/socketio-jwt", | ||
"version": "3.1.3", | ||
"version": "3.1.4", | ||
"type": "module", | ||
@@ -61,24 +61,24 @@ "public": true, | ||
"devDependencies": { | ||
"@commitlint/cli": "17.7.1", | ||
"@commitlint/config-conventional": "17.7.0", | ||
"@commitlint/cli": "18.0.0", | ||
"@commitlint/config-conventional": "18.0.0", | ||
"@swc/cli": "0.1.62", | ||
"@swc/core": "1.3.85", | ||
"@swc/core": "1.3.94", | ||
"@tsconfig/strictest": "2.0.2", | ||
"@types/jsonwebtoken": "9.0.3", | ||
"@types/node": "20.6.2", | ||
"@typescript-eslint/eslint-plugin": "6.7.2", | ||
"@typescript-eslint/parser": "6.7.2", | ||
"axios": "1.5.0", | ||
"@types/jsonwebtoken": "9.0.4", | ||
"@types/node": "20.8.7", | ||
"@typescript-eslint/eslint-plugin": "6.9.0", | ||
"@typescript-eslint/parser": "6.9.0", | ||
"axios": "1.5.1", | ||
"cross-env": "7.0.3", | ||
"editorconfig-checker": "5.1.1", | ||
"eslint": "8.49.0", | ||
"eslint-config-conventions": "11.0.1", | ||
"eslint": "8.52.0", | ||
"eslint-config-conventions": "12.0.0", | ||
"eslint-config-prettier": "9.0.0", | ||
"eslint-plugin-import": "2.28.1", | ||
"eslint-plugin-prettier": "5.0.0", | ||
"eslint-plugin-import": "2.29.0", | ||
"eslint-plugin-prettier": "5.0.1", | ||
"eslint-plugin-promise": "6.1.1", | ||
"eslint-plugin-unicorn": "48.0.1", | ||
"fastify": "4.23.2", | ||
"fastify": "4.24.3", | ||
"husky": "8.0.3", | ||
"lint-staged": "14.0.1", | ||
"lint-staged": "15.0.2", | ||
"markdownlint-cli2": "0.10.0", | ||
@@ -88,4 +88,4 @@ "markdownlint-rule-relative-links": "2.1.0", | ||
"prettier": "3.0.3", | ||
"rimraf": "5.0.1", | ||
"semantic-release": "22.0.0", | ||
"rimraf": "5.0.5", | ||
"semantic-release": "22.0.5", | ||
"socket.io": "4.7.2", | ||
@@ -92,0 +92,0 @@ "socket.io-client": "4.7.2", |
@@ -8,2 +8,6 @@ <h1 align="center">Thream/socketio-jwt</h1> | ||
<p align="center"> | ||
<strong>⚠️ This project is not maintained anymore, you can still use the code as you wish and fork it to maintain it yourself.</strong> | ||
</p> | ||
<p align="center"> | ||
<a href="./CONTRIBUTING.md"><img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat" /></a> | ||
@@ -46,4 +50,4 @@ <a href="./LICENSE"><img src="https://img.shields.io/badge/licence-MIT-blue.svg" alt="Licence MIT"/></a> | ||
```ts | ||
import { Server } from 'socket.io' | ||
import { authorize } from '@thream/socketio-jwt' | ||
import { Server } from "socket.io" | ||
import { authorize } from "@thream/socketio-jwt" | ||
@@ -53,7 +57,7 @@ const io = new Server(9000) | ||
authorize({ | ||
secret: 'your secret or public key' | ||
}) | ||
secret: "your secret or public key", | ||
}), | ||
) | ||
io.on('connection', async (socket) => { | ||
io.on("connection", async (socket) => { | ||
// jwt payload of the connected client | ||
@@ -65,3 +69,3 @@ console.log(socket.decodedToken) | ||
const client = io.sockets.sockets.get(clientId) | ||
client?.emit('messages', { message: 'Success!' }) | ||
client?.emit("messages", { message: "Success!" }) | ||
// we can access the jwt payload of each connected client | ||
@@ -77,8 +81,8 @@ console.log(client?.decodedToken) | ||
```ts | ||
import jwksClient from 'jwks-rsa' | ||
import { Server } from 'socket.io' | ||
import { authorize } from '@thream/socketio-jwt' | ||
import jwksClient from "jwks-rsa" | ||
import { Server } from "socket.io" | ||
import { authorize } from "@thream/socketio-jwt" | ||
const client = jwksClient({ | ||
jwksUri: 'https://sandrino.auth0.com/.well-known/jwks.json' | ||
jwksUri: "https://sandrino.auth0.com/.well-known/jwks.json", | ||
}) | ||
@@ -92,7 +96,7 @@ | ||
return key.getPublicKey() | ||
} | ||
}) | ||
}, | ||
}), | ||
) | ||
io.on('connection', async (socket) => { | ||
io.on("connection", async (socket) => { | ||
// jwt payload of the connected client | ||
@@ -107,4 +111,4 @@ console.log(socket.decodedToken) | ||
```ts | ||
import { Server } from 'socket.io' | ||
import { authorize } from '@thream/socketio-jwt' | ||
import { Server } from "socket.io" | ||
import { authorize } from "@thream/socketio-jwt" | ||
@@ -114,11 +118,11 @@ const io = new Server(9000) | ||
authorize({ | ||
secret: 'your secret or public key', | ||
secret: "your secret or public key", | ||
onAuthentication: async (decodedToken) => { | ||
// return the object that you want to add to the user property | ||
// or throw an error if the token is unauthorized | ||
} | ||
}) | ||
}, | ||
}), | ||
) | ||
io.on('connection', async (socket) => { | ||
io.on("connection", async (socket) => { | ||
// jwt payload of the connected client | ||
@@ -141,14 +145,14 @@ console.log(socket.decodedToken) | ||
```ts | ||
import { io } from 'socket.io-client' | ||
import { isUnauthorizedError } from '@thream/socketio-jwt/build/UnauthorizedError.js' | ||
import { io } from "socket.io-client" | ||
import { isUnauthorizedError } from "@thream/socketio-jwt/build/UnauthorizedError.js" | ||
// Require Bearer Token | ||
const socket = io('http://localhost:9000', { | ||
auth: { token: `Bearer ${yourJWT}` } | ||
const socket = io("http://localhost:9000", { | ||
auth: { token: `Bearer ${yourJWT}` }, | ||
}) | ||
// Handling token expiration | ||
socket.on('connect_error', (error) => { | ||
socket.on("connect_error", (error) => { | ||
if (isUnauthorizedError(error)) { | ||
console.log('User token has expired') | ||
console.log("User token has expired") | ||
} | ||
@@ -158,3 +162,3 @@ }) | ||
// Listening to events | ||
socket.on('messages', (data) => { | ||
socket.on("messages", (data) => { | ||
console.log(data) | ||
@@ -161,0 +165,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
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
16016
167