Skip to content

Commit

Permalink
refactor: Update CORS configuration for service endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
foxminchan committed Aug 26, 2024
1 parent 0374e05 commit 47f2558
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/BookWorm.Identity/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public static IEnumerable<Client> GetClients(ServiceOptions service)
AllowAccessTokensViaBrowser = true,
RedirectUris = { $"{service.Catalog}/swagger/oauth2-redirect.html" },
PostLogoutRedirectUris = { $"{service.Catalog}/swagger/" },
AllowedCorsOrigins = { service.Catalog },
AllowedScopes = { "catalog" }
},
new()
Expand All @@ -63,6 +64,7 @@ public static IEnumerable<Client> GetClients(ServiceOptions service)
AllowAccessTokensViaBrowser = true,
RedirectUris = { $"{service.Ordering}/swagger/oauth2-redirect.html" },
PostLogoutRedirectUris = { $"{service.Ordering}/swagger/" },
AllowedCorsOrigins = { service.Ordering },
AllowedScopes = { "ordering" }
},
new()
Expand All @@ -76,6 +78,7 @@ public static IEnumerable<Client> GetClients(ServiceOptions service)
AllowAccessTokensViaBrowser = true,
RedirectUris = { $"{service.Basket}/swagger/oauth2-redirect.html" },
PostLogoutRedirectUris = { $"{service.Basket}/swagger/" },
AllowedCorsOrigins = { service.Basket },
AllowedScopes = { "basket" }
},
new()
Expand All @@ -89,6 +92,7 @@ public static IEnumerable<Client> GetClients(ServiceOptions service)
AllowAccessTokensViaBrowser = true,
RedirectUris = { $"{service.Rating}/swagger/oauth2-redirect.html" },
PostLogoutRedirectUris = { $"{service.Rating}/swagger/" },
AllowedCorsOrigins = { service.Rating },
AllowedScopes = { "rating" }
},
new()
Expand Down
17 changes: 17 additions & 0 deletions src/BookWorm.ServiceDefaults/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public static IHostApplicationBuilder AddServiceDefaults(this IHostApplicationBu
{
AppContext.SetSwitch("Microsoft.SemanticKernel.Experimental.GenAI.EnableOTelDiagnosticsSensitive", true);

builder.ConfigureCors();

builder.ConfigureOpenTelemetry();

builder.AddDefaultHealthChecks();
Expand All @@ -39,6 +41,19 @@ public static IHostApplicationBuilder AddServiceDefaults(this IHostApplicationBu
return builder;
}

public static IHostApplicationBuilder ConfigureCors(this IHostApplicationBuilder builder)
{
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll", policyBuilder => policyBuilder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
});

return builder;
}

public static IHostApplicationBuilder ConfigureOpenTelemetry(this IHostApplicationBuilder builder)
{
builder.Services.AddHttpContextAccessor();
Expand Down Expand Up @@ -110,6 +125,8 @@ public static WebApplication MapDefaultEndpoints(this WebApplication app)
return app;
}

app.UseCors("AllowAll");

var healthChecks = app.MapGroup("");

healthChecks
Expand Down

0 comments on commit 47f2558

Please sign in to comment.