33 lines
875 B
C#
33 lines
875 B
C#
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);
|
|
}
|
|
} |