-
-
Notifications
You must be signed in to change notification settings - Fork 797
feat(express, koa): make transports similar #2486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯 for removing Lodash!