diff --git a/back/src/Kyoo.Authentication/AuthenticationModule.cs b/back/src/Kyoo.Authentication/AuthenticationModule.cs index 6ce7ebeb..ab0b45a8 100644 --- a/back/src/Kyoo.Authentication/AuthenticationModule.cs +++ b/back/src/Kyoo.Authentication/AuthenticationModule.cs @@ -36,10 +36,6 @@ public static class AuthenticationModule { public static void ConfigureAuthentication(this WebApplicationBuilder builder) { - string secret = builder.Configuration.GetValue( - "AUTHENTICATION_SECRET", - AuthenticationOption.DefaultSecret - )!; PermissionOption options = new() { @@ -114,9 +110,8 @@ public static class AuthenticationModule ), }; builder.Services.AddSingleton(options); - builder.Services.AddSingleton( - new AuthenticationOption() { Secret = secret, Permissions = options, } - ); + var secret = builder.Configuration.GetValue("AUTHENTICATION_SECRET")!; + builder.Services.AddSingleton(new AuthenticationOption() { Secret = secret }); builder .Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) @@ -145,7 +140,7 @@ public static class AuthenticationModule ValidateAudience = false, ValidateLifetime = true, ValidateIssuerSigningKey = true, - IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret)) + IssuerSigningKey = new SymmetricSecurityKey(secret) }; }); diff --git a/back/src/Kyoo.Authentication/Controllers/TokenController.cs b/back/src/Kyoo.Authentication/Controllers/TokenController.cs index 940db1a3..818d65b3 100644 --- a/back/src/Kyoo.Authentication/Controllers/TokenController.cs +++ b/back/src/Kyoo.Authentication/Controllers/TokenController.cs @@ -28,7 +28,7 @@ using Microsoft.IdentityModel.Tokens; namespace Kyoo.Authentication; -public class TokenController(ServerOptions options) : ITokenController +public class TokenController(AuthenticationOption options) : ITokenController { /// public string CreateAccessToken(User user, out TimeSpan expireIn) diff --git a/back/src/Kyoo.Abstractions/Models/ServerOptions.cs b/back/src/Kyoo.Authentication/Models/Options/AuthenticationOption.cs similarity index 87% rename from back/src/Kyoo.Abstractions/Models/ServerOptions.cs rename to back/src/Kyoo.Authentication/Models/Options/AuthenticationOption.cs index f97868b3..74822e0d 100644 --- a/back/src/Kyoo.Abstractions/Models/ServerOptions.cs +++ b/back/src/Kyoo.Authentication/Models/Options/AuthenticationOption.cs @@ -16,9 +16,9 @@ // You should have received a copy of the GNU General Public License // along with Kyoo. If not, see . -namespace Kyoo.Abstractions.Models; +namespace Kyoo.Authentication.Models; -public class ServerOptions +public class AuthenticationOption { - public byte[] Secret { get; } + public byte[] Secret { get; set; } } diff --git a/back/src/Kyoo.Core/CoreModule.cs b/back/src/Kyoo.Core/CoreModule.cs index 0a6be607..c895fc25 100644 --- a/back/src/Kyoo.Core/CoreModule.cs +++ b/back/src/Kyoo.Core/CoreModule.cs @@ -66,11 +66,5 @@ public static class CoreModule builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); - - builder.Services.AddSingleton(x => { - using var scope = x.CreateScope(); - var db = scope.ServiceProvider.GetRequiredService(); - return db.Set().Single(); - }); } } diff --git a/back/src/Kyoo.Postgresql/DatabaseContext.cs b/back/src/Kyoo.Postgresql/DatabaseContext.cs index dd6b6537..7a717cc2 100644 --- a/back/src/Kyoo.Postgresql/DatabaseContext.cs +++ b/back/src/Kyoo.Postgresql/DatabaseContext.cs @@ -66,7 +66,8 @@ public abstract class DatabaseContext : DbContext public DbSet EpisodeWatchStatus { get; set; } public DbSet Issues { get; set; } - public DbSet Options { get; set; } + + public DbSet Options { get; set; } /// /// Add a many to many link between two resources. @@ -354,6 +355,8 @@ public abstract class DatabaseContext : DbContext _HasJson(modelBuilder, x => x.Settings); _HasJson(modelBuilder, x => x.ExternalId); _HasJson(modelBuilder, x => x.Extra); + + modelBuilder.Entity().HasKey(x => x.Key); } public override int SaveChanges() diff --git a/back/src/Kyoo.Postgresql/DbConfigurationProvider.cs b/back/src/Kyoo.Postgresql/DbConfigurationProvider.cs new file mode 100644 index 00000000..9fa1b1ee --- /dev/null +++ b/back/src/Kyoo.Postgresql/DbConfigurationProvider.cs @@ -0,0 +1,28 @@ +using System; +using System.Linq; +using Kyoo.Postgresql; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; + +public class DbConfigurationProvider(Action action) : ConfigurationProvider +{ + public override void Load() + { + DbContextOptionsBuilder builder = new(); + action(builder); + using var context = new PostgresContext(builder.Options, null!); + Data = context.Options.ToDictionary(c => c.Key, c => c.Value)!; + } +} + +public class DbConfigurationSource(Action action) : IConfigurationSource +{ + public IConfigurationProvider Build(IConfigurationBuilder builder) => + new DbConfigurationProvider(action); +} + +public class ServerOption +{ + public string Key { get; set; } + public string Value { get; set; } +} diff --git a/back/src/Kyoo.Postgresql/PostgresModule.cs b/back/src/Kyoo.Postgresql/PostgresModule.cs index 2720635f..012bd0c0 100644 --- a/back/src/Kyoo.Postgresql/PostgresModule.cs +++ b/back/src/Kyoo.Postgresql/PostgresModule.cs @@ -16,6 +16,7 @@ // You should have received a copy of the GNU General Public License // along with Kyoo. If not, see . +using System; using System.Data.Common; using Kyoo.Abstractions.Models; using Microsoft.AspNetCore.Builder; @@ -69,5 +70,14 @@ public static class PostgresModule ); builder.Services.AddHealthChecks().AddDbContextCheck(); + builder.Configuration.AddDbConfigurationProvider(x => x.UseNpgsql(dataSource)); + } + + private static void AddDbConfigurationProvider( + this IConfigurationBuilder builder, + Action action + ) + { + builder.Add(new DbConfigurationSource(action)); } }