-
-
Notifications
You must be signed in to change notification settings - Fork 798
Expand file tree
/
Copy pathauthentication.ts
More file actions
56 lines (41 loc) · 1.59 KB
/
Copy pathauthentication.ts
File metadata and controls
56 lines (41 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { Application, HookContext } from '@feathersjs/feathers'
import { createDebug } from '@feathersjs/commons'
import { authenticate as AuthenticateHook } from '@feathersjs/authentication'
import { Middleware } from './declarations'
const debug = createDebug('@feathersjs/koa/authentication')
export type AuthenticationSettings = {
service?: string
strategies?: string[]
}
export function parseAuthentication(settings: AuthenticationSettings = {}): Middleware {
return async (ctx, next) => {
const app = ctx.app
const service = app.defaultAuthentication?.(settings.service)
if (!service) {
return next()
}
const config = service.configuration
const authStrategies = settings.strategies || config.parseStrategies || config.authStrategies || []
if (authStrategies.length === 0) {
debug('No `authStrategies` or `parseStrategies` found in authentication configuration')
return next()
}
const authentication = await service.parse(ctx.req, ctx.res, ...authStrategies)
if (authentication) {
debug('Parsed authentication from HTTP header', authentication)
ctx.feathers = { ...ctx.feathers, authentication }
}
return next()
}
}
export function authenticate(settings: string | AuthenticationSettings, ...strategies: string[]): Middleware {
const hook = AuthenticateHook(settings, ...strategies)
return async (ctx, next) => {
const app = ctx.app as Application
const params = ctx.feathers
const context = { app, params } as HookContext
await hook(context)
ctx.feathers = context.params
return next()
}
}