Skip to content

Commit 4345af3

Browse files
authored
feat: handle initial redirect through redirect server request handler (#138)
1 parent 7dd0d68 commit 4345af3

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

src/api.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,10 @@ export type RedirectServerRequestInfo = {
261261
/** The suggested HTTP status code. For unknown-url, this is 404. */
262262
status: number;
263263
} & (
264+
| {
265+
result: 'redirecting';
266+
location: string;
267+
}
264268
| {
265269
result: 'rejected';
266270
/** Error information reported by the IdP as defined in RFC6749 section 4.1.2.1 */

src/rfc-8252-http-server.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,16 @@ describe('RFC8252HTTPServer', function () {
411411
url = new URL(server.listeningRedirectUrl || '');
412412
});
413413

414+
it('handles redirecting case', async function () {
415+
const { localUrl } = await server.addRedirect('http://example.com');
416+
const res = await fetch(localUrl, { follow: 0 });
417+
expect(res.status).to.eq(307);
418+
expect(await res.json()).to.have.property(
419+
'location',
420+
'http://example.com'
421+
);
422+
});
423+
414424
it('handles the success case', async function () {
415425
const params = new URLSearchParams([
416426
['foo', 'bar'],

src/rfc-8252-http-server.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,13 @@ export class RFC8252HTTPServer {
123123
// This can be helpful for figuring out whether a browser was
124124
// opened successfully.
125125
entry.onAccessed();
126-
res.status(307);
127-
res.set('Location', entry.targetUrl);
128-
res.send();
126+
this.redirectServerHandler({
127+
req,
128+
res,
129+
status: 307,
130+
result: 'redirecting',
131+
location: entry.targetUrl,
132+
});
129133
};
130134

131135
private _handleOIDCCallback: RequestHandler = (req, res) => {
@@ -515,6 +519,10 @@ function defaultRedirectServerHandler(info: RedirectServerRequestInfo): void {
515519
res.statusCode = status;
516520
res.setHeader('Content-Type', 'text/plain');
517521
switch (result) {
522+
case 'redirecting':
523+
res.setHeader('Location', info.location);
524+
res.end('Redirecting...');
525+
return;
518526
case 'accepted':
519527
res.end('Authentication successful! You can close this window now.');
520528
return;

0 commit comments

Comments
 (0)