diff --git a/back/src/Kyoo.Authentication/Models/Options/AuthenticationOption.cs b/back/src/Kyoo.Abstractions/Models/ServerOptions.cs similarity index 53% rename from back/src/Kyoo.Authentication/Models/Options/AuthenticationOption.cs rename to back/src/Kyoo.Abstractions/Models/ServerOptions.cs index c7e1032a..f97868b3 100644 --- a/back/src/Kyoo.Authentication/Models/Options/AuthenticationOption.cs +++ b/back/src/Kyoo.Abstractions/Models/ServerOptions.cs @@ -16,30 +16,9 @@ // You should have received a copy of the GNU General Public License // along with Kyoo. If not, see . -namespace Kyoo.Authentication.Models; +namespace Kyoo.Abstractions.Models; -/// -/// The main authentication options. -/// -public class AuthenticationOption +public class ServerOptions { - /// - /// The path to get this option from the root configuration. - /// - public const string Path = "authentication"; - - /// - /// The default jwt secret. - /// - public const string DefaultSecret = "4c@mraGB!KRfF@kpS8739y9FcHemKxBsqqxLbdR?"; - - /// - /// The secret used to encrypt the jwt. - /// - public string Secret { get; set; } = DefaultSecret; - - /// - /// Options for permissions - /// - public PermissionOption Permissions { get; set; } = new(); + public byte[] Secret { get; } } diff --git a/back/src/Kyoo.Authentication/Controllers/TokenController.cs b/back/src/Kyoo.Authentication/Controllers/TokenController.cs index f4ca0723..940db1a3 100644 --- a/back/src/Kyoo.Authentication/Controllers/TokenController.cs +++ b/back/src/Kyoo.Authentication/Controllers/TokenController.cs @@ -21,7 +21,6 @@ using System.Collections.Generic; using System.IdentityModel.Tokens.Jwt; using System.Linq; using System.Security.Claims; -using System.Text; using System.Threading.Tasks; using Kyoo.Abstractions.Models; using Kyoo.Authentication.Models; @@ -29,31 +28,14 @@ using Microsoft.IdentityModel.Tokens; namespace Kyoo.Authentication; -/// -/// The service that controls jwt creation and validation. -/// -public class TokenController : ITokenController +public class TokenController(ServerOptions options) : ITokenController { - /// - /// The options that this controller will use. - /// - private readonly AuthenticationOption _options; - - /// - /// Create a new . - /// - /// The options that this controller will use. - public TokenController(AuthenticationOption options) - { - _options = options; - } - /// public string CreateAccessToken(User user, out TimeSpan expireIn) { expireIn = new TimeSpan(1, 0, 0); - SymmetricSecurityKey key = new(Encoding.UTF8.GetBytes(_options.Secret)); + SymmetricSecurityKey key = new(options.Secret); SigningCredentials credential = new(key, SecurityAlgorithms.HmacSha256Signature); string permissions = user.Permissions != null ? string.Join(',', user.Permissions) : string.Empty; @@ -79,7 +61,7 @@ public class TokenController : ITokenController /// public Task CreateRefreshToken(User user) { - SymmetricSecurityKey key = new(Encoding.UTF8.GetBytes(_options.Secret)); + SymmetricSecurityKey key = new(options.Secret); SigningCredentials credential = new(key, SecurityAlgorithms.HmacSha256Signature); JwtSecurityToken token = new( @@ -99,7 +81,7 @@ public class TokenController : ITokenController /// public Guid GetRefreshTokenUserID(string refreshToken) { - SymmetricSecurityKey key = new(Encoding.UTF8.GetBytes(_options.Secret)); + SymmetricSecurityKey key = new(options.Secret); JwtSecurityTokenHandler tokenHandler = new(); ClaimsPrincipal principal; try diff --git a/back/src/Kyoo.Core/CoreModule.cs b/back/src/Kyoo.Core/CoreModule.cs index 1e853065..0a6be607 100644 --- a/back/src/Kyoo.Core/CoreModule.cs +++ b/back/src/Kyoo.Core/CoreModule.cs @@ -17,9 +17,11 @@ // along with Kyoo. If not, see . using System; +using System.Linq; using Kyoo.Abstractions.Controllers; using Kyoo.Abstractions.Models; using Kyoo.Core.Controllers; +using Kyoo.Postgresql; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; @@ -64,5 +66,11 @@ 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 3f2cd14c..dd6b6537 100644 --- a/back/src/Kyoo.Postgresql/DatabaseContext.cs +++ b/back/src/Kyoo.Postgresql/DatabaseContext.cs @@ -66,6 +66,7 @@ public abstract class DatabaseContext : DbContext public DbSet EpisodeWatchStatus { get; set; } public DbSet Issues { get; set; } + public DbSet Options { get; set; } /// /// Add a many to many link between two resources.