diff options
author | vnugent <public@vaughnnugent.com> | 2023-04-13 11:37:30 -0400 |
---|---|---|
committer | vnugent <public@vaughnnugent.com> | 2023-04-13 11:37:30 -0400 |
commit | 0bafd510a0091960dbfe5ad08d3d524153117536 (patch) | |
tree | 414a00aae7afc84f9b11d6bd7395248d5d24b668 /plugins | |
parent | 5d0660dea5e20d1bda06ea9b06b04bbac36b0db9 (diff) |
Database creation tools
Diffstat (limited to 'plugins')
11 files changed, 103 insertions, 18 deletions
diff --git a/plugins/VNLib.Plugins.Essentials.Accounts.Admin/src/VNLib.Plugins.Essentials.Accounts.Admin.csproj b/plugins/VNLib.Plugins.Essentials.Accounts.Admin/src/VNLib.Plugins.Essentials.Accounts.Admin.csproj index 32384ea..16db325 100644 --- a/plugins/VNLib.Plugins.Essentials.Accounts.Admin/src/VNLib.Plugins.Essentials.Accounts.Admin.csproj +++ b/plugins/VNLib.Plugins.Essentials.Accounts.Admin/src/VNLib.Plugins.Essentials.Accounts.Admin.csproj @@ -27,7 +27,6 @@ </ItemGroup> <ItemGroup> - <ProjectReference Include="..\..\..\..\..\core\lib\Plugins.PluginBase\src\VNLib.Plugins.PluginBase.csproj" /> <ProjectReference Include="..\..\..\..\Extensions\lib\VNLib.Plugins.Extensions.Data\src\VNLib.Plugins.Extensions.Data.csproj" /> <ProjectReference Include="..\..\..\..\Extensions\lib\VNLib.Plugins.Extensions.Loading.Sql\src\VNLib.Plugins.Extensions.Loading.Sql.csproj" /> <ProjectReference Include="..\..\..\..\Extensions\lib\VNLib.Plugins.Extensions.Loading\src\VNLib.Plugins.Extensions.Loading.csproj" /> diff --git a/plugins/VNLib.Plugins.Essentials.Accounts.Registration/src/RegistrationContext.cs b/plugins/VNLib.Plugins.Essentials.Accounts.Registration/src/RegistrationContext.cs index 611e30e..3b50303 100644 --- a/plugins/VNLib.Plugins.Essentials.Accounts.Registration/src/RegistrationContext.cs +++ b/plugins/VNLib.Plugins.Essentials.Accounts.Registration/src/RegistrationContext.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2022 Vaughn Nugent +* Copyright (c) 2023 Vaughn Nugent * * Library: VNLib * Package: VNLib.Plugins.Essentials.Accounts.Registration @@ -25,15 +25,33 @@ using Microsoft.EntityFrameworkCore; using VNLib.Plugins.Extensions.Data; +using VNLib.Plugins.Extensions.Loading.Sql; using VNLib.Plugins.Essentials.Accounts.Registration.TokenRevocation; namespace VNLib.Plugins.Essentials.Accounts.Registration { - internal class RegistrationContext : TransactionalDbContext + internal class RegistrationContext : TransactionalDbContext, IDbTableDefinition { public DbSet<RevokedToken> RevokedRegistrationTokens { get; set; } public RegistrationContext(DbContextOptions options) : base(options) {} + + public RegistrationContext() + {} + + public void OnDatabaseCreating(IDbContextBuilder builder, object? state) + { + //Define a table for the revoked tokens + builder.DefineTable<RevokedToken>(nameof(RevokedRegistrationTokens)) + //Define the token column and the created column, let the framework determine the data-types + .WithColumn(p => p.Token) + .MaxLength(200) + .Next() + + //Define the next column + .WithColumn(p => p.Created) + .AllowNull(false); + } } }
\ No newline at end of file diff --git a/plugins/VNLib.Plugins.Essentials.Accounts.Registration/src/RegistrationEntryPoint.cs b/plugins/VNLib.Plugins.Essentials.Accounts.Registration/src/RegistrationEntryPoint.cs index bceb5e1..790a2c6 100644 --- a/plugins/VNLib.Plugins.Essentials.Accounts.Registration/src/RegistrationEntryPoint.cs +++ b/plugins/VNLib.Plugins.Essentials.Accounts.Registration/src/RegistrationEntryPoint.cs @@ -25,6 +25,7 @@ using VNLib.Utils.Logging; using VNLib.Plugins.Extensions.Loading; +using VNLib.Plugins.Extensions.Loading.Sql; using VNLib.Plugins.Extensions.Loading.Routing; using VNLib.Plugins.Essentials.Accounts.Registration.Endpoints; @@ -39,8 +40,17 @@ namespace VNLib.Plugins.Essentials.Accounts.Registration //Route reg endpoint this.Route<RegistrationEntpoint>(); + //Schedule creating database tables + _ = this.ObserveWork(CreateDatabaseTables, 1000); + Log.Information("Plugin loaded"); } + + private async Task CreateDatabaseTables() + { + //Ensure the database is created + await this.EnsureDbCreatedAsync<RegistrationContext>(this); + } protected override void OnUnLoad() { diff --git a/plugins/VNLib.Plugins.Essentials.Accounts.Registration/src/VNLib.Plugins.Essentials.Accounts.Registration.csproj b/plugins/VNLib.Plugins.Essentials.Accounts.Registration/src/VNLib.Plugins.Essentials.Accounts.Registration.csproj index 5b8edc5..5b254d3 100644 --- a/plugins/VNLib.Plugins.Essentials.Accounts.Registration/src/VNLib.Plugins.Essentials.Accounts.Registration.csproj +++ b/plugins/VNLib.Plugins.Essentials.Accounts.Registration/src/VNLib.Plugins.Essentials.Accounts.Registration.csproj @@ -34,7 +34,7 @@ <Deterministic>False</Deterministic> </PropertyGroup> <ItemGroup> - <PackageReference Include="FluentValidation" Version="11.5.1" /> + <PackageReference Include="FluentValidation" Version="11.5.2" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\..\..\..\..\core\lib\Plugins.Essentials\src\VNLib.Plugins.Essentials.csproj" /> diff --git a/plugins/VNLib.Plugins.Essentials.Accounts/src/SecurityProvider/AccountSecProvider.cs b/plugins/VNLib.Plugins.Essentials.Accounts/src/SecurityProvider/AccountSecProvider.cs index eadebcc..c63304a 100644 --- a/plugins/VNLib.Plugins.Essentials.Accounts/src/SecurityProvider/AccountSecProvider.cs +++ b/plugins/VNLib.Plugins.Essentials.Accounts/src/SecurityProvider/AccountSecProvider.cs @@ -64,14 +64,11 @@ namespace VNLib.Plugins.Essentials.Accounts.SecurityProvider private const HashAlg ClientTokenHmacType = HashAlg.SHA256; - /// <summary> /// The client data encryption padding. /// </summary> public static readonly RSAEncryptionPadding ClientEncryptonPadding = RSAEncryptionPadding.OaepSHA256; - //private static HMAC GetPubKeySigningAlg(byte[] key) => new HMACSHA256(key); - private readonly AccountSecConfig _config; public AccountSecProvider(PluginBase plugin) @@ -248,7 +245,7 @@ namespace VNLib.Plugins.Essentials.Accounts.SecurityProvider } //Alloc buffer for encode/decode - using IMemoryHandle<byte> buffer = MemoryUtil.SafeAllocNearestPage<byte>(4000, true); + using IMemoryHandle<byte> buffer = MemoryUtil.SafeAllocNearestPage(4000, true); try { using RSA rsa = RSA.Create(); @@ -314,7 +311,7 @@ namespace VNLib.Plugins.Essentials.Accounts.SecurityProvider //Parse the client jwt signed message using JsonWebToken jwt = JsonWebToken.Parse(signedMessage); - using (UnsafeMemoryHandle<byte> decodeBuffer = MemoryUtil.UnsafeAllocNearestPage<byte>(_config.TokenKeySize, true)) + using (UnsafeMemoryHandle<byte> decodeBuffer = MemoryUtil.UnsafeAllocNearestPage(_config.TokenKeySize, true)) { //Recover the key from base32 ERRNO count = VnEncoding.TryFromBase32Chars(sharedKey, decodeBuffer.Span); @@ -401,7 +398,7 @@ namespace VNLib.Plugins.Essentials.Accounts.SecurityProvider //Alloc buffer for decoding the base64 signatures - using UnsafeMemoryHandle<byte> buffer = MemoryUtil.UnsafeAllocNearestPage<byte>(2 * _config.LoginCookieSize, true); + using UnsafeMemoryHandle<byte> buffer = MemoryUtil.UnsafeAllocNearestPage(2 * _config.LoginCookieSize, true); //Slice up buffers Span<byte> cookieBuffer = buffer.Span[.._config.LoginCookieSize]; @@ -496,7 +493,7 @@ namespace VNLib.Plugins.Essentials.Accounts.SecurityProvider } //Alloc a buffer for decoding the public key - using UnsafeMemoryHandle<byte> pubKeyBuffer = MemoryUtil.UnsafeAllocNearestPage<byte>(base64PubKey.Length, true); + using UnsafeMemoryHandle<byte> pubKeyBuffer = MemoryUtil.UnsafeAllocNearestPage(base64PubKey.Length, true); //Decode the public key ERRNO pbkBytesWritten = VnEncoding.TryFromBase64Chars(base64PubKey, pubKeyBuffer.Span); diff --git a/plugins/VNLib.Plugins.Essentials.Accounts/src/VNLib.Plugins.Essentials.Accounts.csproj b/plugins/VNLib.Plugins.Essentials.Accounts/src/VNLib.Plugins.Essentials.Accounts.csproj index e43c0af..c96900e 100644 --- a/plugins/VNLib.Plugins.Essentials.Accounts/src/VNLib.Plugins.Essentials.Accounts.csproj +++ b/plugins/VNLib.Plugins.Essentials.Accounts/src/VNLib.Plugins.Essentials.Accounts.csproj @@ -43,7 +43,7 @@ <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> </PackageReference> - <PackageReference Include="FluentValidation" Version="11.5.1" /> + <PackageReference Include="FluentValidation" Version="11.5.2" /> </ItemGroup> <ItemGroup> diff --git a/plugins/VNLib.Plugins.Essentials.Content.Routing/src/Model/Route.cs b/plugins/VNLib.Plugins.Essentials.Content.Routing/src/Model/Route.cs index 8c52725..c1531f7 100644 --- a/plugins/VNLib.Plugins.Essentials.Content.Routing/src/Model/Route.cs +++ b/plugins/VNLib.Plugins.Essentials.Content.Routing/src/Model/Route.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2022 Vaughn Nugent +* Copyright (c) 2023 Vaughn Nugent * * Library: VNLib * Package: VNLib.Plugins.Essentials.Content.Routing @@ -23,6 +23,7 @@ */ using System; +using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using Microsoft.EntityFrameworkCore; @@ -34,6 +35,7 @@ namespace VNLib.Plugins.Essentials.Content.Routing.Model [Index(nameof(Id), IsUnique = true)] internal class Route : DbModelBase { + [Key] public override string Id { get; set; } public override DateTime Created { get; set; } public override DateTime LastModified { get; set; } diff --git a/plugins/VNLib.Plugins.Essentials.Content.Routing/src/Model/RoutingContext.cs b/plugins/VNLib.Plugins.Essentials.Content.Routing/src/Model/RoutingContext.cs index 0cbd90f..4edb892 100644 --- a/plugins/VNLib.Plugins.Essentials.Content.Routing/src/Model/RoutingContext.cs +++ b/plugins/VNLib.Plugins.Essentials.Content.Routing/src/Model/RoutingContext.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2022 Vaughn Nugent +* Copyright (c) 2023 Vaughn Nugent * * Library: VNLib * Package: VNLib.Plugins.Essentials.Content.Routing @@ -27,15 +27,64 @@ using System; using Microsoft.EntityFrameworkCore; using VNLib.Plugins.Extensions.Data; +using VNLib.Plugins.Extensions.Loading.Sql; namespace VNLib.Plugins.Essentials.Content.Routing.Model { - internal class RoutingContext : TransactionalDbContext + internal class RoutingContext : TransactionalDbContext, IDbTableDefinition { public DbSet<Route> Routes { get; set; } +#nullable disable + public RoutingContext(DbContextOptions options) :base(options) { } + + public RoutingContext() + {} + +#nullable enable + + public void OnDatabaseCreating(IDbContextBuilder builder, object? userState) + { + //Build the route table + + builder.DefineTable<Route>(nameof(Routes)) + .WithColumn(r => r.Id) + .MaxLength(50) + .Next() + + .WithColumn(r => r.Hostname) + .MaxLength(100) + .AllowNull(false) + .Next() + + .WithColumn(r => r.MatchPath) + .MaxLength(1000) + .AllowNull(false) + .Next() + + //Default to read-on + .WithColumn(r => r.Privilage) + .WithDefault(Accounts.AccountUtil.READ_MSK) + .AllowNull(false) + .Next() + + .WithColumn(r => r.Alternate) + .MaxLength(1000) + .Next() + + .WithColumn(r => (int)r.Routine) + .WithDefault(FpRoutine.Continue) + .Next() + + .WithColumn(r => r.Created) + .AllowNull(false) + .Next() + + .WithColumn(r => r.LastModified) + .AllowNull(false); + } } } diff --git a/plugins/VNLib.Plugins.Essentials.Content.Routing/src/PageRouterEntry.cs b/plugins/VNLib.Plugins.Essentials.Content.Routing/src/PageRouterEntry.cs index 0c04047..e71ee6b 100644 --- a/plugins/VNLib.Plugins.Essentials.Content.Routing/src/PageRouterEntry.cs +++ b/plugins/VNLib.Plugins.Essentials.Content.Routing/src/PageRouterEntry.cs @@ -23,12 +23,14 @@ */ using System; +using System.Threading.Tasks; using System.ComponentModel.Design; using VNLib.Utils.Logging; using VNLib.Plugins.Attributes; using VNLib.Plugins.Extensions.Loading; - +using VNLib.Plugins.Extensions.Loading.Sql; +using VNLib.Plugins.Essentials.Content.Routing.Model; namespace VNLib.Plugins.Essentials.Content.Routing { @@ -49,6 +51,10 @@ namespace VNLib.Plugins.Essentials.Content.Routing { //Init router PageRouter = this.GetOrCreateSingleton<Router>(); + + //Schedule the db creation + _ = this.ObserveWork(OnDbCreationAsync, 500); + Log.Information("Plugin loaded"); } @@ -65,5 +71,11 @@ namespace VNLib.Plugins.Essentials.Content.Routing Log.Information("Routing table reset"); } } + + private async Task OnDbCreationAsync() + { + //Create the router + await this.EnsureDbCreatedAsync<RoutingContext>(null); + } } } diff --git a/plugins/VNLib.Plugins.Essentials.Content.Routing/src/VNLib.Plugins.Essentials.Content.Routing.csproj b/plugins/VNLib.Plugins.Essentials.Content.Routing/src/VNLib.Plugins.Essentials.Content.Routing.csproj index c15e546..2d93897 100644 --- a/plugins/VNLib.Plugins.Essentials.Content.Routing/src/VNLib.Plugins.Essentials.Content.Routing.csproj +++ b/plugins/VNLib.Plugins.Essentials.Content.Routing/src/VNLib.Plugins.Essentials.Content.Routing.csproj @@ -43,7 +43,6 @@ </ItemGroup> <ItemGroup> - <ProjectReference Include="..\..\..\..\..\core\lib\Plugins.PluginBase\src\VNLib.Plugins.PluginBase.csproj" /> <ProjectReference Include="..\..\..\..\Extensions\lib\VNLib.Plugins.Extensions.Data\src\VNLib.Plugins.Extensions.Data.csproj" /> <ProjectReference Include="..\..\..\..\Extensions\lib\VNLib.Plugins.Extensions.Loading.Sql\src\VNLib.Plugins.Extensions.Loading.Sql.csproj" /> <ProjectReference Include="..\..\..\..\Extensions\lib\VNLib.Plugins.Extensions.Loading\src\VNLib.Plugins.Extensions.Loading.csproj" /> diff --git a/plugins/VNLib.Plugins.Essentials.SocialOauth/src/VNLib.Plugins.Essentials.SocialOauth.csproj b/plugins/VNLib.Plugins.Essentials.SocialOauth/src/VNLib.Plugins.Essentials.SocialOauth.csproj index 7e8ee51..3cf39de 100644 --- a/plugins/VNLib.Plugins.Essentials.SocialOauth/src/VNLib.Plugins.Essentials.SocialOauth.csproj +++ b/plugins/VNLib.Plugins.Essentials.SocialOauth/src/VNLib.Plugins.Essentials.SocialOauth.csproj @@ -39,7 +39,6 @@ <ItemGroup> <ProjectReference Include="..\..\..\..\..\core\lib\Net.Rest.Client\src\VNLib.Net.Rest.Client.csproj" /> <ProjectReference Include="..\..\..\..\..\core\lib\Plugins.Essentials\src\VNLib.Plugins.Essentials.csproj" /> - <ProjectReference Include="..\..\..\..\..\core\lib\Plugins.PluginBase\src\VNLib.Plugins.PluginBase.csproj" /> <ProjectReference Include="..\..\..\..\Extensions\lib\VNLib.Plugins.Extensions.Loading\src\VNLib.Plugins.Extensions.Loading.csproj" /> <ProjectReference Include="..\..\..\..\Extensions\lib\VNLib.Plugins.Extensions.Validation\src\VNLib.Plugins.Extensions.Validation.csproj" /> </ItemGroup> |