diff options
author | vman <public@vaughnnugent.com> | 2022-11-30 14:58:14 -0500 |
---|---|---|
committer | vman <public@vaughnnugent.com> | 2022-11-30 14:58:14 -0500 |
commit | a0d5a8d40de9806e21e64475e3297a2a84effe22 (patch) | |
tree | 510ffabe5a8617e7a9388641bf5aefb2fd51742d /Libs/VNLib.Plugins.Essentials.Sessions.OAuth | |
parent | 71ad09dda9ac67ef481d115fb9544dcd56834f22 (diff) |
Project cleanup + analyzer updates
Diffstat (limited to 'Libs/VNLib.Plugins.Essentials.Sessions.OAuth')
6 files changed, 115 insertions, 23 deletions
diff --git a/Libs/VNLib.Plugins.Essentials.Sessions.OAuth/Endpoints/AccessTokenEndpoint.cs b/Libs/VNLib.Plugins.Essentials.Sessions.OAuth/Endpoints/AccessTokenEndpoint.cs index 5c09697..a159456 100644 --- a/Libs/VNLib.Plugins.Essentials.Sessions.OAuth/Endpoints/AccessTokenEndpoint.cs +++ b/Libs/VNLib.Plugins.Essentials.Sessions.OAuth/Endpoints/AccessTokenEndpoint.cs @@ -49,7 +49,7 @@ namespace VNLib.Plugins.Essentials.Sessions.OAuth.Endpoints { private readonly CreateTokenImpl CreateToken; - private readonly Applications Applications; + private readonly ApplicationStore Applications; private readonly Task<JsonDocument?> JWTVerificationKey; diff --git a/Libs/VNLib.Plugins.Essentials.Sessions.OAuth/Endpoints/RevocationEndpoint.cs b/Libs/VNLib.Plugins.Essentials.Sessions.OAuth/Endpoints/RevocationEndpoint.cs index 3c65056..d981f69 100644 --- a/Libs/VNLib.Plugins.Essentials.Sessions.OAuth/Endpoints/RevocationEndpoint.cs +++ b/Libs/VNLib.Plugins.Essentials.Sessions.OAuth/Endpoints/RevocationEndpoint.cs @@ -34,13 +34,13 @@ namespace VNLib.Plugins.Essentials.Sessions.OAuth.Endpoints /// An OAuth2 authorized endpoint for revoking the access token /// held by the current connection /// </summary> - [ConfigurationName("oauth2")] + [ConfigurationName("o2_revocation_endpoint")] internal class RevocationEndpoint : O2EndpointBase { public RevocationEndpoint(PluginBase pbase, IReadOnlyDictionary<string, JsonElement> config) { - string? path = config["revocation_path"].GetString(); + string? path = config["path"].GetString(); InitPathAndLog(path, pbase.Log); } diff --git a/Libs/VNLib.Plugins.Essentials.Sessions.OAuth/O2AuthenticationPluginEntry.cs b/Libs/VNLib.Plugins.Essentials.Sessions.OAuth/O2AuthenticationPluginEntry.cs new file mode 100644 index 0000000..4a48f8b --- /dev/null +++ b/Libs/VNLib.Plugins.Essentials.Sessions.OAuth/O2AuthenticationPluginEntry.cs @@ -0,0 +1,60 @@ +/* +* Copyright (c) 2022 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.Plugins.Essentials.Sessions.OAuth +* File: O2AuthenticationPluginEntry.cs +* +* O2AuthenticationPluginEntry.cs is part of VNLib.Plugins.Essentials.Sessions.OAuth which is part of the larger +* VNLib collection of libraries and utilities. +* +* VNLib.Plugins.Essentials.Sessions.OAuth is free software: you can redistribute it and/or modify +* it under the terms of the GNU Affero General Public License as +* published by the Free Software Foundation, either version 3 of the +* License, or (at your option) any later version. +* +* VNLib.Plugins.Essentials.Sessions.OAuth is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Affero General Public License for more details. +* +* You should have received a copy of the GNU Affero General Public License +* along with this program. If not, see https://www.gnu.org/licenses/. +*/ + +using VNLib.Utils.Logging; +using VNLib.Plugins.Essentials.Sessions.Runtime; + + +namespace VNLib.Plugins.Essentials.Sessions.Oauth +{ + public sealed class O2AuthenticationPluginEntry : PluginBase + { + public override string PluginName => "Essentials.Oauth.Authentication"; + + private readonly O2SessionProviderEntry SessionProvider = new(); + + protected override void OnLoad() + { + try + { + //Load the session provider, that will only load the endpoints + (SessionProvider as IRuntimeSessionProvider).Load(this, Log); + } + catch(KeyNotFoundException kne) + { + Log.Error("Missing required configuration keys {err}", kne.Message); + } + } + + protected override void OnUnLoad() + { + Log.Information("Plugin unloaded"); + } + + protected override void ProcessHostCommand(string cmd) + { + throw new NotImplementedException(); + } + } +}
\ No newline at end of file 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<SessionHandle> ISessionProvider.GetSessionAsync(IHttpEvent entity, CancellationToken cancellationToken) @@ -65,23 +67,30 @@ namespace VNLib.Plugins.Essentials.Sessions.Oauth IReadOnlyDictionary<string, JsonElement> 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<JsonDocument?> 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<RevocationEndpoint>(); + //route auth endpoint + plugin.Route(authEp); + } + + //Optional revocation endpoint + if (plugin.HasConfigForType<RevocationEndpoint>()) + { + //Route revocation endpoint + plugin.Route<RevocationEndpoint>(); + } //Run - _ = CacheWokerDoWorkAsync(plugin, localized, cacheConfig, oauth2Config); + _ = plugin.DeferTask(() => CacheWokerDoWorkAsync(plugin, localized, cacheConfig, oauth2Config), 100); + } private async Task<IOAuth2TokenResult?> 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"); diff --git a/Libs/VNLib.Plugins.Essentials.Sessions.OAuth/OAuth2SessionProvider.cs b/Libs/VNLib.Plugins.Essentials.Sessions.OAuth/OAuth2SessionProvider.cs index 5f9fc7e..d698c81 100644 --- a/Libs/VNLib.Plugins.Essentials.Sessions.OAuth/OAuth2SessionProvider.cs +++ b/Libs/VNLib.Plugins.Essentials.Sessions.OAuth/OAuth2SessionProvider.cs @@ -27,6 +27,7 @@ using System.Net; using Microsoft.EntityFrameworkCore; +using VNLib.Net.Http; using VNLib.Utils; using VNLib.Utils.Logging; using VNLib.Data.Caching; @@ -37,8 +38,6 @@ using VNLib.Plugins.Essentials.Oauth; using VNLib.Plugins.Essentials.Oauth.Tokens; using VNLib.Plugins.Essentials.Oauth.Applications; using VNLib.Plugins.Extensions.Loading.Events; -using VNLib.Net.Http.Core; -using VNLib.Net.Http; namespace VNLib.Plugins.Essentials.Sessions.OAuth { @@ -200,8 +199,10 @@ namespace VNLib.Plugins.Essentials.Sessions.OAuth {} catch (Exception ex) { - errors ??= new(); - errors.Add(ex); + errors = new() + { + ex + }; } } if (errors?.Count > 0) diff --git a/Libs/VNLib.Plugins.Essentials.Sessions.OAuth/VNLib.Plugins.Essentials.Sessions.OAuth.csproj b/Libs/VNLib.Plugins.Essentials.Sessions.OAuth/VNLib.Plugins.Essentials.Sessions.OAuth.csproj index 4cfcd86..d75a1c0 100644 --- a/Libs/VNLib.Plugins.Essentials.Sessions.OAuth/VNLib.Plugins.Essentials.Sessions.OAuth.csproj +++ b/Libs/VNLib.Plugins.Essentials.Sessions.OAuth/VNLib.Plugins.Essentials.Sessions.OAuth.csproj @@ -4,20 +4,38 @@ <TargetFramework>net6.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> - <PlatformTarget>x64</PlatformTarget> - <GenerateDocumentationFile>False</GenerateDocumentationFile> + <GenerateDocumentationFile>True</GenerateDocumentationFile> <Authors>Vaughn Nugent</Authors> <Copyright>Copyright © 2022 Vaughn Nugent</Copyright> <EnableDynamicLoading>true</EnableDynamicLoading> + <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> + <Version>1.0.1.1</Version> + <PackageProjectUrl>https://www.vaughnugent.com</PackageProjectUrl> + <AnalysisLevel>latest-all</AnalysisLevel> </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> + <Deterministic>False</Deterministic> + </PropertyGroup> + + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'"> + <Deterministic>False</Deterministic> + </PropertyGroup> + <Target Name="PostBuild" AfterTargets="PostBuildEvent"> <Exec Command="start xcopy "$(TargetDir)" "F:\Programming\Web Plugins\DevPlugins\RuntimeAssets\$(TargetName)" /E /Y /R" /> </Target> - + <Target Name="PreBuild" BeforeTargets="PreBuildEvent"> + <Exec Command="erase "F:\Programming\Web Plugins\DevPlugins\RuntimeAssets\$(TargetName)" /q > nul" /> + </Target> <ItemGroup> + <ProjectReference Include="..\..\..\..\VNLib\Http\VNLib.Net.Http.csproj" /> + <ProjectReference Include="..\..\..\..\VNLib\Plugins\src\VNLib.Plugins.csproj" /> + <ProjectReference Include="..\..\..\..\VNLib\Utils\src\VNLib.Utils.csproj" /> + <ProjectReference Include="..\..\..\DataCaching\VNLib.Data.Caching.Extensions\VNLib.Data.Caching.Extensions.csproj" /> + <ProjectReference Include="..\..\..\DataCaching\VNLib.Data.Caching\src\VNLib.Data.Caching.csproj" /> <ProjectReference Include="..\..\..\Extensions\VNLib.Plugins.Extensions.Loading.Sql\VNLib.Plugins.Extensions.Loading.Sql.csproj" /> <ProjectReference Include="..\..\..\Extensions\VNLib.Plugins.Extensions.Loading\VNLib.Plugins.Extensions.Loading.csproj" /> <ProjectReference Include="..\..\..\Extensions\VNLib.Plugins.Extensions.Validation\VNLib.Plugins.Extensions.Validation.csproj" /> |