diff --git a/Kyoo/Controllers/StartupCode.cs b/Kyoo/Controllers/StartupCode.cs index 4e161945..fd993d61 100644 --- a/Kyoo/Controllers/StartupCode.cs +++ b/Kyoo/Controllers/StartupCode.cs @@ -1,6 +1,7 @@ using System; using System.Threading; using System.Threading.Tasks; +using IdentityServer4.EntityFramework.DbContexts; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -20,6 +21,8 @@ namespace Kyoo.Controllers using (IServiceScope serviceScope = _serviceProvider.CreateScope()) { serviceScope.ServiceProvider.GetService().Database.EnsureCreated(); + serviceScope.ServiceProvider.GetService().Database.EnsureCreated(); + serviceScope.ServiceProvider.GetService().Database.EnsureCreated(); // Use the next line if the database is not SQLite (SQLite doesn't support complexe migrations). // serviceScope.ServiceProvider.GetService().Database.Migrate();; diff --git a/Kyoo/DatabaseContext.cs b/Kyoo/DatabaseContext.cs index 1188581e..4c5712ce 100644 --- a/Kyoo/DatabaseContext.cs +++ b/Kyoo/DatabaseContext.cs @@ -9,7 +9,7 @@ namespace Kyoo { public class DatabaseContext : DbContext { - public DatabaseContext(DbContextOptions options) : base(options) { } + public DatabaseContext(DbContextOptions options) : base(options) { } public DbSet Libraries { get; set; } public DbSet Collections { get; set; } diff --git a/Kyoo/IdentityContext.cs b/Kyoo/IdentityContext.cs new file mode 100644 index 00000000..d612e321 --- /dev/null +++ b/Kyoo/IdentityContext.cs @@ -0,0 +1,47 @@ +using System.Collections.Generic; +using IdentityServer4.Models; + +namespace Kyoo +{ + public class IdentityContext + { + public static IEnumerable GetIdentityResources() + { + return new List + { + new IdentityResources.OpenId(), + new IdentityResources.Email(), + new IdentityResources.Profile() + }; + } + + public static IEnumerable GetApis() + { + return new[] + { + new ApiResource + { + Name = "Kyoo", + Scopes = + { + new Scope + { + Name = "kyoo.read", + DisplayName = "Read only access to the API.", + }, + new Scope + { + Name = "kyoo.write", + DisplayName = "Read and write access to the public API" + }, + new Scope + { + Name = "kyoo.admin", + DisplayName = "Full access to the admin's API and the public API." + } + } + } + }; + } + } +} \ No newline at end of file diff --git a/Kyoo/Kyoo.csproj b/Kyoo/Kyoo.csproj index 39aa0c23..0ad06118 100644 --- a/Kyoo/Kyoo.csproj +++ b/Kyoo/Kyoo.csproj @@ -22,6 +22,7 @@ + @@ -88,6 +89,6 @@ - + diff --git a/Kyoo/Startup.cs b/Kyoo/Startup.cs index 4711d20c..600d447d 100644 --- a/Kyoo/Startup.cs +++ b/Kyoo/Startup.cs @@ -1,3 +1,7 @@ +using System.Collections.Generic; +using IdentityServer4.EntityFramework.DbContexts; +using IdentityServer4.Models; +using IdentityServer4.Stores; using Kyoo.Controllers; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; @@ -34,9 +38,18 @@ namespace Kyoo services.AddDbContext(options => options.UseLazyLoadingProxies() .UseSqlite(Configuration.GetConnectionString("Database"))); - // services.AddIdentity() - // .AddEntityFrameworkStores() - // services.AddIdentityServer(); + services.AddIdentityServer() + .AddConfigurationStore(options => + { + options.ConfigureDbContext = builder => builder.UseSqlite(Configuration.GetConnectionString("Database")); + }) + .AddOperationalStore(options => + { + options.ConfigureDbContext = builder => builder.UseSqlite(Configuration.GetConnectionString("Database")); + options.EnableTokenCleanup = true; + }) + .AddInMemoryIdentityResources(IdentityContext.GetIdentityResources()) + .AddInMemoryApiResources(IdentityContext.GetApis()); services.AddScoped(); services.AddScoped(); @@ -74,13 +87,14 @@ namespace Kyoo return next(); }); - //app.UseHttpsRedirection(); app.UseStaticFiles(); if (!env.IsDevelopment()) app.UseSpaStaticFiles(); app.UseRouting(); + app.UseIdentityServer(); + app.UseEndpoints(endpoints => { endpoints.MapControllerRoute("API Route", "api/{controller=Home}/{action=Index}/{id?}");