From a0d5a8d40de9806e21e64475e3297a2a84effe22 Mon Sep 17 00:00:00 2001 From: vman Date: Wed, 30 Nov 2022 14:58:14 -0500 Subject: Project cleanup + analyzer updates --- .../O2SessionProviderEntry.cs | 39 ++++++++++++++-------- 1 file changed, 26 insertions(+), 13 deletions(-) (limited to 'Libs/VNLib.Plugins.Essentials.Sessions.OAuth/O2SessionProviderEntry.cs') diff --git a/Libs/VNLib.Plugins.Essentials.Sessions.OAuth/O2SessionProviderEntry.cs b/Libs/VNLib.Plugins.Essentials.Sessions.OAuth/O2SessionProviderEntry.cs index e7c7f29..07b6530 100644 --- a/Libs/VNLib.Plugins.Essentials.Sessions.OAuth/O2SessionProviderEntry.cs +++ b/Libs/VNLib.Plugins.Essentials.Sessions.OAuth/O2SessionProviderEntry.cs @@ -27,6 +27,7 @@ using System.Text.Json; using VNLib.Net.Http; using VNLib.Utils.Logging; using VNLib.Utils.Extensions; +using VNLib.Plugins.Essentials.Oauth.Tokens; using VNLib.Plugins.Essentials.Oauth.Applications; using VNLib.Plugins.Essentials.Sessions.OAuth; using VNLib.Plugins.Essentials.Sessions.OAuth.Endpoints; @@ -35,10 +36,11 @@ using VNLib.Plugins.Extensions.Loading.Routing; using VNLib.Plugins.Extensions.Loading.Sql; using VNLib.Plugins.Extensions.Loading.Events; using VNLib.Plugins.Essentials.Sessions.Runtime; -using VNLib.Plugins.Essentials.Oauth.Tokens; +using VNLib.Data.Caching.Extensions; namespace VNLib.Plugins.Essentials.Sessions.Oauth { + public sealed class O2SessionProviderEntry : IRuntimeSessionProvider { const string VNCACHE_CONFIG_KEY = "vncache"; @@ -49,7 +51,7 @@ namespace VNLib.Plugins.Essentials.Sessions.Oauth bool IRuntimeSessionProvider.CanProcess(IHttpEvent entity) { //If authorization header is set try to process as oauth2 session - return entity.Server.Headers.HeaderSet(System.Net.HttpRequestHeader.Authorization); + return _sessions != null && entity.Server.Headers.HeaderSet(System.Net.HttpRequestHeader.Authorization); } ValueTask ISessionProvider.GetSessionAsync(IHttpEvent entity, CancellationToken cancellationToken) @@ -65,23 +67,30 @@ namespace VNLib.Plugins.Essentials.Sessions.Oauth IReadOnlyDictionary oauth2Config = plugin.GetConfig(OAUTH2_CONFIG_KEY); - string tokenEpPath = oauth2Config["token_path"].GetString() ?? throw new KeyNotFoundException($"Missing required 'token_path' in '{OAUTH2_CONFIG_KEY}' config"); - //Optional application jwt token Task jwtTokenSecret = plugin.TryGetSecretAsync("application_token_key") - .ContinueWith(static t => t.Result == null ? null : JsonDocument.Parse(t.Result)); + .ContinueWith(static t => t.Result == null ? null : JsonDocument.Parse(t.Result), TaskScheduler.Default); - //Init auth endpoint - AccessTokenEndpoint authEp = new(tokenEpPath, plugin, CreateTokenDelegateAsync, jwtTokenSecret); + //Access token endpoint is optional + if (oauth2Config.TryGetValue("token_path", out JsonElement el)) + { + //Init auth endpoint + AccessTokenEndpoint authEp = new(el.GetString()!, plugin, CreateTokenDelegateAsync, jwtTokenSecret); - //route auth endpoint - plugin.Route(authEp); - - //Route revocation endpoint - plugin.Route(); + //route auth endpoint + plugin.Route(authEp); + } + + //Optional revocation endpoint + if (plugin.HasConfigForType()) + { + //Route revocation endpoint + plugin.Route(); + } //Run - _ = CacheWokerDoWorkAsync(plugin, localized, cacheConfig, oauth2Config); + _ = plugin.DeferTask(() => CacheWokerDoWorkAsync(plugin, localized, cacheConfig, oauth2Config), 100); + } private async Task CreateTokenDelegateAsync(HttpEntity entity, UserApplication app, CancellationToken cancellation) @@ -133,6 +142,10 @@ namespace VNLib.Plugins.Essentials.Sessions.Oauth { localized.Error("Missing required configuration variable for VnCache client: {0}", e.Message); } + catch(FBMServerNegiationException fne) + { + localized.Error("Failed to negotiate connection with cache server {reason}", fne.Message); + } catch (Exception ex) { localized.Error(ex, "Cache client error occured in session provider"); -- cgit