From a5f9e67647ec63f9577fc282d7d1a58054251f79 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Sat, 15 Jun 2024 21:53:00 +0300 Subject: [PATCH] feat: add middleware for revocated tokens --- .../Middleware/JwtRevocationMiddleware.cs | 23 +++++++++++++++++++ Endpoint/Program.cs | 1 + 2 files changed, 24 insertions(+) create mode 100644 Endpoint/Middleware/JwtRevocationMiddleware.cs diff --git a/Endpoint/Middleware/JwtRevocationMiddleware.cs b/Endpoint/Middleware/JwtRevocationMiddleware.cs new file mode 100644 index 0000000..97818c7 --- /dev/null +++ b/Endpoint/Middleware/JwtRevocationMiddleware.cs @@ -0,0 +1,23 @@ +using Microsoft.AspNetCore.Http; +using Mirea.Api.Security.Common.Interfaces; +using System.Threading.Tasks; + +namespace Mirea.Api.Endpoint.Middleware; + +public class JwtRevocationMiddleware(RequestDelegate next) +{ + public async Task Invoke(HttpContext context, IRevokedToken revokedTokenStore) + { + if (context.Request.Headers.ContainsKey("Authorization")) + { + var token = context.Request.Headers.Authorization.ToString().Replace("Bearer ", ""); + if (await revokedTokenStore.IsTokenRevokedAsync(token)) + { + context.Response.StatusCode = StatusCodes.Status401Unauthorized; + return; + } + } + + await next(context); + } +} \ No newline at end of file diff --git a/Endpoint/Program.cs b/Endpoint/Program.cs index cf016f4..a904e98 100644 --- a/Endpoint/Program.cs +++ b/Endpoint/Program.cs @@ -95,6 +95,7 @@ public class Program app.UseMiddleware(); app.UseMiddleware(); + app.UseMiddleware(); app.UseHttpsRedirection();