-
-
Notifications
You must be signed in to change notification settings - Fork 292
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
[QUESTION] Override passport-local's "Bad Request" #776
Comments
Ha yes when a written the unit test for this example, I expected a 403 instead of 400. @Post("/login")
@ValidateCredential()
@Authenticate("login", {failWithError: false})
@Returns(User)
@Responses(400, {description: "Validation error"})
login(@Req() req: Req, @BodyParams() credentials: Credentials) {
// FACADE
return req.user;
} Decorator and middleware: function ValidateCredential() {
return applyDecorators(
UseBefore(ValidateCredentialMiddleware),
Responses(403, {description: "Missing credential"})
)
}
@Middleware()
class ValidateCredentalMiddleware() {
use(@BodyParams() credentials: any ) {
if (!credentials.email && !credentials.password){
throw new Forbidden("missing credentials")
}
}
} Seconds solution is to implement create a class inherited from Passport local statregy: const {lookup} = require('passport-local/lib/utils')
export class CustomLocalStrategy extends Strategy {
authenticate(req: any, options: any) {
options = options || {};
const username = lookup(req.body, this._usernameField) || lookup(req.query, this._usernameField);
const password = lookup(req.body, this._passwordField) || lookup(req.query, this._passwordField);
if (!username || !password) {
return this.fail({ message: options.badRequestMessage || 'Missing credentials' }, 403);
}
return super.authenticate(req, options)
}
} Then, add this custom strategy to the LoginLocalProtocol configuration :) I hope theses solutions will help you :) Romain |
Hello @Romakita , thanks for the response, I ended up with this: import { applyDecorators } from "@tsed/core";
import { UseBefore, Middleware, Req} from "@tsed/common";
import { Responses } from "@tsed/swagger";
import { Forbidden } from "ts-httpexceptions";
export function ValidateCredential() {
return applyDecorators(
UseBefore(ValidateCredentialMiddleware),
Responses(403, { description: "Missing credentials." })
)
}
@Middleware()
export class ValidateCredentialMiddleware {
// `req.user` is set and unset by /login and /logout respectively
use(@Req() req: Req) {
if (!req.user) {
throw new Forbidden("Missing credentials.");
}
}
}
// contollers/UserControlller.ts
@Get("/me")
@ValidateCredential()
// @Authorize()
async me(): Promise<User> {
//...
} For routes that need authentication. For About the protected routes: it is a shame, I thought that |
For me the Authorize decorator should protect your route. Here is the implementation of the middleware:
The middleware is automatically added by |
Yeah, using It is a simple setup like these examples: From my understanding:
should throw "401 Unauthorized", for I have configured the passport protocol, instead of 400. It's either a bug or has something to do with this: Side note: the correct response is |
Hello @RickStanley |
tl;td If it's too much of a hassle, you can close this issue, as the custom solution fits my need. My initial suggestion was that maybe either But after some reading through passportjs' issues, I found that many people are trying to overwrite the "Bad Request", as the message is not helpful at all as is, which of course you've alreay provided a solution for this, and I've tweaked to fit my need. Finally, I was going to close this, but since you asked if there was something else to do, I went to look for some similar issues. I found this, which leads to my initial thought that maybe But then again, this current solution is enough, though I really would like to use In any case, I think this was a good issue, I've seen this kind question in a lot of places like NestJS and FoalTS. I hope someone find this helpful. Refs: ¹: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/401 |
Hi, is it possible to overwrite
passport-local
's default response when the authentication parameters are not present? Because, as of now, it only reponds with "Bad Request" which isn't enough for the client, I would rather have something like "Missing credentials" and add403 Missing credentials
(or smth) for routes that are protected.In you example there is an missmatch with the described unit test, it expects 403 but clearly you knew it wasn't going to return 403 and wrote 400 instead:
https://github.com/TypedProject/tsed-example-passportjs/blob/d9214788c1dc43a651a5e609302a6c0babe6a4f4/test/integration/controllers/auth/auth.spec.ts#L18-L25
Anyway, some people pointed out that it was due to the hardcoded message in the
passport-local
itself and/or missingbody-parser
dependency (which my project doesn't)The text was updated successfully, but these errors were encountered: