fix: Rewrite request with a root path to OIDC Provider

* fix(oidc): rewrite requests with rootpath

* fix: respect query parameters
This commit is contained in:
Jasper Vaneessen 2022-05-18 09:42:01 +02:00 committed by GitHub
parent e7ba2d49f8
commit 0a84230307
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 4 deletions

View File

@ -18,6 +18,13 @@ export class OidcHttpHandler extends HttpHandler {
public async handle({ request, response }: HttpHandlerInput): Promise<void> {
const provider = await this.providerFactory.getProvider();
// Rewrite requests to allow hosting on root paths
const path = new URL(provider.issuer).pathname;
if (path.length > 1 && request.url!.startsWith(`${path}.well-known/openid-configuration`)) {
request.url = request.url!.replace(path, '/');
}
this.logger.debug(`Sending request to oidc-provider: ${request.url}`);
// Even though the typings do not indicate this, this is a Promise that needs to be awaited.
// Otherwise, the `BaseHttpServerFactory` will write a 404 before the OIDC library could handle the response.

View File

@ -8,7 +8,7 @@
</head>
<body>
<header>
<a href="/"><img src="/.well-known/css/images/solid.svg" alt="[Solid logo]" /></a>
<a href="./"><img src="/.well-known/css/images/solid.svg" alt="[Solid logo]" /></a>
<h1>Community Solid Server</h1>
</header>
<main>
@ -22,7 +22,7 @@
<h2 id="users">Getting started as a <em>user</em></h2>
<p>
<a href="/idp/register/">Sign up for an account</a>
<a href="./idp/register/">Sign up for an account</a>
to get started with your own Pod and WebID.
</p>
<p>

View File

@ -5,7 +5,9 @@ import type { HttpRequest } from '../../../src/server/HttpRequest';
import type { HttpResponse } from '../../../src/server/HttpResponse';
describe('An OidcHttpHandler', (): void => {
const request: HttpRequest = {} as any;
const request: HttpRequest = {
url: '/.well-known/openid-configuration',
} as any;
const response: HttpResponse = {} as any;
let provider: jest.Mocked<Provider>;
let providerFactory: jest.Mocked<ProviderFactory>;
@ -14,11 +16,12 @@ describe('An OidcHttpHandler', (): void => {
beforeEach(async(): Promise<void> => {
provider = {
callback: jest.fn().mockReturnValue(jest.fn()),
issuer: 'http://localhost:3000/',
} as any;
providerFactory = {
getProvider: jest.fn().mockResolvedValue(provider),
};
} as any;
handler = new OidcHttpHandler(providerFactory);
});
@ -29,4 +32,24 @@ describe('An OidcHttpHandler', (): void => {
expect(provider.callback.mock.results[0].value).toHaveBeenCalledTimes(1);
expect(provider.callback.mock.results[0].value).toHaveBeenLastCalledWith(request, response);
});
it('rewrites the request when using base URL with root path.', async(): Promise<void> => {
Object.assign(provider, { issuer: 'http://localhost:3000/path/' });
request.url = '/path/.well-known/openid-configuration';
await expect(handler.handle({ request, response })).resolves.toBeUndefined();
expect(request.url).toBe('/.well-known/openid-configuration');
expect(provider.callback).toHaveBeenCalledTimes(1);
expect(provider.callback.mock.results[0].value).toHaveBeenCalledTimes(1);
expect(provider.callback.mock.results[0].value).toHaveBeenLastCalledWith(request, response);
});
it('respects query parameters when rewriting requests.', async(): Promise<void> => {
Object.assign(provider, { issuer: 'http://localhost:3000/path/' });
request.url = '/path/.well-known/openid-configuration?param1=value1';
await expect(handler.handle({ request, response })).resolves.toBeUndefined();
expect(request.url).toBe('/.well-known/openid-configuration?param1=value1');
expect(provider.callback).toHaveBeenCalledTimes(1);
expect(provider.callback.mock.results[0].value).toHaveBeenCalledTimes(1);
expect(provider.callback.mock.results[0].value).toHaveBeenLastCalledWith(request, response);
});
});