From e77477b81e5623502b19db0fb29d4ea88c26b934 Mon Sep 17 00:00:00 2001 From: vnugent Date: Sat, 8 Apr 2023 16:43:52 -0400 Subject: Passwords singlton and user-loading --- .../src/LoadingExtensions.cs | 111 +-------------------- 1 file changed, 2 insertions(+), 109 deletions(-) (limited to 'lib/VNLib.Plugins.Extensions.Loading/src/LoadingExtensions.cs') diff --git a/lib/VNLib.Plugins.Extensions.Loading/src/LoadingExtensions.cs b/lib/VNLib.Plugins.Extensions.Loading/src/LoadingExtensions.cs index 3e151b4..5511398 100644 --- a/lib/VNLib.Plugins.Extensions.Loading/src/LoadingExtensions.cs +++ b/lib/VNLib.Plugins.Extensions.Loading/src/LoadingExtensions.cs @@ -32,14 +32,11 @@ using System.Threading.Tasks; using System.Collections.Generic; using System.Runtime.CompilerServices; -using VNLib.Utils; -using VNLib.Utils.Memory; using VNLib.Utils.Logging; using VNLib.Utils.Extensions; -using VNLib.Plugins.Essentials.Accounts; namespace VNLib.Plugins.Extensions.Loading -{ +{ /// /// Provides common loading (and unloading when required) extensions for plugins @@ -54,6 +51,7 @@ namespace VNLib.Plugins.Extensions.Loading public const string DEBUG_CONFIG_KEY = "debug"; public const string SECRETS_CONFIG_KEY = "secrets"; public const string PASSWORD_HASHING_KEY = "passwords"; + public const string CUSTOM_PASSWORD_ASM_KEY = "custom_asm"; /* * Plugin local cache used for storing singletons for a plugin instance @@ -95,23 +93,6 @@ namespace VNLib.Plugins.Extensions.Loading public static T GetOrCreateSingleton(PluginBase plugin, Func serviceFactory) => (T)GetOrCreateSingleton(plugin, typeof(T), p => serviceFactory(p)!); - - /// - /// Gets the plugins ambient if loaded, or loads it if required. This class will - /// be unloaded when the plugin us unloaded. - /// - /// - /// The ambient - /// - /// - /// - public static IPasswordHashingProvider GetPasswords(this PluginBase plugin) - { - plugin.ThrowIfUnloaded(); - //Check if a password configuration element is loaded, otherwise load with defaults - return plugin.GetOrCreateSingleton().Passwords; - } - /// /// Loads an assembly into the current plugin's load context and will unload when disposed /// or the plugin is unloaded from the host application. @@ -551,93 +532,5 @@ namespace VNLib.Plugins.Extensions.Loading return lazyFactory; } } - - [ConfigurationName(PASSWORD_HASHING_KEY, Required = false)] - private sealed class SecretProvider : VnDisposeable, ISecretProvider, IAsyncConfigurable - { - private byte[]? _pepper; - private Exception? _error; - - public SecretProvider(PluginBase plugin, IConfigScope config) - { - if(config.TryGetValue("args", out JsonElement el)) - { - //Convert to dict - IReadOnlyDictionary hashingArgs = el.EnumerateObject().ToDictionary(static k => k.Name, static v => v.Value); - - //Get hashing arguments - uint saltLen = hashingArgs["salt_len"].GetUInt32(); - uint hashLen = hashingArgs["hash_len"].GetUInt32(); - uint timeCost = hashingArgs["time_cost"].GetUInt32(); - uint memoryCost = hashingArgs["memory_cost"].GetUInt32(); - uint parallelism = hashingArgs["parallelism"].GetUInt32(); - //Load passwords - Passwords = new(this, (int)saltLen, timeCost, memoryCost, parallelism, hashLen); - } - else - { - Passwords = new(this); - } - } - - public SecretProvider(PluginBase plugin) - { - Passwords = new(this); - } - - - public PasswordHashing Passwords { get; } - - /// - public int BufferSize - { - get - { - Check(); - return _pepper!.Length; - } - } - - public ERRNO GetSecret(Span buffer) - { - Check(); - //Coppy pepper to buffer - _pepper.CopyTo(buffer); - //Return pepper length - return _pepper!.Length; - } - - protected override void Check() - { - base.Check(); - if(_error != null) - { - throw _error; - } - } - - protected override void Free() - { - //Clear the pepper if set - MemoryUtil.InitializeBlock(_pepper.AsSpan()); - } - - public async Task ConfigureServiceAsync(PluginBase plugin) - { - try - { - //Get the pepper from secret storage - _pepper = await plugin.TryGetSecretAsync(PASSWORD_HASHING_KEY).ToBase64Bytes(); - } - catch (Exception ex) - { - //Store exception for re-propagation - _error = ex; - - //Propagate exception to system - throw; - } - } - } } } -- cgit