Skip to content
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

Forward Passport's info #24

Closed
sroze opened this issue Nov 13, 2018 · 4 comments
Closed

Forward Passport's info #24

sroze opened this issue Nov 13, 2018 · 4 comments
Labels
enhancement New feature or request status: todo

Comments

@sroze
Copy link

sroze commented Nov 13, 2018

When using passport-http to provide some Basic authentication, I don't receive the Realm in the failure response and therefore I'm not prompted by the browser.

It is setting the realm as the failure reason. Because we ignore the info argument then it's never sent back to the client.

Any idea on how you'd see this working? I'm happy to do the PR.

@dreamdevil00
Copy link

Sometimes we want to know why the authenticate process failed.For example, if the jwt token expired, the error info would be stored in info argument. But it is ignored. We can not get it anymore.

@kamilmysliwiec

thoughts?

deathman92 added a commit to deathman92/passport that referenced this issue Feb 1, 2019
Some passport strategies require to pass arbitrary arguments to done callback. For example https://github.com/jaredhanson/passport-totp#configure-strategy requires to pass key and period. Or it can be info data for req.authInfo (related to nestjs#24).
Now one can return array from Strategy#validate method, like
```ts
async validate(payload: JwtPayload) {
    const user = await this.authService.validateUser(payload);
    if (!user) {
      throw new UnauthorizedException();
    }
    return [user, 'some info'];
  }
```
@kamilmysliwiec kamilmysliwiec added enhancement New feature or request status: todo labels Feb 3, 2019
@lutvianes
Copy link

For now, exception filter can be used to trigger Basic authentication prompt by browser. At least this code works for me.

@Catch(UnauthorizedException)
export class BasicAuthExceptionFilter extends BaseExceptionFilter<UnauthorizedException> {
    constructor(@Inject(HTTP_SERVER_REF) applicationRef?: HttpServer) {
        super(applicationRef)
    }

    catch(exception: UnauthorizedException, host: ArgumentsHost) {
        const ctx = host.switchToHttp();
        const response = ctx.getResponse();
        const request = ctx.getRequest();
        const status = exception.getStatus()

        if (request.authInfo) {
            response
                .status(status)
                .set('WWW-Authenticate', request.authInfo)
                .send()
        } else {
            super.catch(exception, host)
        }
    }
}

deathman92 added a commit to deathman92/passport that referenced this issue Apr 25, 2019
Some passport strategies require to pass arbitrary arguments to done callback. For example https://github.com/jaredhanson/passport-totp#configure-strategy requires to pass key and period. Or it can be info data for req.authInfo (related to nestjs#24).
Now one can return array from Strategy#validate method, like
```ts
async validate(payload: JwtPayload) {
    const user = await this.authService.validateUser(payload);
    if (!user) {
      throw new UnauthorizedException();
    }
    return [user, 'some info'];
  }
```
@kamilmysliwiec
Copy link
Member

Done #29. Published as 6.1.0

@Roaders
Copy link

Roaders commented May 20, 2020

Many many thanks for that code snippet @lutvianes - there's really not much help available when googling why basic auth does not work in nest and your code fixed it for me. The only bit that was missing for me was hooking the filter up in my controller:

    @Get('timeBasic')
    @UseGuards(BasicAuthGuard)
    @UseFilters(BasicAuthExceptionFilter)
    getTimeBasic() {
        return this.appService.getTime();
    }

And a different constructor param for the filter (I think that HTTP_SERVER_REF has been deprecated)

import { BaseExceptionFilter, HttpAdapterHost } from '@nestjs/core';

@Catch(UnauthorizedException)
export class BasicAuthExceptionFilter extends BaseExceptionFilter<UnauthorizedException> {
    constructor(adapterHost: HttpAdapterHost) {
        super(adapterHost.httpAdapter);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request status: todo
Projects
None yet
Development

No branches or pull requests

5 participants