2022-02-14 01:51:37 +01:00
|
|
|
using Blazored.LocalStorage;
|
2026-10-03 15:26:21 +02:00
|
|
|
using decePubClient.Helpers;
|
2022-02-14 01:51:37 +01:00
|
|
|
using decePubClient.Models;
|
|
|
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
|
|
|
|
|
namespace decePubClient.Services
|
|
|
|
|
{
|
2026-10-03 15:26:21 +02:00
|
|
|
public class TokenAuthStateProvider(ILocalStorageService storage, IStorage dbStorage, ILogger<TokenAuthStateProvider> logger)
|
|
|
|
|
: AuthenticationStateProvider
|
2022-02-14 01:51:37 +01:00
|
|
|
{
|
|
|
|
|
public void SetToken(/*string token, long expirationTicks = default*/)
|
|
|
|
|
{
|
|
|
|
|
//AuthData = await Storage.GetItemAsync<AuthData>(nameof(AuthData));
|
|
|
|
|
//if (string.IsNullOrEmpty(token))
|
|
|
|
|
//{
|
|
|
|
|
// Logger.LogInformation($"set null({nameof(SetToken)})");
|
|
|
|
|
// AuthData.Token = null;
|
|
|
|
|
// AuthData.TokenExpiration = null;
|
|
|
|
|
//}
|
|
|
|
|
//else
|
|
|
|
|
//{
|
|
|
|
|
// AuthData.Token = token;
|
|
|
|
|
// AuthData.TokenExpiration = expirationTicks;
|
|
|
|
|
//}
|
|
|
|
|
//await Storage.SetItemAsync(nameof(AuthData), AuthData);
|
|
|
|
|
|
|
|
|
|
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-03 15:26:21 +02:00
|
|
|
/// <summary>The stored token while it is still valid, otherwise null.</summary>
|
|
|
|
|
public async Task<string> GetToken(CancellationToken cancellationToken = default)
|
2022-02-14 01:51:37 +01:00
|
|
|
{
|
2026-10-03 15:26:21 +02:00
|
|
|
var authData = await storage.GetItemAsync<AuthData>(nameof(AuthData), cancellationToken);
|
|
|
|
|
if (string.IsNullOrEmpty(authData?.Token) || authData.TokenExpiration is not { } expiration || expiration <= DateTime.UtcNow.Ticks)
|
|
|
|
|
return null;
|
|
|
|
|
return authData.Token;
|
2022-02-14 01:51:37 +01:00
|
|
|
}
|
|
|
|
|
|
2026-10-03 15:26:21 +02:00
|
|
|
public async Task<bool> IsAuthenticatedAsync(CancellationToken cancellationToken = default) =>
|
|
|
|
|
await GetToken(cancellationToken) is not null;
|
|
|
|
|
|
|
|
|
|
public async Task LogoutAsync(bool deleteDb = false, CancellationToken cancellationToken = default)
|
2022-02-14 01:51:37 +01:00
|
|
|
{
|
2026-10-03 15:26:21 +02:00
|
|
|
logger.LogInformation($"set null({nameof(LogoutAsync)})");
|
2022-02-14 01:51:37 +01:00
|
|
|
//await Storage.RemoveItemAsync(nameof(PrivateCacheData));
|
2026-10-03 15:26:21 +02:00
|
|
|
await storage.RemoveItemAsync(nameof(AuthData), cancellationToken);
|
2022-02-14 01:51:37 +01:00
|
|
|
if (deleteDb)
|
2026-10-03 15:26:21 +02:00
|
|
|
await dbStorage.RemoveAll(includeClientLogs: true, cancellationToken: cancellationToken);
|
2022-02-14 01:51:37 +01:00
|
|
|
|
|
|
|
|
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
|
|
|
|
|
{
|
2026-10-03 15:26:21 +02:00
|
|
|
var authData = await storage.GetItemAsync<AuthData>(nameof(AuthData));
|
|
|
|
|
if (string.IsNullOrEmpty(authData?.Token))
|
2022-02-14 01:51:37 +01:00
|
|
|
{
|
2026-10-03 15:26:21 +02:00
|
|
|
logger.LogInformation($"set null({nameof(GetAuthenticationStateAsync)})");
|
2022-02-14 01:51:37 +01:00
|
|
|
return new(new());
|
|
|
|
|
}
|
|
|
|
|
var claims = new List<Claim>
|
|
|
|
|
{
|
2026-10-03 15:26:21 +02:00
|
|
|
new(ClaimTypes.UserData, JsonSerializer.Serialize(authData.User, SUtility.DefaultSerializer))
|
2022-02-14 01:51:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//claims.Add(new(Policies.IsUser, (AuthData.User.Policies.Contains(Policies.IsUser)).ToString().ToLower()));
|
|
|
|
|
//claims.Add(new(Policies.UserPlus, (AuthData.User.Policies.Contains(Policies.UserPlus)).ToString().ToLower()));
|
|
|
|
|
//claims.Add(new(Policies.IsAdmin, (AuthData.User.Policies.Contains(Policies.IsAdmin)).ToString().ToLower()));
|
|
|
|
|
|
|
|
|
|
var identity = new ClaimsIdentity(claims, "jwt");
|
|
|
|
|
return new(new(identity));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|