isn/src/isnd/Startup.cs

179 lines
6.9 KiB
C#
Raw Normal View History

2026-07-05 17:22:57 +01:00

2023-03-14 00:15:42 +00:00
using Microsoft.AspNetCore.Builder;
2021-04-08 02:03:17 +01:00
using Microsoft.AspNetCore.Hosting;
2021-04-25 12:12:50 +01:00
using Microsoft.EntityFrameworkCore;
2022-09-26 21:54:22 +01:00
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
2021-04-25 12:12:50 +01:00
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
2022-09-26 21:54:22 +01:00
using Microsoft.Extensions.Hosting;
2021-08-15 19:09:01 +01:00
using isnd.Data;
using isnd.Interfaces;
using isnd.Services;
using isnd.Entities;
using isnd.Authorization;
using isnd.Data.Roles;
2021-06-18 23:57:02 +01:00
using Microsoft.AspNetCore.Authorization;
2021-07-05 12:55:52 +01:00
using Microsoft.Extensions.Options;
2021-07-11 22:13:48 +01:00
using isnd.Helpers;
2022-04-09 19:53:41 +01:00
using Microsoft.IdentityModel.Tokens;
2022-12-11 18:03:16 +00:00
using System;
2022-12-17 01:29:22 +00:00
using System.IO;
2023-04-09 03:28:02 +01:00
using Microsoft.AspNetCore.HttpOverrides;
2026-07-05 17:22:57 +01:00
using Microsoft.OpenApi;
2021-04-08 02:03:17 +01:00
2021-08-15 19:09:01 +01:00
namespace isnd
2021-04-08 02:03:17 +01:00
{
2023-03-14 00:15:42 +00:00
2026-07-05 17:22:57 +01:00
/**
* Startup class for configuring services and the app's request pipeline.
*/
2021-04-08 02:03:17 +01:00
public class Startup
{
2022-10-15 18:25:40 +01:00
2026-07-05 17:22:57 +01:00
/**
* Constructor for the Startup class.
* @param config The configuration object.
*/
2021-04-08 03:08:20 +01:00
public Startup(IConfiguration config)
2021-04-08 02:03:17 +01:00
{
Configuration = config;
}
2026-07-05 17:22:57 +01:00
/**
* The configuration object.
*/
2022-09-25 17:07:56 +01:00
public static IConfiguration Configuration { get; private set; }
2021-04-08 02:03:17 +01:00
2026-07-05 17:22:57 +01:00
/**
* This method gets called by the runtime. Use this method to add services to the container.
* @param services The service collection.
*/
2021-04-08 02:03:17 +01:00
public void ConfigureServices(IServiceCollection services)
{
2021-08-28 21:26:39 +01:00
var smtpSettingsconf = Configuration.GetSection("Smtp");
var isndSettingsconf = Configuration.GetSection("Isn");
var adminStartupListConf = Configuration.GetSection("AdminList");
var unleashConf = Configuration.GetSection("Unleash");
2021-04-08 03:08:20 +01:00
2023-04-09 03:28:02 +01:00
services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders =
ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
});
2021-08-28 21:26:39 +01:00
services.Configure<SmtpSettings>(smtpSettingsconf)
.Configure<IsndSettings>(isndSettingsconf)
.Configure<AdminStartupList>(adminStartupListConf)
.Configure<UnleashClientSettings>(unleashConf)
.Configure<MigrationsEndPointOptions>(o => o.Path = "~/migrate")
.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(
Configuration.GetConnectionString("DefaultConnection")))
2023-04-08 13:36:58 +01:00
.AddIdentity<ApplicationUser, IdentityRole>()
2021-06-18 23:57:02 +01:00
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
2023-04-08 13:36:58 +01:00
.AddSignInManager()
.AddDefaultUI()
.AddDefaultTokenProviders();
2022-09-26 21:54:22 +01:00
2023-04-08 13:36:58 +01:00
services.AddMvc(o => o.EnableEndpointRouting = false);
2023-01-29 13:04:50 +00:00
2021-04-08 03:08:20 +01:00
services.AddDataProtection();
2021-07-05 12:55:52 +01:00
2021-06-18 23:57:02 +01:00
services.AddAuthorization(options =>
{
2022-04-09 19:53:26 +01:00
options.AddPolicy(IsndConstants.RequireAdminPolicyName,
policy => policy.RequireRole(IsndConstants.AdministratorRoleName));
options.AddPolicy(IsndConstants.RequireValidApiKey, policy =>
2021-06-18 23:57:02 +01:00
policy.Requirements.Add(new ValidApiKeyRequirement()));
2021-08-28 21:26:39 +01:00
})
2022-04-09 19:53:41 +01:00
2021-08-28 21:26:39 +01:00
.AddTransient<IMailer, EmailSender>()
.AddTransient<IEmailSender, EmailSender>()
.AddTransient<IPackageManager, PackageManager>()
.AddSingleton<IAuthorizationHandler, ValidApiKeyRequirementHandler>()
.AddSingleton(s =>
2021-07-11 22:13:48 +01:00
{
2023-01-29 13:04:50 +00:00
var config = s.GetRequiredService<IOptions<UnleashClientSettings>>();
if (config.Value == null)
throw new System.Exception("No unleash client settings");
if (config.Value.ApiUrl == null)
throw new System.Exception("No unleash client ApiUrl");
if (config.Value.ClientApiKey == null)
throw new System.Exception("No unleash client ClientApiKey");
2026-07-05 17:22:57 +01:00
return s.GetRequiredService<Microsoft.AspNetCore.Hosting.IWebHostEnvironment>().CreateUnleashClient(config.Value);
2021-07-11 22:13:48 +01:00
});
2023-01-29 13:04:50 +00:00
2021-08-23 03:21:08 +01:00
2022-04-09 19:53:41 +01:00
services.AddAuthentication("Bearer")
.AddJwtBearer("Bearer", options =>
{
2023-02-06 21:43:18 +00:00
options.Authority = isndSettingsconf.GetValue<string>("ExternalUrl");
2022-04-09 19:53:41 +01:00
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateAudience = false
};
});
2023-02-06 21:43:18 +00:00
2023-03-14 00:15:42 +00:00
services.AddControllersWithViews()
2023-04-08 13:36:58 +01:00
.AddNewtonsoftJson(s =>
{
s.SerializerSettings.ReferenceResolverProvider = () => new NSJWebApiReferenceResolver();
});
2022-12-11 18:03:16 +00:00
services.AddSwaggerGen(options =>
2022-12-17 01:29:22 +00:00
{
2023-01-29 13:04:50 +00:00
options.SwaggerDoc("v1", new OpenApiInfo
2022-12-11 18:03:16 +00:00
{
Version = "v1",
2023-01-29 13:04:50 +00:00
Title = "isnd API",
Description = "An ASP.NET Core Web API for managing isnd items",
2022-12-11 18:03:16 +00:00
TermsOfService = new Uri("https://isn.pschneider.fr/terms"),
Contact = new OpenApiContact
{
Name = "Example Contact",
Url = new Uri("https://isn.pschneider.fr/contact")
},
License = new OpenApiLicense
{
Name = "Example License",
Url = new Uri("https://isn.pschneider.fr/license")
}
2022-12-17 01:29:22 +00:00
});
2023-01-29 13:04:50 +00:00
var xmlFilename = $"{typeof(Startup).Assembly.GetName().Name}.xml";
options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
2022-12-17 01:29:22 +00:00
});
2021-04-08 02:03:17 +01:00
}
2026-07-05 17:22:57 +01:00
/**
* This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
* @param app The application builder.
* @param env The web host environment.
* @param dbContext The application database context.
* @param isnSettingsOption The isnd settings options.
*/
2023-01-29 13:04:50 +00:00
public void Configure(IApplicationBuilder app,
2023-02-06 21:43:18 +00:00
IWebHostEnvironment env,
2023-04-08 13:36:58 +01:00
ApplicationDbContext dbContext,
IOptions<IsndSettings> isnSettingsOption)
2021-04-08 02:03:17 +01:00
{
2023-04-09 03:28:02 +01:00
app.UseForwardedHeaders();
2021-04-08 02:03:17 +01:00
if (env.IsDevelopment())
{
2021-04-25 12:12:50 +01:00
app.UseDeveloperExceptionPage();
app.UseMigrationsEndPoint();
2022-12-11 18:03:16 +00:00
app.UseSwagger();
app.UseSwaggerUI();
2021-04-08 02:03:17 +01:00
}
else
{
2022-06-19 14:17:49 +01:00
app.UseExceptionHandler("/Home/Error");
2022-09-26 21:54:22 +01:00
app.UseHsts();
dbContext.Database.Migrate();
2021-04-08 02:03:17 +01:00
}
2023-02-06 21:43:18 +00:00
app.UseStatusCodePages().UseStaticFiles().UseAuthentication();
app.UseMvcWithDefaultRoute();
2021-04-08 02:03:17 +01:00
}
}
}