36 lines
1.1 KiB
C#
36 lines
1.1 KiB
C#
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.Options;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using CarParkValidationAPI.Models;
|
|
using CarParkValidationAPI.Interface;
|
|
|
|
|
|
namespace CarParkValidationAPI.Authorization
|
|
{
|
|
public class JwtMiddleware(RequestDelegate next, IOptions<AppSettings> appSettings)
|
|
{
|
|
private readonly AppSettings _appSettings = appSettings.Value;
|
|
|
|
public async Task Invoke(HttpContext context, IUserService userService, IJwtUtils jwtUtils)
|
|
{
|
|
|
|
string? token = context.Request.Headers.Authorization.FirstOrDefault()?.Split(" ").Last();
|
|
if (null == token)
|
|
context.Request.Cookies.TryGetValue("token", out token);
|
|
|
|
var user = jwtUtils.ValidateJwtToken(token);
|
|
if (user != null)
|
|
{
|
|
// attach user to context on successful jwt validation
|
|
//here to put in the real user
|
|
//TODO if you want to add information for User
|
|
context.Items["User"] = user;
|
|
}
|
|
|
|
await next(context);
|
|
}
|
|
}
|
|
}
|
|
|