2023-02-01 20:49:02 +00:00
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
using ImageBoardServerApp.Data;
|
|
|
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
|
|
|
|
|
|
|
|
|
|
namespace ImageBoardServerApp.Auth;
|
|
|
|
|
|
|
|
|
|
public class CustomAuthenticationStateProvider : AuthenticationStateProvider
|
|
|
|
|
{
|
2023-02-12 13:55:37 +00:00
|
|
|
|
private readonly ProtectedLocalStorage _sessionStorage;
|
2023-06-12 16:51:25 +00:00
|
|
|
|
|
2023-02-01 20:49:02 +00:00
|
|
|
|
private ClaimsPrincipal _anonymous = new ClaimsPrincipal(new ClaimsIdentity());
|
|
|
|
|
|
2023-02-12 13:55:37 +00:00
|
|
|
|
public CustomAuthenticationStateProvider(ProtectedLocalStorage sessionStorage)
|
2023-02-01 20:49:02 +00:00
|
|
|
|
{
|
|
|
|
|
_sessionStorage = sessionStorage;
|
|
|
|
|
}
|
2023-06-12 16:51:25 +00:00
|
|
|
|
|
2023-02-01 20:49:02 +00:00
|
|
|
|
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var userSessionStorageResult = await _sessionStorage.GetAsync<UserData>("UserSession");
|
|
|
|
|
var userSession = userSessionStorageResult.Success ? userSessionStorageResult.Value : null;
|
|
|
|
|
if (userSession == null)
|
|
|
|
|
return await Task.FromResult(new AuthenticationState(_anonymous));
|
|
|
|
|
var claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>
|
|
|
|
|
{
|
2023-02-02 07:15:43 +00:00
|
|
|
|
//new Claim(ClaimTypes.Email, userSession.Email),
|
2023-06-12 18:46:44 +00:00
|
|
|
|
new Claim(ClaimTypes.Name, userSession.Email),
|
2023-02-01 20:49:02 +00:00
|
|
|
|
new Claim(ClaimTypes.Role, userSession.Role)
|
|
|
|
|
}, "CustomAuth"));
|
2023-06-12 18:46:44 +00:00
|
|
|
|
return await Task.FromResult(new AuthenticationState(claimsPrincipal));
|
2023-02-01 20:49:02 +00:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2023-06-12 18:46:44 +00:00
|
|
|
|
Console.WriteLine("Resorting to Anon");
|
|
|
|
|
return await Task.FromResult(new AuthenticationState(_anonymous));
|
2023-02-01 20:49:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task UpdateAuthenticationStateAsync(UserData session)
|
|
|
|
|
{
|
|
|
|
|
ClaimsPrincipal claimsPrincipal;
|
|
|
|
|
|
|
|
|
|
if (session != null)
|
|
|
|
|
{
|
|
|
|
|
await _sessionStorage.SetAsync("UserSession", session);
|
|
|
|
|
claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>
|
|
|
|
|
{
|
2023-06-12 18:46:44 +00:00
|
|
|
|
new Claim(ClaimTypes.Name, session.Email),
|
|
|
|
|
new Claim(ClaimTypes.Role, session.Role)
|
2023-02-01 20:49:02 +00:00
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await _sessionStorage.DeleteAsync("UserSession");
|
|
|
|
|
claimsPrincipal = _anonymous;
|
|
|
|
|
}
|
2023-06-12 16:51:25 +00:00
|
|
|
|
|
2023-02-01 20:49:02 +00:00
|
|
|
|
NotifyAuthenticationStateChanged(Task.FromResult(new AuthenticationState(claimsPrincipal)));
|
|
|
|
|
}
|
|
|
|
|
}
|