Skip to content

Commit

Permalink
Merge pull request #78 from varianter/cleanup-swagger
Browse files Browse the repository at this point in the history
cleanup swagger config
  • Loading branch information
jonasbjoralt authored Sep 28, 2023
2 parents dae1f55 + d42b9fc commit f05da59
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 45 deletions.
12 changes: 0 additions & 12 deletions backend/Api/BuildHelpers/ErrorHandler.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>
{
{ $"{settings.ApiScope}", "Access API backend on user behalf" }
{ $"{adOptions.ApiScope}", "Access API backend on user behalf" }
};

c.AddSecurityRequirement(new OpenApiSecurityRequirement
Expand All @@ -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 }
}
});

Expand All @@ -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
}
}
Expand Down
1 change: 0 additions & 1 deletion backend/Api/Options/AzureAdOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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!;
Expand Down
41 changes: 15 additions & 26 deletions backend/Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -21,43 +21,32 @@

builder.Services.AddMemoryCache();

//TODO: Cleanup swagger config
builder.Services.AddEndpointsApiExplorer();
var azureSettingsSection = builder.Configuration.GetSection("AzureAd");
var azureSettings = azureSettingsSection.Get<AzureAdOptions>();

if (azureSettings == null) // TODO: Better checking of params
ErrorHandler.ThrowRequirementsException("Unable to load 'AzureAd' from settings");
var adOptions = builder.Configuration.GetSection("AzureAd").Get<AzureAdOptions>();
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();

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();
Expand Down

0 comments on commit f05da59

Please sign in to comment.