diff --git a/Kyoo.Authentication/AuthenticationModule.cs b/Kyoo.Authentication/AuthenticationModule.cs index e0bd5628..b21214ff 100644 --- a/Kyoo.Authentication/AuthenticationModule.cs +++ b/Kyoo.Authentication/AuthenticationModule.cs @@ -10,7 +10,6 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -89,28 +88,10 @@ namespace Kyoo.Authentication // TODO handle direct-videos with bearers (probably add a ?token query param and a app.Use to translate that for videos) - // TODO Support sign-out, check if login work, check if tokens should be stored. + // TODO Check if tokens should be stored. // TODO remove unused/commented code, add documentation. - // services.AddIdentityCore() - // .AddSignInManager() - // .AddDefaultTokenProviders() - // .AddUserStore(); - - // services.AddDbContext(options => - // { - // options.UseNpgsql(_configuration.GetDatabaseConnection("postgres")); - // }); - - // services.AddIdentityCore(o => - // { - // o.Stores.MaxLengthForKeys = 128; - // }) - // .AddSignInManager() - // .AddDefaultTokenProviders() - // .AddEntityFrameworkStores(); - services.Configure(_configuration.GetSection(PermissionOption.Path)); services.Configure(_configuration.GetSection(CertificateOption.Path)); services.Configure(_configuration.GetSection(AuthenticationOption.Path)); @@ -124,35 +105,15 @@ namespace Kyoo.Authentication options.UserInteraction.ErrorUrl = $"{publicUrl}/error"; options.UserInteraction.LogoutUrl = $"{publicUrl}/logout"; }) - // .AddAspNetIdentity() - // .AddConfigurationStore(options => - // { - // options.ConfigureDbContext = builder => - // builder.UseNpgsql(_configuration.GetDatabaseConnection("postgres"), - // sql => sql.MigrationsAssembly(assemblyName)); - // }) - // .AddOperationalStore(options => - // { - // options.ConfigureDbContext = builder => - // builder.UseNpgsql(_configuration.GetDatabaseConnection("postgres"), - // sql => sql.MigrationsAssembly(assemblyName)); - // options.EnableTokenCleanup = true; - // }) .AddInMemoryIdentityResources(IdentityContext.GetIdentityResources()) .AddInMemoryApiScopes(IdentityContext.GetScopes()) .AddInMemoryApiResources(IdentityContext.GetApis()) .AddInMemoryClients(IdentityContext.GetClients()) + .AddInMemoryClients(_configuration.GetSection("authentication:clients")) .AddProfileService() .AddSigninKeys(certificateOptions); - // TODO implement means to add clients or api scopes for other plugins. // TODO split scopes (kyoo.read should be task.read, video.read etc) - - // services.AddAuthentication(o => - // { - // o.DefaultScheme = IdentityConstants.ApplicationScheme; - // o.DefaultSignInScheme = IdentityConstants.ExternalScheme; - // }) - // .AddIdentityCookies(_ => { }); + services.AddAuthentication() .AddJwtBearer(options => { @@ -171,10 +132,10 @@ namespace Kyoo.Authentication { options.AddPolicy(permission, policy => { - // policy.AuthenticationSchemes.Add(IdentityConstants.ApplicationScheme); policy.AuthenticationSchemes.Add(JwtBearerDefaults.AuthenticationScheme); policy.AddRequirements(new AuthRequirement(permission)); - // policy.RequireScope($"kyoo.{permission.ToLower()}"); + // Scopes are disables to support default permissions. + // To enable them, use the following line: policy.RequireScope($"kyoo.{permission.ToLower()}"); }); } }); diff --git a/Kyoo.Authentication/Certificates.cs b/Kyoo.Authentication/Controllers/Certificates.cs similarity index 100% rename from Kyoo.Authentication/Certificates.cs rename to Kyoo.Authentication/Controllers/Certificates.cs diff --git a/Kyoo.Authentication/Controllers/UserStore.cs b/Kyoo.Authentication/Controllers/UserStore.cs deleted file mode 100644 index 0649796f..00000000 --- a/Kyoo.Authentication/Controllers/UserStore.cs +++ /dev/null @@ -1,133 +0,0 @@ -using System; -using System.Threading; -using System.Threading.Tasks; -using Kyoo.Controllers; -using Kyoo.Models; -using Microsoft.AspNetCore.Identity; - -namespace Kyoo.Authentication -{ - /// - /// An implementation of an that uses an . - /// - public class UserStore : IUserStore - { - /// - /// The user repository used to store users. - /// - private readonly IUserRepository _users; - - /// - /// Create a new . - /// - /// The user repository to use - public UserStore(IUserRepository users) - { - _users = users; - } - - - /// - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - - /// - /// Implementation of the IDisposable pattern - /// - /// True if this class should be disposed. - protected virtual void Dispose(bool disposing) - { - bool _ = disposing; - // Not implemented because this class has nothing to dispose. - } - - /// - public Task GetUserIdAsync(User user, CancellationToken cancellationToken) - { - return Task.FromResult(user.ID.ToString()); - } - - /// - public Task GetUserNameAsync(User user, CancellationToken cancellationToken) - { - return Task.FromResult(user.Username); - } - - /// - public Task SetUserNameAsync(User user, string userName, CancellationToken cancellationToken) - { - user.Username = userName; - return Task.CompletedTask; - } - - /// - public Task GetNormalizedUserNameAsync(User user, CancellationToken cancellationToken) - { - return Task.FromResult(user.Slug); - } - - /// - public Task SetNormalizedUserNameAsync(User user, string normalizedName, CancellationToken cancellationToken) - { - user.Slug = normalizedName; - return Task.CompletedTask; - } - - /// - public async Task CreateAsync(User user, CancellationToken cancellationToken) - { - try - { - await _users.Create(user); - return IdentityResult.Success; - } - catch (Exception ex) - { - return IdentityResult.Failed(new IdentityError {Code = ex.GetType().Name, Description = ex.Message}); - } - } - - /// - public async Task UpdateAsync(User user, CancellationToken cancellationToken) - { - try - { - await _users.Edit(user, false); - return IdentityResult.Success; - } - catch (Exception ex) - { - return IdentityResult.Failed(new IdentityError {Code = ex.GetType().Name, Description = ex.Message}); - } - } - - /// - public async Task DeleteAsync(User user, CancellationToken cancellationToken) - { - try - { - await _users.Delete(user); - return IdentityResult.Success; - } - catch (Exception ex) - { - return IdentityResult.Failed(new IdentityError {Code = ex.GetType().Name, Description = ex.Message}); - } - } - - /// - public Task FindByIdAsync(string userId, CancellationToken cancellationToken) - { - return _users.GetOrDefault(int.Parse(userId)); - } - - /// - public Task FindByNameAsync(string normalizedUserName, CancellationToken cancellationToken) - { - return _users.GetOrDefault(normalizedUserName); - } - } -} \ No newline at end of file diff --git a/Kyoo.Authentication/IdentityContext.cs b/Kyoo.Authentication/Models/IdentityContext.cs similarity index 69% rename from Kyoo.Authentication/IdentityContext.cs rename to Kyoo.Authentication/Models/IdentityContext.cs index 071a601b..e6ca3353 100644 --- a/Kyoo.Authentication/IdentityContext.cs +++ b/Kyoo.Authentication/Models/IdentityContext.cs @@ -4,8 +4,15 @@ using IdentityServer4.Models; namespace Kyoo.Authentication { + /// + /// The hard coded context of the identity server. + /// public static class IdentityContext { + /// + /// The list of identity resources supported (email, profile and openid) + /// + /// The list of identity resources supported public static IEnumerable GetIdentityResources() { return new List @@ -16,6 +23,13 @@ namespace Kyoo.Authentication }; } + /// + /// The list of officially supported clients. + /// + /// + /// You can add custom clients in the settings.json file. + /// + /// The list of officially supported clients. public static IEnumerable GetClients() { return new List @@ -40,6 +54,10 @@ namespace Kyoo.Authentication }; } + /// + /// The list of scopes supported by the API. + /// + /// The list of scopes public static IEnumerable GetScopes() { return new[] @@ -67,6 +85,10 @@ namespace Kyoo.Authentication }; } + /// + /// The list of APIs (this is used to create Audiences) + /// + /// The list of apis public static IEnumerable GetApis() { return new[] diff --git a/Kyoo/settings.json b/Kyoo/settings.json index ff3fffd3..34b24da9 100644 --- a/Kyoo/settings.json +++ b/Kyoo/settings.json @@ -35,7 +35,8 @@ "default": [], "newUser": ["read", "play", "write", "admin"] }, - "profilePicturePath": "users/" + "profilePicturePath": "users/", + "clients": [] },