Skip to content

Commit

Permalink
fix(auth): Malformed JWTs no longer results in InternalServerError (#870
Browse files Browse the repository at this point in the history
)

<!--- Provide a general summary of your changes in the Title above -->

## Description

<!--- Describe your changes in detail -->
Some malformed tokens results in 500 errors in
`JwtSchemeSelectorMiddleware`

## Related Issue(s)

- #868 

## Verification

- [x] **Your** code builds clean without any errors or warnings
- [x] Manual testing done (required)
- [ ] Relevant automated test added (if you find this hard, leave it and
we'll help out)

## Documentation

- [ ] Documentation is updated (either in `docs`-directory, Altinnpedia
or a separate linked PR in
[altinn-studio-docs.](https://github.com/Altinn/altinn-studio-docs), if
applicable)
  • Loading branch information
oskogstad authored Jun 20, 2024
1 parent c9b50e9 commit 5f2f386
Showing 1 changed file with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.IdentityModel.Tokens.Jwt;
using FastEndpoints;
using FluentValidation.Results;

namespace Digdir.Domain.Dialogporten.WebApi.Common.Authentication;

Expand Down Expand Up @@ -33,9 +35,16 @@ public Task InvokeAsync(HttpContext context)
return _next(context);
}

var jwtToken = handler.ReadJwtToken(token);
context.Items[Constants.CurrentTokenIssuer] = jwtToken.Issuer;
return _next(context);
try
{
var jwtToken = handler.ReadJwtToken(token);
context.Items[Constants.CurrentTokenIssuer] = jwtToken.Issuer;
return _next(context);
}
catch (Exception)
{
return context.Response.SendErrorsAsync([new ValidationFailure("BearerToken", "Malformed token")]);
}
}
}

Expand Down

0 comments on commit 5f2f386

Please sign in to comment.