using Worker.Models; using Microsoft.EntityFrameworkCore; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.EntityFrameworkCore.Metadata.Internal; using System.Text; using Worker.Services; using Microsoft.OpenApi.Models; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(options => { options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme() { Name = "Authorization", In = ParameterLocation.Header, Type = SecuritySchemeType.Http, Scheme = "Bearer" }); options.AddSecurityRequirement(new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer" } }, Array.Empty() } }); }); // Initialize Database with correct connection string. builder.Services.AddEntityFrameworkNpgsql().AddDbContext(options => options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"))); // Add Identity services to the container. builder.Services.AddIdentity() .AddEntityFrameworkStores() .AddDefaultTokenProviders(); // Set up JWT authentication builder.Services.AddAuthorization(); builder.Services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(options => { options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters { ValidateIssuerSigningKey = true, // Make sure the token is valid from the key IssuerSigningKey = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["JwtSettings:Secret"]!)), ValidateIssuer = false, // Not caring about where the token is coming from ValidateAudience = false, // Not caring about who is receiving this token ValidateLifetime = true // Ensure it's not expired }; }); builder.Services.AddScoped(); // TODO Implement CORS functionality for API builder.Services.AddCors(options => { options.AddPolicy(name: "frontend", policy => { policy.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader(); }); }); var app = builder.Build(); // Apply migrations to database using (var scope = app.Services.CreateScope()) { var clothingItemContext = scope.ServiceProvider.GetRequiredService(); clothingItemContext.Database.Migrate(); } // Apply CORS policy app.UseCors("frontend"); app.UseSwagger(); app.UseSwaggerUI(); // app.UseHttpsRedirection(); app.UseAuthentication(); app.UseAuthorization(); app.MapControllers(); app.Run();