using Core_Archi.Helpers; using AuthHex; using AuthHex.Infra.UoW; using AuthHex.Interfaces; using AuthHex.Repos; using AuthHex.Services.UserManager; using AuthHex.Services.RecoveryManager; using AuthHex.Utility; using AuthSystem.API.Utils; using Microsoft.EntityFrameworkCore; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.IdentityModel.Tokens; using System.Text; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); builder.Services.AddDbContext(options => { var connectionString = builder.Configuration.GetConnectionString("DefaultConnection"); options.UseMySql(connectionString, new MySqlServerVersion(new Version(8, 0, 21))); }); // Configure RSA Key Provider var privateKeyXml = builder.Configuration["Jwt:RsaPrivateKey"]!; var rsaKeyProvider = new RsaKeyProvider(privateKeyXml); builder.Services.AddSingleton(rsaKeyProvider); // Add JWT Authentication builder.Services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true, ValidIssuer = builder.Configuration["Jwt:Issuer"], ValidAudience = builder.Configuration["Jwt:Audience"], IssuerSigningKey = rsaKeyProvider.GetPublicKey(), ClockSkew = TimeSpan.Zero }; }); builder.Services.AddAuthorization(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddHttpClient(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); var configuredOrigins = builder.Configuration.GetSection("Cors:AllowedOrigins").Get() ?? Array.Empty(); var allowedOrigins = configuredOrigins .Where(origin => !string.IsNullOrWhiteSpace(origin) && origin != "*") .ToList(); var frontendUrl = builder.Configuration["AppSettings:FrontendUrl"]; if (!string.IsNullOrWhiteSpace(frontendUrl) && !allowedOrigins.Contains(frontendUrl, StringComparer.OrdinalIgnoreCase)) { allowedOrigins.Add(frontendUrl); } // Add CORS policy builder.Services.AddCors(options => { options.AddPolicy("AllowAll", policy => { if (allowedOrigins.Count > 0) { policy.WithOrigins(allowedOrigins.ToArray()) .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials(); return; } policy.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader(); }); }); var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } if (!app.Environment.IsDevelopment()) { app.UseHttpsRedirection(); } app.UseRouting(); app.UseCors("AllowAll"); app.UseAuthentication(); app.UseAuthorization(); app.MapControllers(); app.Run();