Commit 0
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using GameAuthApi.Services;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace GameAuthApi.Middleware;
|
||||
|
||||
public class TokenBlacklistMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
|
||||
public TokenBlacklistMiddleware(RequestDelegate next)
|
||||
{
|
||||
_next = next;
|
||||
}
|
||||
|
||||
public async Task Invoke(HttpContext context, BlacklistService blacklistService)
|
||||
{
|
||||
var token = context.Request.Headers["Authorization"].FirstOrDefault()?.Replace("Bearer ", "");
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(token))
|
||||
{
|
||||
if (await blacklistService.IsTokenBlacklistedAsync(token))
|
||||
{
|
||||
context.Response.StatusCode = 401;
|
||||
await context.Response.WriteAsync("Token has been revoked");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
await _next(context);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user