Skip to content

Commit

Permalink
fix: cors error if BasePath is set
Browse files Browse the repository at this point in the history
Duendes `.UseIdentityServer()` uses the `BasePath` set by
`app.UsePathBase()` which works properly with CORS
unlike `BasePathMiddleware` and `.UseWhen()`.
  • Loading branch information
gentoo90 committed Dec 10, 2024
1 parent 9d3d5b0 commit db5730d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 45 deletions.
10 changes: 10 additions & 0 deletions e2e/tests/base-path.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ describe('Base path', () => {
expect(result).toHaveProperty('token_endpoint', process.env.OIDC_TOKEN_URL_WITH_BASE_PATH);
});

test('CORS', async () => {
const origin = 'https://google.com';
const response = await fetch(process.env.OIDC_DISCOVERY_ENDPOINT_WITH_BASE_PATH, {
headers: {
origin,
},
});
expect(response.headers.get('access-control-allow-origin')).toEqual(origin);
});

test('Token Endpoint', async () => {
if (!client) throw new Error('Client not found');

Expand Down
32 changes: 0 additions & 32 deletions src/Middlewares/BasePathMiddleware.cs

This file was deleted.

15 changes: 2 additions & 13 deletions src/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using Duende.IdentityServer.Hosting;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.FileProviders;
using OpenIdConnectServer;
using OpenIdConnectServer.Helpers;
using OpenIdConnectServer.JsonConverters;
using OpenIdConnectServer.Middlewares;
using OpenIdConnectServer.Services;
using OpenIdConnectServer.Validation;
using Serilog;
Expand Down Expand Up @@ -53,6 +51,7 @@

var app = builder.Build();

app.UsePathBase(Config.GetAspNetServicesOptions().BasePath);

var aspNetServicesOptions = Config.GetAspNetServicesOptions();
AspNetServicesHelper.ConfigureAspNetServices(builder.Services, aspNetServicesOptions);
Expand All @@ -65,16 +64,6 @@

app.UseIdentityServer();

var basePath = Config.GetAspNetServicesOptions().BasePath;
if (!string.IsNullOrEmpty(basePath))
{
app.UseWhen(ctx => ctx.Request.Path.StartsWithSegments(basePath), appBuilder =>
{
appBuilder.UseMiddleware<BasePathMiddleware>();
appBuilder.UseMiddleware<IdentityServerMiddleware>();
});
}

app.UseHttpsRedirection();

var manifestEmbeddedProvider = new ManifestEmbeddedFileProvider(typeof(Program).Assembly, "wwwroot");
Expand Down

0 comments on commit db5730d

Please sign in to comment.