69 lines
1.8 KiB
C#
69 lines
1.8 KiB
C#
using IronServices.Client;
|
|
|
|
namespace IronServices.Maui.Services;
|
|
|
|
/// <summary>
|
|
/// MAUI implementation of ITokenStorage using SecureStorage.
|
|
/// Securely stores authentication tokens on the device.
|
|
/// </summary>
|
|
public class MauiSecureTokenStorage : ITokenStorage
|
|
{
|
|
private const string SessionTokenKey = "ironservices_session_token";
|
|
private const string TokenExpiryKey = "ironservices_token_expiry";
|
|
|
|
public async Task SaveTokenAsync(string token, DateTime? expiresAt)
|
|
{
|
|
try
|
|
{
|
|
await SecureStorage.Default.SetAsync(SessionTokenKey, token);
|
|
if (expiresAt.HasValue)
|
|
{
|
|
await SecureStorage.Default.SetAsync(TokenExpiryKey, expiresAt.Value.ToString("O"));
|
|
}
|
|
else
|
|
{
|
|
SecureStorage.Default.Remove(TokenExpiryKey);
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
// SecureStorage may not be available on all platforms
|
|
}
|
|
}
|
|
|
|
public async Task<(string? Token, DateTime? ExpiresAt)> GetTokenAsync()
|
|
{
|
|
try
|
|
{
|
|
var token = await SecureStorage.Default.GetAsync(SessionTokenKey);
|
|
DateTime? expiresAt = null;
|
|
|
|
var expiryStr = await SecureStorage.Default.GetAsync(TokenExpiryKey);
|
|
if (DateTime.TryParse(expiryStr, out var expiry))
|
|
{
|
|
expiresAt = expiry;
|
|
}
|
|
|
|
return (token, expiresAt);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return (null, null);
|
|
}
|
|
}
|
|
|
|
public Task ClearTokenAsync()
|
|
{
|
|
try
|
|
{
|
|
SecureStorage.Default.Remove(SessionTokenKey);
|
|
SecureStorage.Default.Remove(TokenExpiryKey);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
// Ignore
|
|
}
|
|
return Task.CompletedTask;
|
|
}
|
|
}
|