Skip to content

Commit

Permalink
fix: throw error when authenticate (#2141)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Jul 21, 2022
1 parent 75977d2 commit 730a282
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 55 deletions.
5 changes: 3 additions & 2 deletions packages/core/src/common/filterManager.ts
Expand Up @@ -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;
Expand Down
60 changes: 15 additions & 45 deletions packages/passport/src/passport/authenticator.ts
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
32 changes: 26 additions & 6 deletions 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';
Expand Down Expand Up @@ -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']);
}
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions packages/passport/src/passport/session.stratey.ts
Expand Up @@ -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.'
);
}
Expand All @@ -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;
Expand Down

0 comments on commit 730a282

Please sign in to comment.