-
-
Notifications
You must be signed in to change notification settings - Fork 750
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(express, koa): make transports similar (#2486)
- Loading branch information
Showing
24 changed files
with
401 additions
and
336 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,61 @@ | ||
import { RequestHandler, Request, Response } from 'express'; | ||
import { HookContext } from '@feathersjs/feathers'; | ||
import { createDebug } from '@feathersjs/commons'; | ||
import { merge, flatten } from 'lodash'; | ||
import { NextFunction, RequestHandler, Request, Response } from 'express'; | ||
import { authenticate as AuthenticateHook } from '@feathersjs/authentication'; | ||
|
||
import { Application } from './declarations'; | ||
|
||
const debug = createDebug('@feathersjs/express/authentication'); | ||
|
||
type StrategyOptions = { | ||
service?: string; | ||
strategies: string[] | ||
const toHandler = (func: (req: Request, res: Response, next: () => void) => Promise<void>): RequestHandler => { | ||
return (req, res, next) => func(req, res, next).catch(error => next(error)); | ||
}; | ||
|
||
const normalizeStrategy = (_settings: string|StrategyOptions, ..._strategies: string[]) => | ||
typeof _settings === 'string' | ||
? { strategies: flatten([ _settings, ..._strategies ]) } | ||
: _settings; | ||
export type AuthenticationSettings = { | ||
service?: string; | ||
strategies?: string[]; | ||
}; | ||
|
||
export function parseAuthentication (settings: any = {}): RequestHandler { | ||
return function (req, res, next) { | ||
const app = req.app as any; | ||
const service = app.defaultAuthentication ? app.defaultAuthentication(settings.service) : null; | ||
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 === null) { | ||
if (!service) { | ||
return next(); | ||
} | ||
|
||
const config = service.configuration; | ||
const authStrategies = config.parseStrategies || config.authStrategies || []; | ||
const authStrategies = settings.strategies || config.parseStrategies || config.authStrategies || []; | ||
|
||
if (authStrategies.length === 0) { | ||
debug('No `authStrategies` or `parseStrategies` found in authentication configuration'); | ||
return next(); | ||
} | ||
|
||
service.parse(req, res, ...authStrategies) | ||
.then((authentication: any) => { | ||
if (authentication) { | ||
debug('Parsed authentication from HTTP header', authentication); | ||
merge(req, { | ||
authentication, | ||
feathers: { authentication } | ||
}); | ||
} | ||
|
||
next(); | ||
}).catch(next); | ||
}; | ||
} | ||
const authentication = await service.parse(req, res, ...authStrategies) | ||
|
||
if (authentication) { | ||
debug('Parsed authentication from HTTP header', authentication); | ||
req.feathers = { ...req.feathers, authentication }; | ||
} | ||
|
||
export function authenticate (_settings: string|StrategyOptions, ..._strategies: string[]) { | ||
const settings = normalizeStrategy(_settings, ..._strategies); | ||
return next(); | ||
}); | ||
} | ||
|
||
if (!Array.isArray(settings.strategies) || settings.strategies.length === 0) { | ||
throw new Error('\'authenticate\' middleware requires at least one strategy name'); | ||
} | ||
export function authenticate (settings: string | AuthenticationSettings, ...strategies: string[]): RequestHandler { | ||
const hook = AuthenticateHook(settings, ...strategies); | ||
|
||
return (_req: Request, _res: Response, next: NextFunction) => { | ||
const req = _req as any; | ||
const { app, authentication } = req; | ||
const service = app.defaultAuthentication(settings.service); | ||
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; | ||
|
||
debug('Authenticating with Express middleware and strategies', settings.strategies); | ||
await hook(context); | ||
|
||
service.authenticate(authentication, req.feathers, ...settings.strategies) | ||
.then((authResult: any) => { | ||
debug('Merging request with', authResult); | ||
merge(req, authResult); | ||
req.feathers = context.params; | ||
|
||
next(); | ||
}).catch(next); | ||
}; | ||
return next(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.