2018-10-26 07:22:53 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Security.Claims;
|
2021-07-01 06:56:12 +02:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2018-10-26 07:22:53 +02:00
|
|
|
|
using IdentityServer4.EntityFramework.DbContexts;
|
|
|
|
|
using IdentityServer4.EntityFramework.Mappers;
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Microsoft.Net.Http.Headers;
|
|
|
|
|
using Teknik.Configuration;
|
|
|
|
|
using Teknik.IdentityServer.Configuration;
|
|
|
|
|
using Teknik.IdentityServer.Security;
|
|
|
|
|
using Teknik.IdentityServer.Middleware;
|
|
|
|
|
using Teknik.Logging;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Teknik.IdentityServer.Models;
|
|
|
|
|
using IdentityServer4.Services;
|
2021-07-01 06:56:12 +02:00
|
|
|
|
using Teknik.WebCommon.Middleware;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
using Teknik.Middleware;
|
|
|
|
|
using Teknik.WebCommon;
|
|
|
|
|
using Teknik.IdentityServer.Controllers;
|
2018-10-26 07:22:53 +02:00
|
|
|
|
|
|
|
|
|
namespace Teknik.IdentityServer
|
|
|
|
|
{
|
|
|
|
|
public class Startup
|
|
|
|
|
{
|
2021-07-01 06:56:12 +02:00
|
|
|
|
public Startup(IConfiguration configuration, IWebHostEnvironment env)
|
2018-10-26 07:22:53 +02:00
|
|
|
|
{
|
|
|
|
|
Configuration = configuration;
|
|
|
|
|
Environment = env;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IConfiguration Configuration { get; }
|
2021-07-01 06:56:12 +02:00
|
|
|
|
public IWebHostEnvironment Environment { get; }
|
2018-10-26 07:22:53 +02:00
|
|
|
|
|
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
|
{
|
2021-07-01 06:56:12 +02:00
|
|
|
|
string dataDir = (Configuration != null) ? Configuration["ConfigDirectory"] : null;
|
2019-02-03 07:55:47 +01:00
|
|
|
|
if (string.IsNullOrEmpty(dataDir))
|
|
|
|
|
{
|
|
|
|
|
string baseDir = Environment.ContentRootPath;
|
|
|
|
|
dataDir = Path.Combine(baseDir, "App_Data");
|
|
|
|
|
}
|
2018-10-26 07:22:53 +02:00
|
|
|
|
AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);
|
|
|
|
|
|
|
|
|
|
var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
|
|
|
|
|
|
|
|
|
|
// Create Configuration Singleton
|
|
|
|
|
services.AddScoped<Config, Config>(opt => Config.Load(dataDir));
|
|
|
|
|
|
|
|
|
|
// Build an intermediate service provider
|
|
|
|
|
var sp = services.BuildServiceProvider();
|
|
|
|
|
|
|
|
|
|
// Resolve the services from the service provider
|
|
|
|
|
var config = sp.GetService<Config>();
|
2021-07-01 06:56:12 +02:00
|
|
|
|
var devEnv = config?.DevEnvironment ?? true;
|
|
|
|
|
var defaultConn = config?.DbConnection ?? string.Empty;
|
|
|
|
|
var authority = config?.UserConfig?.IdentityServerConfig?.Authority ?? string.Empty;
|
2021-07-13 05:21:50 +02:00
|
|
|
|
var signingCert = config?.UserConfig?.IdentityServerConfig?.SigningCertificate ?? string.Empty;
|
2018-10-26 07:22:53 +02:00
|
|
|
|
|
2021-07-01 06:56:12 +02:00
|
|
|
|
if (devEnv)
|
2019-01-29 03:19:01 +01:00
|
|
|
|
{
|
2021-07-01 06:56:12 +02:00
|
|
|
|
Environment.EnvironmentName = Environments.Development;
|
2019-01-29 03:19:01 +01:00
|
|
|
|
}
|
2019-01-29 04:29:13 +01:00
|
|
|
|
else
|
|
|
|
|
{
|
2021-07-01 06:56:12 +02:00
|
|
|
|
Environment.EnvironmentName = Environments.Production;
|
2019-01-29 04:29:13 +01:00
|
|
|
|
}
|
2019-01-29 03:19:01 +01:00
|
|
|
|
|
2018-10-26 07:22:53 +02:00
|
|
|
|
services.ConfigureApplicationCookie(options =>
|
|
|
|
|
{
|
|
|
|
|
options.Cookie.Name = "TeknikAuth";
|
|
|
|
|
options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
|
|
|
|
|
options.Cookie.SameSite = Microsoft.AspNetCore.Http.SameSiteMode.Strict;
|
|
|
|
|
options.ExpireTimeSpan = TimeSpan.FromDays(30);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
services.AddHttpsRedirection(options =>
|
|
|
|
|
{
|
2019-01-29 03:19:01 +01:00
|
|
|
|
options.RedirectStatusCode = (Environment.IsDevelopment()) ? StatusCodes.Status307TemporaryRedirect : StatusCodes.Status308PermanentRedirect;
|
|
|
|
|
#if DEBUG
|
|
|
|
|
options.HttpsPort = 5050;
|
|
|
|
|
#else
|
|
|
|
|
options.HttpsPort = 443;
|
|
|
|
|
#endif
|
2018-10-26 07:22:53 +02:00
|
|
|
|
});
|
|
|
|
|
|
2021-07-01 06:56:12 +02:00
|
|
|
|
services.AddScoped<IErrorController, ErrorController>();
|
|
|
|
|
services.AddControllersWithViews()
|
2021-07-13 05:21:50 +02:00
|
|
|
|
.AddControllersAsServices()
|
|
|
|
|
.AddNewtonsoftJson();
|
2021-07-01 06:56:12 +02:00
|
|
|
|
|
2018-10-26 07:22:53 +02:00
|
|
|
|
// Sessions
|
|
|
|
|
services.AddResponseCaching();
|
|
|
|
|
services.AddMemoryCache();
|
|
|
|
|
services.AddSession();
|
|
|
|
|
|
|
|
|
|
// Set the anti-forgery cookie name
|
|
|
|
|
services.AddAntiforgery(options =>
|
|
|
|
|
{
|
|
|
|
|
options.Cookie.Name = "TeknikAuthAntiForgery";
|
|
|
|
|
options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
|
|
|
|
|
options.Cookie.SameSite = Microsoft.AspNetCore.Http.SameSiteMode.Strict;
|
|
|
|
|
});
|
|
|
|
|
|
2021-07-01 06:56:12 +02:00
|
|
|
|
//services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
|
2018-10-26 07:22:53 +02:00
|
|
|
|
|
2021-07-01 06:56:12 +02:00
|
|
|
|
services.AddDbContext<ApplicationDbContext>(options => options
|
|
|
|
|
.UseLazyLoadingProxies()
|
|
|
|
|
.UseSqlServer(defaultConn, sqlOptions => sqlOptions.MigrationsAssembly(migrationsAssembly)),
|
|
|
|
|
ServiceLifetime.Transient);
|
2018-10-26 07:22:53 +02:00
|
|
|
|
|
|
|
|
|
services.AddIdentity<ApplicationUser, IdentityRole>(options =>
|
|
|
|
|
{
|
|
|
|
|
options.Password = new PasswordOptions()
|
|
|
|
|
{
|
|
|
|
|
RequireDigit = false,
|
|
|
|
|
RequiredLength = 4,
|
|
|
|
|
RequiredUniqueChars = 1,
|
|
|
|
|
RequireLowercase = false,
|
|
|
|
|
RequireNonAlphanumeric = false,
|
|
|
|
|
RequireUppercase = false
|
|
|
|
|
};
|
|
|
|
|
})
|
|
|
|
|
.AddEntityFrameworkStores<ApplicationDbContext>()
|
|
|
|
|
.AddDefaultTokenProviders();
|
|
|
|
|
|
2021-07-13 05:21:50 +02:00
|
|
|
|
var identityBuilder = services.AddIdentityServer(options =>
|
2018-10-26 07:22:53 +02:00
|
|
|
|
{
|
|
|
|
|
options.Events.RaiseErrorEvents = true;
|
|
|
|
|
options.Events.RaiseInformationEvents = true;
|
|
|
|
|
options.Events.RaiseFailureEvents = true;
|
|
|
|
|
options.Events.RaiseSuccessEvents = true;
|
|
|
|
|
options.UserInteraction.ErrorUrl = "/Error/IdentityError";
|
|
|
|
|
options.UserInteraction.ErrorIdParameter = "errorId";
|
2019-01-27 05:40:18 +01:00
|
|
|
|
options.Cors.CorsPaths.Add(new PathString("/connect/authorize"));
|
|
|
|
|
options.Cors.CorsPaths.Add(new PathString("/connect/endsession"));
|
|
|
|
|
options.Cors.CorsPaths.Add(new PathString("/connect/checksession"));
|
|
|
|
|
options.Cors.CorsPaths.Add(new PathString("/connect/introspect"));
|
2019-01-29 08:15:00 +01:00
|
|
|
|
options.Caching.ClientStoreExpiration = TimeSpan.FromHours(1);
|
2018-10-26 07:22:53 +02:00
|
|
|
|
})
|
|
|
|
|
.AddOperationalStore(options =>
|
|
|
|
|
options.ConfigureDbContext = builder =>
|
2021-07-01 06:56:12 +02:00
|
|
|
|
builder.UseSqlServer(defaultConn, sqlOptions => sqlOptions.MigrationsAssembly(migrationsAssembly)))
|
2018-10-26 07:22:53 +02:00
|
|
|
|
.AddConfigurationStore(options =>
|
|
|
|
|
options.ConfigureDbContext = builder =>
|
2021-07-01 06:56:12 +02:00
|
|
|
|
builder.UseSqlServer(defaultConn, sqlOptions => sqlOptions.MigrationsAssembly(migrationsAssembly)))
|
2019-01-29 08:15:00 +01:00
|
|
|
|
.AddConfigurationStoreCache()
|
2018-10-26 07:22:53 +02:00
|
|
|
|
.AddAspNetIdentity<ApplicationUser>()
|
2021-07-13 05:21:50 +02:00
|
|
|
|
.AddRedirectUriValidator<TeknikRedirectUriValidator>();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(signingCert))
|
|
|
|
|
{
|
|
|
|
|
identityBuilder.AddSigningCredential($"CN={signingCert}");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
identityBuilder.AddDeveloperSigningCredential();
|
|
|
|
|
}
|
2018-10-26 07:22:53 +02:00
|
|
|
|
|
|
|
|
|
services.AddAuthorization(options =>
|
|
|
|
|
{
|
|
|
|
|
foreach (var policy in Policies.Get())
|
|
|
|
|
{
|
|
|
|
|
options.AddPolicy(policy.Name, p =>
|
|
|
|
|
{
|
|
|
|
|
foreach (var scope in policy.Scopes)
|
|
|
|
|
{
|
|
|
|
|
p.RequireScope(scope);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
services.AddAuthentication("Bearer")
|
|
|
|
|
.AddIdentityServerAuthentication(options =>
|
|
|
|
|
{
|
2021-07-01 06:56:12 +02:00
|
|
|
|
options.Authority = authority;
|
2018-10-26 07:22:53 +02:00
|
|
|
|
options.RequireHttpsMetadata = true;
|
|
|
|
|
|
|
|
|
|
options.ApiName = "auth-api";
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
services.AddTransient<IPasswordHasher<ApplicationUser>, TeknikPasswordHasher>();
|
|
|
|
|
services.AddTransient<IProfileService, TeknikProfileService>();
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-01 06:56:12 +02:00
|
|
|
|
public void Configure(IApplicationBuilder app, ApplicationDbContext dbContext, Config config)
|
2018-10-26 07:22:53 +02:00
|
|
|
|
{
|
2021-07-01 06:56:12 +02:00
|
|
|
|
// Create and Migrate the database
|
|
|
|
|
dbContext?.Database?.Migrate();
|
|
|
|
|
|
|
|
|
|
// Setup static files and cache them client side
|
|
|
|
|
app.UseStaticFiles(new StaticFileOptions
|
|
|
|
|
{
|
|
|
|
|
OnPrepareResponse = ctx =>
|
|
|
|
|
{
|
|
|
|
|
ctx.Context.Response.Headers[HeaderNames.CacheControl] = "public,max-age=" + 31536000;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Initiate Routing
|
|
|
|
|
app.UseRouting();
|
|
|
|
|
|
2018-10-26 07:22:53 +02:00
|
|
|
|
// Setup the HttpContext
|
|
|
|
|
app.UseHttpContextSetup();
|
|
|
|
|
|
|
|
|
|
// HttpContext Session
|
|
|
|
|
app.UseSession(new SessionOptions()
|
|
|
|
|
{
|
|
|
|
|
IdleTimeout = TimeSpan.FromMinutes(30),
|
|
|
|
|
Cookie = new CookieBuilder()
|
|
|
|
|
{
|
|
|
|
|
Name = "TeknikAuthSession",
|
|
|
|
|
SecurePolicy = CookieSecurePolicy.Always,
|
|
|
|
|
SameSite = Microsoft.AspNetCore.Http.SameSiteMode.Strict
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2021-07-01 06:56:12 +02:00
|
|
|
|
// Force a HTTPS redirection (301)
|
|
|
|
|
app.UseHttpsRedirection();
|
2018-10-26 07:22:53 +02:00
|
|
|
|
|
2021-07-01 06:56:12 +02:00
|
|
|
|
// Use Exception Handling
|
|
|
|
|
app.UseErrorHandler();
|
2018-10-26 07:22:53 +02:00
|
|
|
|
|
|
|
|
|
// Custom Middleware
|
|
|
|
|
app.UseBlacklist();
|
|
|
|
|
app.UseCORS();
|
|
|
|
|
app.UseCSP();
|
|
|
|
|
app.UseSecurityHeaders();
|
|
|
|
|
|
|
|
|
|
// Cache Responses
|
|
|
|
|
app.UseResponseCaching();
|
|
|
|
|
|
2021-07-01 06:56:12 +02:00
|
|
|
|
if (config != null)
|
|
|
|
|
InitializeDbTestDataAsync(app, config);
|
2018-10-26 07:22:53 +02:00
|
|
|
|
|
2021-07-01 06:56:12 +02:00
|
|
|
|
app.UseIdentityServer();
|
|
|
|
|
|
2021-07-13 05:21:50 +02:00
|
|
|
|
// Authorize all the things!
|
|
|
|
|
app.UseAuthentication();
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
2021-07-01 06:56:12 +02:00
|
|
|
|
app.UseEndpoints(endpoints =>
|
2018-10-26 07:22:53 +02:00
|
|
|
|
{
|
2021-07-01 06:56:12 +02:00
|
|
|
|
endpoints.MapDefaultControllerRoute();
|
2018-10-26 07:22:53 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-01 06:56:12 +02:00
|
|
|
|
private static void InitializeDbTestDataAsync(IApplicationBuilder app, Config config)
|
2018-10-26 07:22:53 +02:00
|
|
|
|
{
|
|
|
|
|
using (var scope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
|
|
|
|
|
{
|
|
|
|
|
scope.ServiceProvider.GetRequiredService<PersistedGrantDbContext>().Database.Migrate();
|
|
|
|
|
scope.ServiceProvider.GetRequiredService<ConfigurationDbContext>().Database.Migrate();
|
|
|
|
|
scope.ServiceProvider.GetRequiredService<ApplicationDbContext>().Database.Migrate();
|
|
|
|
|
|
|
|
|
|
var context = scope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();
|
|
|
|
|
|
|
|
|
|
if (!context.Clients.Any())
|
|
|
|
|
{
|
|
|
|
|
foreach (var client in Clients.Get(config))
|
|
|
|
|
{
|
|
|
|
|
context.Clients.Add(client.ToEntity());
|
|
|
|
|
}
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!context.IdentityResources.Any())
|
|
|
|
|
{
|
|
|
|
|
foreach (var resource in Resources.GetIdentityResources())
|
|
|
|
|
{
|
|
|
|
|
context.IdentityResources.Add(resource.ToEntity());
|
|
|
|
|
}
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-01 06:56:12 +02:00
|
|
|
|
if (!context.ApiScopes.Any())
|
|
|
|
|
{
|
|
|
|
|
foreach (var apiScope in Resources.GetApiScopes())
|
|
|
|
|
{
|
|
|
|
|
context.ApiScopes.Add(apiScope.ToEntity());
|
|
|
|
|
}
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-26 07:22:53 +02:00
|
|
|
|
if (!context.ApiResources.Any())
|
|
|
|
|
{
|
|
|
|
|
foreach (var resource in Resources.GetApiResources(config))
|
|
|
|
|
{
|
|
|
|
|
context.ApiResources.Add(resource.ToEntity());
|
|
|
|
|
}
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|