using Microsoft.Extensions.Caching.Memory; using SharepointToolbox.Web.Core.Models; namespace SharepointToolbox.Web.Services.OAuth; public class OAuthFlowCache : IOAuthFlowCache { private readonly IMemoryCache _cache; public OAuthFlowCache(IMemoryCache cache) { _cache = cache; } public void StoreFlowState(string state, OAuthFlowState flowState) => _cache.Set($"oauth_state_{state}", flowState, TimeSpan.FromMinutes(10)); public OAuthFlowState? GetAndRemoveFlowState(string state) { var key = $"oauth_state_{state}"; var value = _cache.Get(key); if (value is not null) _cache.Remove(key); return value; } public void StoreTokens(string tokenKey, SessionTokens tokens) => _cache.Set($"oauth_tokens_{tokenKey}", tokens, TimeSpan.FromMinutes(2)); public SessionTokens? GetAndRemoveTokens(string tokenKey) { var key = $"oauth_tokens_{tokenKey}"; var value = _cache.Get(key); if (value is not null) _cache.Remove(key); return value; } public void StoreRegistrationResult(string key, AppRegistrationResult result) => _cache.Set($"oauth_reg_{key}", result, TimeSpan.FromMinutes(5)); public AppRegistrationResult? GetAndRemoveRegistrationResult(string key) { var cacheKey = $"oauth_reg_{key}"; var value = _cache.Get(cacheKey); if (value is not null) _cache.Remove(cacheKey); return value; } }