using DysonNetwork.Pass.Data; using DysonNetwork.Pass.Features.Account; using DysonNetwork.Pass.Features.Auth; using DysonNetwork.Pass.Features.Auth.OidcProvider.Options; using DysonNetwork.Pass.Features.Auth.OidcProvider.Services; using DysonNetwork.Pass.Features.Auth.OpenId; using DysonNetwork.Pass.Storage; using DysonNetwork.Pass.Storage.Handlers; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; using NodaTime; using NodaTime.Serialization.SystemTextJson; using System.Text; using DysonNetwork.Pass.Email; using DysonNetwork.Pass.Developer; using DysonNetwork.Pass.Features.Account; using DysonNetwork.Pass.Features.Account.Services; using DysonNetwork.Pass.Permission; using Quartz; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers().AddJsonOptions(options => { options.JsonSerializerOptions.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb); }); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); // Configure AppDatabase builder.Services.AddDbContext(options => { options.UseNpgsql(builder.Configuration.GetConnectionString("App"), o => o.UseNodaTime().UseNetTopologySuite().UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery)); }); // Add custom services builder.Services.AddScoped(); // Old AuthService is being replaced with the new authentication services // builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); // Add authentication services builder.Services.AddAuthServices(); // Add OIDC services builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); // Add other services builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddHttpContextAccessor(); builder.Services.AddHttpClient(); // Configure OIDC Provider Options builder.Services.Configure(builder.Configuration.GetSection("OidcProvider")); // Configure Authentication builder.Services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = false, // Will be validated by the OidcProviderService ValidateLifetime = true, ValidateIssuerSigningKey = true, ValidIssuer = builder.Configuration["OidcProvider:IssuerUri"], IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]!)) }; }); // Configure Quartz for background jobs builder.Services.AddQuartz(q => { var jobKey = new JobKey("PassDatabaseRecyclingJob"); q.AddJob(opts => opts.WithIdentity(jobKey)); q.AddTrigger(opts => opts .ForJob(jobKey) .WithSimpleSchedule(s => s.WithIntervalInHours(24).RepeatForever()) .StartAt(DateBuilder.EvenHourDate(DateTimeOffset.UtcNow).AddHours(1)) ); }); builder.Services.AddQuartzHostedService(q => q.WaitForJobsToComplete = true); var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseHttpsRedirection(); app.UseAuthentication(); app.UseAuthorization(); app.MapControllers(); app.Run();