-
-
Notifications
You must be signed in to change notification settings - Fork 798
Expand file tree
/
Copy pathauthentication.ts
More file actions
66 lines (50 loc) · 1.91 KB
/
Copy pathauthentication.ts
File metadata and controls
66 lines (50 loc) · 1.91 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
57
58
59
60
61
62
63
64
65
66
import { RequestHandler, Request, Response } from 'express'
import { HookContext } from '@feathersjs/feathers'
import { createDebug } from '@feathersjs/commons'
import { authenticate as AuthenticateHook } from '@feathersjs/authentication'
import { Application } from './declarations'
const debug = createDebug('@feathersjs/express/authentication')
const toHandler = (
func: (req: Request, res: Response, next: () => void) => Promise<void>
): RequestHandler => {
return (req, res, next) => func(req, res, next).catch((error) => next(error))
}
export type AuthenticationSettings = {
service?: string
strategies?: string[]
}
export function parseAuthentication(settings: AuthenticationSettings = {}): RequestHandler {
return toHandler(async (req, res, next) => {
const app = req.app as any as Application
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(req, res, ...authStrategies)
if (authentication) {
debug('Parsed authentication from HTTP header', authentication)
req.feathers = { ...req.feathers, authentication }
}
return next()
})
}
export function authenticate(
settings: string | AuthenticationSettings,
...strategies: string[]
): RequestHandler {
const hook = AuthenticateHook(settings, ...strategies)
return toHandler(async (req, _res, next) => {
const app = req.app as any as Application
const params = req.feathers
const context = { app, params } as any as HookContext
await hook(context)
req.feathers = context.params
return next()
})
}