:drunk: Write shit code trying to split up the Auth (WIP)

This commit is contained in:
2025-07-06 12:58:18 +08:00
parent 5757526ea5
commit 6a3d04af3d
224 changed files with 1889 additions and 36885 deletions

View File

@ -0,0 +1,115 @@
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.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<PassDatabase>(options =>
{
options.UseNpgsql(builder.Configuration.GetConnectionString("App"),
o => o.UseNodaTime().UseNetTopologySuite().UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery));
});
// Add custom services
builder.Services.AddScoped<AccountService>();
builder.Services.AddScoped<AuthService>();
builder.Services.AddScoped<MagicSpellService>();
builder.Services.AddScoped<AccountEventService>();
builder.Services.AddScoped<AccountUsernameService>();
builder.Services.AddScoped<NotificationService>();
builder.Services.AddScoped<RelationshipService>();
builder.Services.AddScoped<EmailService>();
builder.Services.AddScoped<PermissionService>();
// Add OIDC services
builder.Services.AddScoped<OidcProviderService>();
builder.Services.AddScoped<AppleOidcService>();
builder.Services.AddScoped<GoogleOidcService>();
builder.Services.AddScoped<MicrosoftOidcService>();
builder.Services.AddScoped<DiscordOidcService>();
builder.Services.AddScoped<GitHubOidcService>();
builder.Services.AddScoped<AfdianOidcService>();
// Add other services
builder.Services.AddSingleton<ICacheService, InMemoryCacheService>();
builder.Services.AddSingleton<FlushBufferService>();
builder.Services.AddHttpContextAccessor();
builder.Services.AddHttpClient();
// Configure OIDC Provider Options
builder.Services.Configure<OidcProviderOptions>(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<PassDatabaseRecyclingJob>(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();