Skip to content

Commit

Permalink
fix(authentication): Add safe dispatch data for authentication reques…
Browse files Browse the repository at this point in the history
…ts (#2662)
  • Loading branch information
daffl authored Jun 11, 2022
1 parent f7e87db commit d8104a1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/authentication/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"@feathersjs/errors": "^5.0.0-pre.23",
"@feathersjs/feathers": "^5.0.0-pre.23",
"@feathersjs/transport-commons": "^5.0.0-pre.23",
"@feathersjs/schema": "^5.0.0-pre.23",
"@types/jsonwebtoken": "^8.5.8",
"jsonwebtoken": "^8.5.1",
"lodash": "^4.17.21",
Expand All @@ -64,7 +65,6 @@
},
"devDependencies": {
"@feathersjs/memory": "^5.0.0-pre.23",
"@feathersjs/schema": "^5.0.0-pre.23",
"@types/lodash": "^4.14.182",
"@types/mocha": "^9.1.1",
"@types/node": "^17.0.40",
Expand Down
13 changes: 8 additions & 5 deletions packages/authentication/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { connection, event } from './hooks'
import '@feathersjs/transport-commons'
import { createDebug } from '@feathersjs/commons'
import { ServiceMethods, ServiceAddons } from '@feathersjs/feathers'
import { resolveDispatch } from '@feathersjs/schema'
import jsonwebtoken from 'jsonwebtoken'

const debug = createDebug('@feathersjs/authentication/service')
Expand Down Expand Up @@ -120,12 +121,14 @@ export class AuthenticationService

const accessToken = await this.createAccessToken(payload, jwtOptions, params.secret)

return merge({ accessToken }, authResult, {
return {
accessToken,
...authResult,
authentication: {
accessToken,
...authResult.authentication,
payload: jsonwebtoken.decode(accessToken)
}
})
}
}

/**
Expand Down Expand Up @@ -182,8 +185,8 @@ export class AuthenticationService
}

this.hooks({
create: [connection('login'), event('login')],
remove: [connection('logout'), event('logout')]
create: [resolveDispatch(), connection('login'), event('login')],
remove: [resolveDispatch(), connection('logout'), event('logout')]
} as any)

this.app.on('disconnect', async (connection) => {
Expand Down
25 changes: 25 additions & 0 deletions packages/authentication/test/jwt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import assert from 'assert'
import merge from 'lodash/merge'
import { feathers, Application, Service } from '@feathersjs/feathers'
import { memory } from '@feathersjs/memory'
import { getDispatch, resolve, resolveDispatch } from '@feathersjs/schema'

import { AuthenticationService, JWTStrategy, hooks } from '../src'
import { ServerResponse } from 'http'
Expand All @@ -19,6 +20,16 @@ describe('authentication/jwt', () => {
let accessToken: string
let payload: any

const userDispatchResolver = resolve({
converter: async () => {
return {
dispatch: true,
message: 'Hello world'
}
},
properties: {}
})

beforeEach(async () => {
app = feathers()

Expand Down Expand Up @@ -51,6 +62,9 @@ describe('authentication/jwt', () => {
})

app.service('users').hooks({
around: {
all: [resolveDispatch(userDispatchResolver)]
},
after: {
get: [
(context) => {
Expand Down Expand Up @@ -119,6 +133,17 @@ describe('authentication/jwt', () => {
})
})

it('resolves safe dispatch data in authentication result', async () => {
const authResult = await app.service('authentication').create({
strategy: 'jwt',
accessToken
})

const dispatch = getDispatch(authResult)

assert.deepStrictEqual(dispatch.user, { dispatch: true, message: 'Hello world' })
})

it('sends disconnect event when connection token expires and removes all connection information', async () => {
const connection: any = {}
const token: string = await app.service('authentication').createAccessToken(
Expand Down
3 changes: 1 addition & 2 deletions packages/express/test/authentication.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ describe('@feathersjs/express/authentication', () => {
it('successful local authentication', () => {
assert.ok(authResult.accessToken)
assert.deepStrictEqual(omit(authResult.authentication, 'payload'), {
strategy: 'local',
accessToken: authResult.accessToken
strategy: 'local'
})
assert.strictEqual(authResult.user.email, email)
assert.strictEqual(authResult.user.password, undefined)
Expand Down

0 comments on commit d8104a1

Please sign in to comment.