From d42b9fc22eea0b700c553559eaa48b538d4cb731 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Bj=C3=B8ralt?= Date: Thu, 28 Sep 2023 14:41:24 +0200 Subject: [PATCH] cleanup swagger config --- backend/Api/BuildHelpers/ErrorHandler.cs | 12 ------ .../{SwaggerBuild.cs => SwaggerExtensions.cs} | 12 +++--- backend/Api/Options/AzureAdOptions.cs | 1 - backend/Api/Program.cs | 41 +++++++------------ 4 files changed, 21 insertions(+), 45 deletions(-) delete mode 100644 backend/Api/BuildHelpers/ErrorHandler.cs rename backend/Api/BuildHelpers/{SwaggerBuild.cs => SwaggerExtensions.cs} (69%) diff --git a/backend/Api/BuildHelpers/ErrorHandler.cs b/backend/Api/BuildHelpers/ErrorHandler.cs deleted file mode 100644 index 4099a1c4..00000000 --- a/backend/Api/BuildHelpers/ErrorHandler.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace Api.BuildHelpers; - -public abstract class ErrorHandler -{ - public static void ThrowRequirementsException(string customErrorMessage) - { - var errorMessage = - $"{customErrorMessage}. Environment: {Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}"; - Console.WriteLine(errorMessage); - throw new Exception(errorMessage); - } -} \ No newline at end of file diff --git a/backend/Api/BuildHelpers/SwaggerBuild.cs b/backend/Api/BuildHelpers/SwaggerExtensions.cs similarity index 69% rename from backend/Api/BuildHelpers/SwaggerBuild.cs rename to backend/Api/BuildHelpers/SwaggerExtensions.cs index 41a8b117..6f5dee67 100644 --- a/backend/Api/BuildHelpers/SwaggerBuild.cs +++ b/backend/Api/BuildHelpers/SwaggerExtensions.cs @@ -4,13 +4,13 @@ namespace Api.BuildHelpers; -public abstract class SwaggerBuild +public static class SwaggerExtensions { - public static void AddSwaggerOAuthSetupAction(AzureAdOptions? settings, SwaggerGenOptions c) + public static void ConfigureSwaggerAuthentication(this SwaggerGenOptions c, AzureAdOptions adOptions) { var scopes = new Dictionary { - { $"{settings.ApiScope}", "Access API backend on user behalf" } + { $"{adOptions.ApiScope}", "Access API backend on user behalf" } }; c.AddSecurityRequirement(new OpenApiSecurityRequirement @@ -20,7 +20,7 @@ public static void AddSwaggerOAuthSetupAction(AzureAdOptions? settings, SwaggerG { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "oauth2" } }, - new[] { settings.ApiScope } + new[] { adOptions.ApiScope } } }); @@ -31,8 +31,8 @@ public static void AddSwaggerOAuthSetupAction(AzureAdOptions? settings, SwaggerG { Implicit = new OpenApiOAuthFlow { - AuthorizationUrl = settings.AuthorizationUrl(), - TokenUrl = settings.TokenUrl(), + AuthorizationUrl = adOptions.AuthorizationUrl(), + TokenUrl = adOptions.TokenUrl(), Scopes = scopes } } diff --git a/backend/Api/Options/AzureAdOptions.cs b/backend/Api/Options/AzureAdOptions.cs index da911776..ea282518 100644 --- a/backend/Api/Options/AzureAdOptions.cs +++ b/backend/Api/Options/AzureAdOptions.cs @@ -3,7 +3,6 @@ namespace Api.Options; public class AzureAdOptions { public Uri Instance { get; set; } = null!; - public bool DisableAuthAd { get; set; } = false; public string ClientId { get; set; } = null!; public string TenantId { get; set; } = null!; public string ApiScope { get; set; } = null!; diff --git a/backend/Api/Program.cs b/backend/Api/Program.cs index d73f9793..33532d34 100644 --- a/backend/Api/Program.cs +++ b/backend/Api/Program.cs @@ -11,7 +11,7 @@ var connection = builder.Configuration.GetConnectionString("VibesDb"); if (string.IsNullOrEmpty(connection)) - ErrorHandler.ThrowRequirementsException("Could not find database connection string"); + throw new Exception("No connection string found"); builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddMicrosoftIdentityWebApi(builder.Configuration); @@ -21,23 +21,15 @@ builder.Services.AddMemoryCache(); -//TODO: Cleanup swagger config builder.Services.AddEndpointsApiExplorer(); -var azureSettingsSection = builder.Configuration.GetSection("AzureAd"); -var azureSettings = azureSettingsSection.Get(); -if (azureSettings == null) // TODO: Better checking of params - ErrorHandler.ThrowRequirementsException("Unable to load 'AzureAd' from settings"); +var adOptions = builder.Configuration.GetSection("AzureAd").Get(); +if (adOptions == null) throw new Exception("Required AzureAd options are missing"); -builder.Services.AddSwaggerGen(c => +builder.Services.AddSwaggerGen(genOptions => { - c.SwaggerDoc("v1", new OpenApiInfo { Title = "Vibes API", Version = "v1" }); - - var disableSwaggerAuth = - azureSettings != null && !builder.Environment.IsProduction() && azureSettings.DisableAuthAd; - if (disableSwaggerAuth) return; - - SwaggerBuild.AddSwaggerOAuthSetupAction(azureSettings, c); + genOptions.SwaggerDoc("v1", new OpenApiInfo { Title = "Vibes API", Version = "v1" }); + genOptions.ConfigureSwaggerAuthentication(adOptions); }); var app = builder.Build(); @@ -45,19 +37,16 @@ app.MapApiGroup("variant", "Varianter") .MapConsultantApi(); -// Configure the HTTP request pipeline. -if (!app.Environment.IsProduction()) +app.UseSwagger(); +app.UseSwaggerUI(c => { - app.UseDeveloperExceptionPage(); - app.UseSwagger(); - app.UseSwaggerUI(c => - { - c.SwaggerEndpoint("v1/swagger.json", "Vibes Backend API"); - c.OAuthClientId($"{azureSettings?.ClientId}"); - c.OAuthUsePkce(); - c.OAuthScopeSeparator(" "); - }); -} + c.SwaggerEndpoint("v1/swagger.json", "Vibes Backend API"); + c.OAuthClientId(adOptions.ClientId); + c.OAuthUsePkce(); + c.OAuthScopeSeparator(" "); +}); + +if (!app.Environment.IsProduction()) app.UseDeveloperExceptionPage(); // Only use redirection in production if (app.Environment.IsProduction()) app.UseHttpsRedirection();