From 1f2b3530ebeafa162fe4df41e691c33cb2ff0009 Mon Sep 17 00:00:00 2001 From: vman Date: Thu, 15 Dec 2022 01:45:03 -0500 Subject: JWK sigs, session cleanup v1 --- .../OAuth2SessionProvider.cs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'Libs/VNLib.Plugins.Essentials.Sessions.OAuth/OAuth2SessionProvider.cs') diff --git a/Libs/VNLib.Plugins.Essentials.Sessions.OAuth/OAuth2SessionProvider.cs b/Libs/VNLib.Plugins.Essentials.Sessions.OAuth/OAuth2SessionProvider.cs index d698c81..106029f 100644 --- a/Libs/VNLib.Plugins.Essentials.Sessions.OAuth/OAuth2SessionProvider.cs +++ b/Libs/VNLib.Plugins.Essentials.Sessions.OAuth/OAuth2SessionProvider.cs @@ -55,18 +55,20 @@ namespace VNLib.Plugins.Essentials.Sessions.OAuth private readonly IOauthSessionIdFactory factory; private readonly TokenStore TokenStore; - - public OAuth2SessionProvider(FBMClient client, int maxCacheItems, IOauthSessionIdFactory idFactory, DbContextOptions dbCtx) + private readonly uint MaxConnections; + + public OAuth2SessionProvider(FBMClient client, int maxCacheItems, uint maxConnections, IOauthSessionIdFactory idFactory, DbContextOptions dbCtx) : base(client, maxCacheItems) { factory = idFactory; TokenStore = new(dbCtx); + MaxConnections = maxConnections; } /// - protected override RemoteSession SessionCtor(string sessionId) => new OAuth2Session(sessionId, Client, BackgroundTimeout, InvlidatateCache); + protected override RemoteSession SessionCtor(string sessionId) => new OAuth2Session(sessionId, Client, BackgroundTimeout, InvalidatateCache); - private void InvlidatateCache(OAuth2Session session) + private void InvalidatateCache(OAuth2Session session) { lock (CacheLock) { @@ -91,6 +93,14 @@ namespace VNLib.Plugins.Essentials.Sessions.OAuth return SessionHandle.Empty; } + //Limit max number of waiting clients + if (WaitingConnections > MaxConnections) + { + //Set 503 for temporary unavail + entity.CloseResponse(System.Net.HttpStatusCode.ServiceUnavailable); + return new SessionHandle(null, FileProcessArgs.VirtualSkip, null); + } + //Recover the session RemoteSession session = await base.GetSessionAsync(entity, sessionId, cancellationToken); @@ -174,14 +184,14 @@ namespace VNLib.Plugins.Essentials.Sessions.OAuth /* - * Interval for remving expired tokens + * Interval for removing expired tokens */ /// async Task IIntervalScheduleable.OnIntervalAsync(ILogProvider log, CancellationToken cancellationToken) { //Calculate valid token time - DateTimeOffset validAfter = DateTimeOffset.UtcNow.Subtract(factory.SessionValidFor); + DateTime validAfter = DateTime.UtcNow.Subtract(factory.SessionValidFor); //Remove tokens from db store IReadOnlyCollection revoked = await TokenStore.CleanupExpiredTokensAsync(validAfter, cancellationToken); //exception list -- cgit