Skip to content

Commit 95ecde9

Browse files
committed
Revert uuid
1 parent ab89eb7 commit 95ecde9

File tree

3 files changed

+262
-6
lines changed

3 files changed

+262
-6
lines changed

package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/authentication/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"jsonwebtoken": "^9.0.2",
6464
"lodash": "^4.17.21",
6565
"long-timeout": "^0.1.1",
66-
"uuid": "^13.0.0"
66+
"uuid": "^11.1.0"
6767
},
6868
"devDependencies": {
6969
"@feathersjs/memory": "^5.0.34",
Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
/* eslint-disable @typescript-eslint/ban-ts-comment */
2+
import assert from 'assert'
3+
import { feathers, Application, Params, ServiceMethods } from '@feathersjs/feathers'
4+
5+
import { Strategy1, Strategy2 } from '../fixtures'
6+
import { AuthenticationService, hooks } from '../../src'
7+
8+
const { authenticate } = hooks
9+
10+
describe('authentication/hooks/authenticate', () => {
11+
let app: Application<{
12+
authentication: AuthenticationService
13+
'auth-v2': AuthenticationService
14+
users: Partial<ServiceMethods> & { id: string }
15+
}>
16+
17+
beforeEach(() => {
18+
app = feathers()
19+
app.use(
20+
'authentication',
21+
new AuthenticationService(app, 'authentication', {
22+
entity: 'user',
23+
service: 'users',
24+
secret: 'supersecret',
25+
authStrategies: ['first']
26+
})
27+
)
28+
app.use(
29+
'auth-v2',
30+
new AuthenticationService(app, 'auth-v2', {
31+
entity: 'user',
32+
service: 'users',
33+
secret: 'supersecret',
34+
authStrategies: ['test']
35+
})
36+
)
37+
app.use('users', {
38+
id: 'id',
39+
40+
async find() {
41+
return []
42+
},
43+
44+
async get(_id: string | number, params: Params) {
45+
return params
46+
}
47+
})
48+
49+
const service = app.service('authentication')
50+
51+
service.register('first', new Strategy1())
52+
service.register('second', new Strategy2())
53+
54+
app.service('auth-v2').register('test', new Strategy1())
55+
56+
app.service('users').hooks({
57+
get: [authenticate('first', 'second')]
58+
})
59+
60+
app.service('users').id = 'name'
61+
app.setup()
62+
})
63+
64+
it('throws an error when no strategies are passed', () => {
65+
try {
66+
// @ts-ignore
67+
authenticate()
68+
assert.fail('Should never get here')
69+
} catch (error: any) {
70+
assert.strictEqual(error.message, 'The authenticate hook needs at least one allowed strategy')
71+
}
72+
})
73+
74+
it('throws an error when not a before hook', async () => {
75+
const users = app.service('users')
76+
77+
users.hooks({
78+
after: {
79+
all: [authenticate('first')]
80+
}
81+
})
82+
83+
try {
84+
await users.find()
85+
assert.fail('Should never get here')
86+
} catch (error: any) {
87+
assert.strictEqual(error.name, 'NotAuthenticated')
88+
assert.strictEqual(error.message, 'The authenticate hook must be used as a before hook')
89+
}
90+
})
91+
92+
it('throws an error if authentication service is gone', async () => {
93+
delete app.services.authentication
94+
95+
try {
96+
await app.service('users').get(1, {
97+
authentication: {
98+
some: 'thing'
99+
}
100+
})
101+
assert.fail('Should never get here')
102+
} catch (error: any) {
103+
assert.strictEqual(error.name, 'NotAuthenticated')
104+
assert.strictEqual(error.message, 'Could not find a valid authentication service')
105+
}
106+
})
107+
108+
it('authenticates with first strategy, merges params', async () => {
109+
const params = {
110+
authentication: {
111+
strategy: 'first',
112+
username: 'David'
113+
}
114+
}
115+
116+
const result = await app.service('users').get(1, params)
117+
118+
assert.deepStrictEqual(result, Object.assign({}, params, Strategy1.result))
119+
})
120+
121+
it('authenticates with first strategy, keeps references alive (#1629)', async () => {
122+
const connection = {}
123+
const params = {
124+
connection,
125+
authentication: {
126+
strategy: 'first',
127+
username: 'David'
128+
}
129+
}
130+
131+
app.service('users').hooks({
132+
after: {
133+
get: (context) => {
134+
context.result.params = context.params
135+
}
136+
}
137+
})
138+
139+
const result = await app.service('users').get(1, params)
140+
141+
assert.ok(result.params.connection === connection)
142+
})
143+
144+
it('authenticates with different authentication service', async () => {
145+
const params = {
146+
authentication: {
147+
strategy: 'test',
148+
username: 'David'
149+
}
150+
}
151+
152+
app.service('users').hooks({
153+
before: {
154+
find: [
155+
authenticate({
156+
service: 'auth-v2',
157+
strategies: ['test']
158+
})
159+
]
160+
}
161+
})
162+
163+
const result = await app.service('users').find(params)
164+
165+
assert.deepStrictEqual(result, [])
166+
})
167+
168+
it('authenticates with second strategy', async () => {
169+
const params = {
170+
authentication: {
171+
strategy: 'second',
172+
v2: true,
173+
password: 'supersecret'
174+
}
175+
}
176+
177+
const result = await app.service('users').get(1, params)
178+
179+
assert.deepStrictEqual(
180+
result,
181+
Object.assign(
182+
{
183+
authentication: params.authentication,
184+
params: { authenticated: true }
185+
},
186+
Strategy2.result
187+
)
188+
)
189+
})
190+
191+
it('passes for internal calls without authentication', async () => {
192+
const result = await app.service('users').get(1)
193+
194+
assert.deepStrictEqual(result, {})
195+
})
196+
197+
it('fails for invalid params.authentication', async () => {
198+
try {
199+
await app.service('users').get(1, {
200+
authentication: {
201+
strategy: 'first',
202+
some: 'thing'
203+
}
204+
})
205+
assert.fail('Should never get here')
206+
} catch (error: any) {
207+
assert.strictEqual(error.name, 'NotAuthenticated')
208+
assert.strictEqual(error.message, 'Invalid Dave')
209+
}
210+
})
211+
212+
it('fails for external calls without authentication', async () => {
213+
try {
214+
await app.service('users').get(1, {
215+
provider: 'rest'
216+
})
217+
assert.fail('Should never get here')
218+
} catch (error: any) {
219+
assert.strictEqual(error.name, 'NotAuthenticated')
220+
assert.strictEqual(error.message, 'Not authenticated')
221+
}
222+
})
223+
224+
it('passes with authenticated: true but external call', async () => {
225+
const params = {
226+
provider: 'rest',
227+
authenticated: true
228+
}
229+
const result = await app.service('users').get(1, params)
230+
231+
assert.deepStrictEqual(result, params)
232+
})
233+
234+
it('errors when used on the authentication service', async () => {
235+
const auth = app.service('authentication')
236+
237+
auth.hooks({
238+
before: {
239+
create: authenticate('first')
240+
}
241+
})
242+
243+
try {
244+
await auth.create({
245+
strategy: 'first',
246+
username: 'David'
247+
})
248+
assert.fail('Should never get here')
249+
} catch (error: any) {
250+
assert.strictEqual(
251+
error.message,
252+
'The authenticate hook does not need to be used on the authentication service'
253+
)
254+
}
255+
})
256+
})

0 commit comments

Comments
 (0)