mirror of
https://github.com/Thream/socketio-jwt.git
synced 2024-07-21 09:38:31 +02:00
feat: usage of auth option to send credentials
BREAKING CHANGE: extraHeaders with Authorization doesn't work anymore See: https://socket.io/docs/v3/middlewares/#Sending-credentials
This commit is contained in:
parent
4ba3e3bccb
commit
a14d4e937b
@ -97,9 +97,9 @@ io.on('connection', async (socket) => {
|
|||||||
```ts
|
```ts
|
||||||
import { io } from 'socket.io-client'
|
import { io } from 'socket.io-client'
|
||||||
|
|
||||||
// Require Bearer Tokens to be passed in as an Authorization Header
|
// Require Bearer Token
|
||||||
const socket = io('http://localhost:9000', {
|
const socket = io('http://localhost:9000', {
|
||||||
extraHeaders: { Authorization: `Bearer ${yourJWT}` }
|
auth: { token: `Bearer ${yourJWT}` }
|
||||||
})
|
})
|
||||||
|
|
||||||
// Handling token expiration
|
// Handling token expiration
|
||||||
|
@ -31,7 +31,7 @@ describe('authorize - with secret as string in options', () => {
|
|||||||
|
|
||||||
it('should emit error with bad token format', (done) => {
|
it('should emit error with bad token format', (done) => {
|
||||||
const socket = io('http://localhost:9000', {
|
const socket = io('http://localhost:9000', {
|
||||||
extraHeaders: { Authorization: 'testing' }
|
auth: { token: 'testing' }
|
||||||
})
|
})
|
||||||
socket.on('connect_error', (err: any) => {
|
socket.on('connect_error', (err: any) => {
|
||||||
expect(err.data.message).toEqual(
|
expect(err.data.message).toEqual(
|
||||||
@ -45,7 +45,7 @@ describe('authorize - with secret as string in options', () => {
|
|||||||
|
|
||||||
it('should emit error with unauthorized handshake', (done) => {
|
it('should emit error with unauthorized handshake', (done) => {
|
||||||
const socket = io('http://localhost:9000', {
|
const socket = io('http://localhost:9000', {
|
||||||
extraHeaders: { Authorization: 'Bearer testing' }
|
auth: { token: 'Bearer testing' }
|
||||||
})
|
})
|
||||||
socket.on('connect_error', (err: any) => {
|
socket.on('connect_error', (err: any) => {
|
||||||
expect(err.data.message).toEqual(
|
expect(err.data.message).toEqual(
|
||||||
@ -59,7 +59,7 @@ describe('authorize - with secret as string in options', () => {
|
|||||||
|
|
||||||
it('should connect the user', (done) => {
|
it('should connect the user', (done) => {
|
||||||
const socket = io('http://localhost:9000', {
|
const socket = io('http://localhost:9000', {
|
||||||
extraHeaders: { Authorization: `Bearer ${token}` }
|
auth: { token: `Bearer ${token}` }
|
||||||
})
|
})
|
||||||
socket.on('connect', () => {
|
socket.on('connect', () => {
|
||||||
socket.close()
|
socket.close()
|
||||||
@ -93,7 +93,7 @@ describe('authorize - with secret as callback in options', () => {
|
|||||||
|
|
||||||
it('should connect the user', (done) => {
|
it('should connect the user', (done) => {
|
||||||
const socket = io('http://localhost:9000', {
|
const socket = io('http://localhost:9000', {
|
||||||
extraHeaders: { Authorization: `Bearer ${token}` }
|
auth: { token: `Bearer ${token}` }
|
||||||
})
|
})
|
||||||
socket.on('connect', () => {
|
socket.on('connect', () => {
|
||||||
socket.close()
|
socket.close()
|
||||||
|
@ -40,9 +40,9 @@ export const authorize = (options: AuthorizeOptions): SocketIOMiddleware => {
|
|||||||
const { secret, algorithms = ['HS256'] } = options
|
const { secret, algorithms = ['HS256'] } = options
|
||||||
return async (socket, next) => {
|
return async (socket, next) => {
|
||||||
let encodedToken: string | null = null
|
let encodedToken: string | null = null
|
||||||
const authorizationHeader = socket.request.headers.authorization
|
const { token } = socket.handshake.auth
|
||||||
if (authorizationHeader != null) {
|
if (token != null) {
|
||||||
const tokenSplitted = authorizationHeader.split(' ')
|
const tokenSplitted = token.split(' ')
|
||||||
if (tokenSplitted.length !== 2 || tokenSplitted[0] !== 'Bearer') {
|
if (tokenSplitted.length !== 2 || tokenSplitted[0] !== 'Bearer') {
|
||||||
return next(
|
return next(
|
||||||
new UnauthorizedError('credentials_bad_format', {
|
new UnauthorizedError('credentials_bad_format', {
|
||||||
|
Loading…
Reference in New Issue
Block a user