mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-06-04 14:14:12 -04:00
Add server options in database
This commit is contained in:
parent
92ec734276
commit
9b486c0c55
@ -16,30 +16,9 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
|
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
namespace Kyoo.Authentication.Models;
|
namespace Kyoo.Abstractions.Models;
|
||||||
|
|
||||||
/// <summary>
|
public class ServerOptions
|
||||||
/// The main authentication options.
|
|
||||||
/// </summary>
|
|
||||||
public class AuthenticationOption
|
|
||||||
{
|
{
|
||||||
/// <summary>
|
public byte[] Secret { get; }
|
||||||
/// 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();
|
|
||||||
}
|
}
|
@ -21,7 +21,6 @@ using System.Collections.Generic;
|
|||||||
using System.IdentityModel.Tokens.Jwt;
|
using System.IdentityModel.Tokens.Jwt;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Kyoo.Abstractions.Models;
|
using Kyoo.Abstractions.Models;
|
||||||
using Kyoo.Authentication.Models;
|
using Kyoo.Authentication.Models;
|
||||||
@ -29,31 +28,14 @@ using Microsoft.IdentityModel.Tokens;
|
|||||||
|
|
||||||
namespace Kyoo.Authentication;
|
namespace Kyoo.Authentication;
|
||||||
|
|
||||||
/// <summary>
|
public class TokenController(ServerOptions options) : ITokenController
|
||||||
/// The service that controls jwt creation and validation.
|
|
||||||
/// </summary>
|
|
||||||
public class TokenController : 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 />
|
/// <inheritdoc />
|
||||||
public string CreateAccessToken(User user, out TimeSpan expireIn)
|
public string CreateAccessToken(User user, out TimeSpan expireIn)
|
||||||
{
|
{
|
||||||
expireIn = new TimeSpan(1, 0, 0);
|
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);
|
SigningCredentials credential = new(key, SecurityAlgorithms.HmacSha256Signature);
|
||||||
string permissions =
|
string permissions =
|
||||||
user.Permissions != null ? string.Join(',', user.Permissions) : string.Empty;
|
user.Permissions != null ? string.Join(',', user.Permissions) : string.Empty;
|
||||||
@ -79,7 +61,7 @@ public class TokenController : ITokenController
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public Task<string> CreateRefreshToken(User user)
|
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);
|
SigningCredentials credential = new(key, SecurityAlgorithms.HmacSha256Signature);
|
||||||
JwtSecurityToken token =
|
JwtSecurityToken token =
|
||||||
new(
|
new(
|
||||||
@ -99,7 +81,7 @@ public class TokenController : ITokenController
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public Guid GetRefreshTokenUserID(string refreshToken)
|
public Guid GetRefreshTokenUserID(string refreshToken)
|
||||||
{
|
{
|
||||||
SymmetricSecurityKey key = new(Encoding.UTF8.GetBytes(_options.Secret));
|
SymmetricSecurityKey key = new(options.Secret);
|
||||||
JwtSecurityTokenHandler tokenHandler = new();
|
JwtSecurityTokenHandler tokenHandler = new();
|
||||||
ClaimsPrincipal principal;
|
ClaimsPrincipal principal;
|
||||||
try
|
try
|
||||||
|
@ -17,9 +17,11 @@
|
|||||||
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
|
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Linq;
|
||||||
using Kyoo.Abstractions.Controllers;
|
using Kyoo.Abstractions.Controllers;
|
||||||
using Kyoo.Abstractions.Models;
|
using Kyoo.Abstractions.Models;
|
||||||
using Kyoo.Core.Controllers;
|
using Kyoo.Core.Controllers;
|
||||||
|
using Kyoo.Postgresql;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
@ -64,5 +66,11 @@ public static class CoreModule
|
|||||||
builder.Services.AddScoped<IIssueRepository, IssueRepository>();
|
builder.Services.AddScoped<IIssueRepository, IssueRepository>();
|
||||||
builder.Services.AddScoped<SqlVariableContext>();
|
builder.Services.AddScoped<SqlVariableContext>();
|
||||||
builder.Services.AddScoped<MiscRepository>();
|
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();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -66,6 +66,7 @@ public abstract class DatabaseContext : DbContext
|
|||||||
public DbSet<EpisodeWatchStatus> EpisodeWatchStatus { get; set; }
|
public DbSet<EpisodeWatchStatus> EpisodeWatchStatus { get; set; }
|
||||||
|
|
||||||
public DbSet<Issue> Issues { get; set; }
|
public DbSet<Issue> Issues { get; set; }
|
||||||
|
public DbSet<ServerOptions> Options { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Add a many to many link between two resources.
|
/// Add a many to many link between two resources.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user