diff --git a/packages/core/src/common/filterManager.ts b/packages/core/src/common/filterManager.ts index c98ebd0b279..59027161593 100644 --- a/packages/core/src/common/filterManager.ts +++ b/packages/core/src/common/filterManager.ts @@ -52,12 +52,13 @@ export class FilterManager< const filter = await applicationContext.getAsync(FilterClass); const exceptionMetadata = getClassMetadata(CATCH_KEY, FilterClass); if (exceptionMetadata && exceptionMetadata.catchTargets) { + exceptionMetadata.catchOptions = exceptionMetadata.catchOptions || {}; for (const Exception of exceptionMetadata.catchTargets) { this.exceptionMap.set(Exception, { filter, - catchOptions: exceptionMetadata.catchOptions || {}, + catchOptions: exceptionMetadata.catchOptions, }); - if (exceptionMetadata?.catchOptions['matchPrototype']) { + if (exceptionMetadata.catchOptions['matchPrototype']) { this.protoMatchList.push(err => { if (err instanceof Exception) { return Exception; diff --git a/packages/passport/src/passport/authenticator.ts b/packages/passport/src/passport/authenticator.ts index 0b3731686bb..7547a9c2d3e 100644 --- a/packages/passport/src/passport/authenticator.ts +++ b/packages/passport/src/passport/authenticator.ts @@ -239,21 +239,7 @@ export class PassportAuthenticator { }; } - public serializeUser(fn, req?, done?) { - if (typeof fn === 'function') { - return this._serializers.push(fn); - } - - // private implementation that traverses the chain of serializers, attempting - // to serialize a user - const user = fn; - - // For backwards compatibility - if (typeof req === 'function') { - done = req; - req = undefined; - } - + public serializeUser(user, req, done) { const stack = this._serializers; (function pass(i, err, obj) { // serializers use 'pass' as an error to skip processing @@ -300,21 +286,7 @@ export class PassportAuthenticator { * * @api public */ - public deserializeUser(fn, req?, done?) { - if (typeof fn === 'function') { - return this._deserializers.push(fn); - } - - // private implementation that traverses the chain of deserializers, - // attempting to deserialize a user - const obj = fn; - - // For backwards compatibility - if (typeof req === 'function') { - done = req; - req = undefined; - } - + public deserializeUser(obj, req, done) { const stack = this._deserializers; (function pass(i, err, user) { // deserializers use 'pass' as an error to skip processing @@ -391,21 +363,7 @@ export class PassportAuthenticator { * * @api public */ - public transformAuthInfo(fn, req?, done?) { - if (typeof fn === 'function') { - return this._infoTransformers.push(fn); - } - - // private implementation that traverses the chain of transformers, - // attempting to transform auth info - const info = fn; - - // For backwards compatibility - if (typeof req === 'function') { - done = req; - req = undefined; - } - + public transformAuthInfo(info, req, done) { const stack = this._infoTransformers; (function pass(i, err, tinfo) { // transformers use 'pass' as an error to skip processing @@ -509,4 +467,16 @@ export class PassportAuthenticator { }); }); } + + public addSerializer(fn) { + this._serializers.push(fn); + } + + public addDeserializer(fn) { + this._deserializers.push(fn); + } + + public addInfoTransformer(fn) { + this._infoTransformers.push(fn); + } } diff --git a/packages/passport/src/passport/passport.service.ts b/packages/passport/src/passport/passport.service.ts index 16e027f33a4..2c83c071b9a 100644 --- a/packages/passport/src/passport/passport.service.ts +++ b/packages/passport/src/passport/passport.service.ts @@ -1,6 +1,6 @@ import { App, Config, Init, Inject } from '@midwayjs/decorator'; import { AbstractPassportMiddleware, AuthenticateOptions } from '../interface'; -import { httpError } from '@midwayjs/core'; +import { httpError, MidwayHttpError } from '@midwayjs/core'; import { PassportAuthenticator } from './authenticator'; import { AbstractStrategyWrapper, Strategy } from './strategy'; import { create as createReqMock } from './request'; @@ -39,15 +39,15 @@ export function PassportStrategy( this.passport.use(this.strategy); } if (this['serializeUser']) { - this.passport.serializeUser(this['serializeUser']); + this.passport.addSerializer(this['serializeUser']); } if (this['deserializeUser']) { - this.passport.deserializeUser(this['deserializeUser']); + this.passport.addDeserializer(this['deserializeUser']); } if (this['transformAuthInfo']) { - this.passport.transformAuthInfo(this['transformAuthInfo']); + this.passport.addInfoTransformer(this['transformAuthInfo']); } } @@ -113,7 +113,17 @@ export function PassportMiddleware( authOptions ); - const authenticateResult = await authenticate(req); + let authenticateResult; + try { + authenticateResult = await authenticate(req); + } catch (err) { + if (err instanceof MidwayHttpError) { + throw err; + } else { + // 如果验证流程里有错误,抛出一个 500 错误 + throw new httpError.InternalServerErrorError(err); + } + } // success if (authenticateResult.successResult) { @@ -190,7 +200,17 @@ export function PassportMiddleware( authOptions ); - const authenticateResult = await authenticate(req); + let authenticateResult; + try { + authenticateResult = await authenticate(req); + } catch (err) { + if (err instanceof MidwayHttpError) { + throw err; + } else { + // 如果验证流程里有错误,抛出一个 500 错误 + throw new httpError.InternalServerErrorError(err); + } + } // success if (authenticateResult.successResult) { diff --git a/packages/passport/src/passport/session.stratey.ts b/packages/passport/src/passport/session.stratey.ts index 4187d616c71..c6837dbb56c 100644 --- a/packages/passport/src/passport/session.stratey.ts +++ b/packages/passport/src/passport/session.stratey.ts @@ -38,7 +38,7 @@ export class SessionStrategy extends Strategy { */ authenticate(req, options) { if (!req.session) { - return new httpError.UnauthorizedError( + throw new httpError.UnauthorizedError( 'Login sessions require session support,please enable it.' ); } @@ -57,7 +57,7 @@ export class SessionStrategy extends Strategy { const paused = options.pauseStream ? pause(req) : null; this._deserializeUser(su, req, (err, user) => { if (err) { - return new httpError.UnauthorizedError(err.message); + throw new httpError.UnauthorizedError(err.message); } if (!user) { delete req.session[this.options.sessionUserProperty].user;