diff --git a/back/src/Kyoo.Authentication/AuthenticationModule.cs b/back/src/Kyoo.Authentication/AuthenticationModule.cs index 8d32898f..5c20521a 100644 --- a/back/src/Kyoo.Authentication/AuthenticationModule.cs +++ b/back/src/Kyoo.Authentication/AuthenticationModule.cs @@ -62,15 +62,17 @@ namespace Kyoo.Authentication public void Configure(IServiceCollection services) { string secret = _configuration.GetValue("AUTHENTICATION_SECRET", AuthenticationOption.DefaultSecret); - services.Configure(x => + PermissionOption permissions = new() { - x.Secret = secret; - x.Permissions = new PermissionOption - { - Default = _configuration.GetValue("UNLOGGED_PERMISSIONS", "overall.read").Split(','), - NewUser = _configuration.GetValue("DEFAULT_PERMISSIONS", "overall.read").Split(','), - ApiKeys = _configuration.GetValue("KYOO_APIKEYS", string.Empty).Split(','), - }; + Default = _configuration.GetValue("UNLOGGED_PERMISSIONS", "overall.read").Split(','), + NewUser = _configuration.GetValue("DEFAULT_PERMISSIONS", "overall.read").Split(','), + ApiKeys = _configuration.GetValue("KYOO_APIKEYS", string.Empty).Split(','), + }; + services.AddSingleton(permissions); + services.AddSingleton(new AuthenticationOption() + { + Secret = secret, + Permissions = permissions, }); // TODO handle direct-videos with bearers (probably add a cookie and a app.Use to translate that for videos) diff --git a/back/src/Kyoo.Authentication/Controllers/PermissionValidator.cs b/back/src/Kyoo.Authentication/Controllers/PermissionValidator.cs index 7ffddd7f..e5b4fa48 100644 --- a/back/src/Kyoo.Authentication/Controllers/PermissionValidator.cs +++ b/back/src/Kyoo.Authentication/Controllers/PermissionValidator.cs @@ -30,7 +30,6 @@ using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; -using Microsoft.Extensions.Options; using Microsoft.Extensions.Primitives; namespace Kyoo.Authentication @@ -44,13 +43,13 @@ namespace Kyoo.Authentication /// /// The permissions options to retrieve default permissions. /// - private readonly IOptionsMonitor _options; + private readonly PermissionOption _options; /// /// Create a new factory with the given options. /// /// The option containing default values. - public PermissionValidator(IOptionsMonitor options) + public PermissionValidator(PermissionOption options) { _options = options; } @@ -90,7 +89,7 @@ namespace Kyoo.Authentication /// /// The permissions options to retrieve default permissions. /// - private readonly IOptionsMonitor _options; + private readonly PermissionOption _options; /// /// Create a new permission validator with the given options. @@ -103,7 +102,7 @@ namespace Kyoo.Authentication string permission, Kind kind, Group group, - IOptionsMonitor options) + PermissionOption options) { _permission = permission; _kind = kind; @@ -117,7 +116,7 @@ namespace Kyoo.Authentication /// The partial permission to validate. /// The group of the permission. /// The option containing default values. - public PermissionValidatorFilter(object partialInfo, Group? group, IOptionsMonitor options) + public PermissionValidatorFilter(object partialInfo, Group? group, PermissionOption options) { switch (partialInfo) { @@ -183,7 +182,7 @@ namespace Kyoo.Authentication } else if (res.None) { - ICollection permissions = _options.CurrentValue.Default ?? Array.Empty(); + ICollection permissions = _options.Default ?? Array.Empty(); if (permissions.All(x => x != permStr && x != overallStr)) { context.Result = _ErrorResult($"Unlogged user does not have permission {permStr} or {overallStr}", StatusCodes.Status401Unauthorized); @@ -199,7 +198,7 @@ namespace Kyoo.Authentication { if (!context.HttpContext.Request.Headers.TryGetValue("X-API-Key", out StringValues apiKey)) return AuthenticateResult.NoResult(); - if (!_options.CurrentValue.ApiKeys.Contains(apiKey)) + if (!_options.ApiKeys.Contains(apiKey)) return AuthenticateResult.Fail("Invalid API-Key."); return AuthenticateResult.Success( new AuthenticationTicket( diff --git a/back/src/Kyoo.Authentication/Controllers/TokenController.cs b/back/src/Kyoo.Authentication/Controllers/TokenController.cs index efc48784..fe0c1b0b 100644 --- a/back/src/Kyoo.Authentication/Controllers/TokenController.cs +++ b/back/src/Kyoo.Authentication/Controllers/TokenController.cs @@ -26,7 +26,6 @@ using System.Text; using System.Threading.Tasks; using Kyoo.Abstractions.Models; using Kyoo.Authentication.Models; -using Microsoft.Extensions.Options; using Microsoft.IdentityModel.Tokens; namespace Kyoo.Authentication @@ -39,13 +38,13 @@ namespace Kyoo.Authentication /// /// The options that this controller will use. /// - private readonly IOptions _options; + private readonly AuthenticationOption _options; /// /// Create a new . /// /// The options that this controller will use. - public TokenController(IOptions options) + public TokenController(AuthenticationOption options) { _options = options; } @@ -55,7 +54,7 @@ namespace Kyoo.Authentication { expireIn = new TimeSpan(1, 0, 0); - SymmetricSecurityKey key = new(Encoding.UTF8.GetBytes(_options.Value.Secret)); + SymmetricSecurityKey key = new(Encoding.UTF8.GetBytes(_options.Secret)); SigningCredentials credential = new(key, SecurityAlgorithms.HmacSha256Signature); string permissions = user.Permissions != null ? string.Join(',', user.Permissions) @@ -80,7 +79,7 @@ namespace Kyoo.Authentication /// public Task CreateRefreshToken(User user) { - SymmetricSecurityKey key = new(Encoding.UTF8.GetBytes(_options.Value.Secret)); + SymmetricSecurityKey key = new(Encoding.UTF8.GetBytes(_options.Secret)); SigningCredentials credential = new(key, SecurityAlgorithms.HmacSha256Signature); JwtSecurityToken token = new( signingCredentials: credential, @@ -99,7 +98,7 @@ namespace Kyoo.Authentication /// public int GetRefreshTokenUserID(string refreshToken) { - SymmetricSecurityKey key = new(Encoding.UTF8.GetBytes(_options.Value.Secret)); + SymmetricSecurityKey key = new(Encoding.UTF8.GetBytes(_options.Secret)); JwtSecurityTokenHandler tokenHandler = new(); ClaimsPrincipal principal; try diff --git a/back/src/Kyoo.Authentication/Views/AuthApi.cs b/back/src/Kyoo.Authentication/Views/AuthApi.cs index f42d0ed5..3a97190b 100644 --- a/back/src/Kyoo.Authentication/Views/AuthApi.cs +++ b/back/src/Kyoo.Authentication/Views/AuthApi.cs @@ -29,7 +29,6 @@ using Kyoo.Authentication.Models; using Kyoo.Authentication.Models.DTO; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Options; using Microsoft.IdentityModel.Tokens; using static Kyoo.Abstractions.Models.Utils.Constants; using BCryptNet = BCrypt.Net.BCrypt; @@ -57,7 +56,7 @@ namespace Kyoo.Authentication.Views /// /// The permisson options. /// - private readonly IOptionsMonitor _permissions; + private readonly PermissionOption _permissions; /// /// Create a new . @@ -65,7 +64,7 @@ namespace Kyoo.Authentication.Views /// The repository used to check if the user exists. /// The token generator. /// The permission opitons. - public AuthApi(IUserRepository users, ITokenController token, IOptionsMonitor permissions) + public AuthApi(IUserRepository users, ITokenController token, PermissionOption permissions) { _users = users; _token = token; @@ -124,7 +123,7 @@ namespace Kyoo.Authentication.Views public async Task> Register([FromBody] RegisterRequest request) { User user = request.ToUser(); - user.Permissions = _permissions.CurrentValue.NewUser; + user.Permissions = _permissions.NewUser; // If no users exists, the new one will be an admin. Give it every permissions. if (await _users.GetOrDefault(where: x => true) == null) user.Permissions = PermissionOption.Admin; diff --git a/back/src/Kyoo.Host/HostModule.cs b/back/src/Kyoo.Host/HostModule.cs index 061370f3..4f83bb62 100644 --- a/back/src/Kyoo.Host/HostModule.cs +++ b/back/src/Kyoo.Host/HostModule.cs @@ -66,12 +66,6 @@ namespace Kyoo.Host builder.RegisterComposite().InstancePerLifetimeScope(); } - /// - public void Configure(IServiceCollection services) - { - services.Configure(_configuration.GetSection(BasicOptions.Path)); - } - /// public IEnumerable ConfigureSteps => new[] {