using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage; using SharepointToolbox.Web.Core.Models; namespace SharepointToolbox.Web.Services.Session; public class SessionCredentialStore : ISessionCredentialStore { private const string Key = "sp-session-tokens"; private readonly ProtectedSessionStorage _storage; public SessionCredentialStore(ProtectedSessionStorage storage) { _storage = storage; } public async Task GetAsync() { try { var result = await _storage.GetAsync(Key); return result.Success ? result.Value : null; } catch { return null; } } public async Task SetAsync(SessionTokens tokens) => await _storage.SetAsync(Key, tokens); public async Task UpdateRefreshTokenAsync(string newRefreshToken) { var tokens = await GetAsync(); if (tokens is null) return; tokens.RefreshToken = newRefreshToken; await _storage.SetAsync(Key, tokens); } public async Task ClearAsync() => await _storage.DeleteAsync(Key); public async Task HasCredentialsAsync() { var tokens = await GetAsync(); return tokens is not null && !string.IsNullOrEmpty(tokens.RefreshToken); } }