Add server options in database

This commit is contained in:
Zoe Roux 2024-04-23 00:58:57 +02:00
parent 92ec734276
commit 9b486c0c55
No known key found for this signature in database
4 changed files with 16 additions and 46 deletions

View File

@ -16,30 +16,9 @@
// You should have received a copy of the GNU General Public License
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
namespace Kyoo.Authentication.Models;
namespace Kyoo.Abstractions.Models;
/// <summary>
/// The main authentication options.
/// </summary>
public class AuthenticationOption
public class ServerOptions
{
/// <summary>
/// The path to get this option from the root configuration.
/// </summary>
public const string Path = "authentication";
/// <summary>
/// The default jwt secret.
/// </summary>
public const string DefaultSecret = "4c@mraGB!KRfF@kpS8739y9FcHemKxBsqqxLbdR?";
/// <summary>
/// The secret used to encrypt the jwt.
/// </summary>
public string Secret { get; set; } = DefaultSecret;
/// <summary>
/// Options for permissions
/// </summary>
public PermissionOption Permissions { get; set; } = new();
public byte[] Secret { get; }
}

View File

@ -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;
/// <summary>
/// The service that controls jwt creation and validation.
/// </summary>
public class TokenController : ITokenController
public class TokenController(ServerOptions options) : ITokenController
{
/// <summary>
/// The options that this controller will use.
/// </summary>
private readonly AuthenticationOption _options;
/// <summary>
/// Create a new <see cref="TokenController"/>.
/// </summary>
/// <param name="options">The options that this controller will use.</param>
public TokenController(AuthenticationOption options)
{
_options = options;
}
/// <inheritdoc />
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
/// <inheritdoc />
public Task<string> 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
/// <inheritdoc />
public Guid GetRefreshTokenUserID(string refreshToken)
{
SymmetricSecurityKey key = new(Encoding.UTF8.GetBytes(_options.Secret));
SymmetricSecurityKey key = new(options.Secret);
JwtSecurityTokenHandler tokenHandler = new();
ClaimsPrincipal principal;
try

View File

@ -17,9 +17,11 @@
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
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<IIssueRepository, IssueRepository>();
builder.Services.AddScoped<SqlVariableContext>();
builder.Services.AddScoped<MiscRepository>();
builder.Services.AddSingleton<ServerOptions>(x => {
using var scope = x.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<DatabaseContext>();
return db.Set<ServerOptions>().Single();
});
}
}

View File

@ -66,6 +66,7 @@ public abstract class DatabaseContext : DbContext
public DbSet<EpisodeWatchStatus> EpisodeWatchStatus { get; set; }
public DbSet<Issue> Issues { get; set; }
public DbSet<ServerOptions> Options { get; set; }
/// <summary>
/// Add a many to many link between two resources.