using MesaFabApproval.Shared.Services; using NLog.Web; using MesaFabApprovalAPI.Services; using Microsoft.OpenApi.Models; using dotenv.net; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.IdentityModel.Tokens; using System.Text; using Microsoft.AspNetCore.Authorization; using MesaFabApproval.API.Services; using NLog.Extensions.Logging; using MesaFabApproval.API.Clients; using System.Net.Mail; WebApplicationBuilder builder = WebApplication.CreateBuilder(args); DotEnv.Load(); builder.Logging.ClearProviders(); builder.Logging.SetMinimumLevel(LogLevel.Trace); builder.Logging.AddNLog(); builder.Services.AddMemoryCache(); string jwtIssuer = Environment.GetEnvironmentVariable("FabApprovalJwtIssuer") ?? throw new ArgumentNullException("FabApprovalJwtIssuer environment variable not found"); string jwtAudience = Environment.GetEnvironmentVariable("FabApprovalJwtAudience") ?? throw new ArgumentNullException("FabApprovalJwtAudience environment variable not found"); string jwtKey = Environment.GetEnvironmentVariable("FabApprovalJwtKey") ?? throw new ArgumentNullException("FabApprovalJwtKey environment variable not found"); builder.Services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(options => { options.RequireHttpsMetadata = false; options.SaveToken = true; options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuerSigningKey = true, ValidIssuer = jwtIssuer, ValidateAudience = false, IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtKey)), ClockSkew = TimeSpan.Zero }; }); builder.Services.AddAuthorization(options => { options.DefaultPolicy = new AuthorizationPolicyBuilder() .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme) .RequireAuthenticatedUser() .Build(); }); builder.Services.AddHttpClient(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped((serviceProvider) => { return new SmtpClient("mailrelay-external.infineon.com"); }); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "Mesa Fab Approval API", Version = "v1" }); c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme { In = ParameterLocation.Header, Description = "Please insert JWT with Bearer into field", Name = "Authorization", Type = SecuritySchemeType.ApiKey }); c.AddSecurityRequirement(new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer" } }, new string[] { } } }); }); builder.Services.AddCors(options => { options.AddDefaultPolicy( builder => { builder.AllowAnyOrigin() .AllowAnyHeader() .AllowAnyMethod(); }); }); WebApplication app = builder.Build(); app.UseCors(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseHttpsRedirection(); app.UseAuthentication(); app.UseAuthorization(); app.MapControllers(); app.Run();