diff --git a/EnvelopeGenerator.API/Program.cs b/EnvelopeGenerator.API/Program.cs index 658995e7..f2478083 100644 --- a/EnvelopeGenerator.API/Program.cs +++ b/EnvelopeGenerator.API/Program.cs @@ -206,6 +206,19 @@ try context.Token = cookieToken; } + return Task.CompletedTask; + }, + OnTokenValidated = context => + { + var paths = context.Request.Path.Value?.Split('/', StringSplitOptions.RemoveEmptyEntries); + var envelopeKey = paths?.LastOrDefault(); + + var sub = context.Principal?.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier)?.Value + ?? context.Principal?.FindFirst("sub")?.Value; + + if (envelopeKey is null || sub != envelopeKey) + context.Fail("Envelope key in the path does not match the token subject."); + return Task.CompletedTask; } };