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

Restart OIDC Auth Flow on Invalid State #2119

Merged
merged 1 commit into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/backend/src/authn/authn.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export class AuthnController {

@Get('oidc/callback')
@UseGuards(AuthGuard('oidc'))
@UseFilters(new AuthenticationExceptionFilter())
async getUserFromOIDC(@Req() req: Request): Promise<void> {
const session = await this.authnService.login(req.user as User);
await this.setSessionCookies(req, session);
Expand Down
9 changes: 9 additions & 0 deletions apps/backend/src/filters/authentication-exception.filter.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import {ArgumentsHost, Catch, ExceptionFilter} from '@nestjs/common';
import _ from 'lodash';
import {ConfigService} from '../config/config.service';

@Catch(Error)
export class AuthenticationExceptionFilter implements ExceptionFilter {
configService = new ConfigService();
catch(exception: Error, host: ArgumentsHost): void {
const ctx = host.switchToHttp();
const request = ctx.getRequest();
const response = ctx.getResponse();
// Restart OIDC auth flow if state fails to verify, this is a known bug with passport-openidconnect that occours sometimes when logging in.
if (
_.get(request, 'authInfo.message') ===
'Unable to verify authorization request state.'
) {
return response.redirect(301, '/authn/oidc');
}
response.cookie('authenticationError', exception.message, {
secure: this.configService.isInProductionMode()
});
Expand Down