Skip to content

Commit

Permalink
feat: allow redirect with WebdaError.Redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
loopingz committed Jun 30, 2023
1 parent 4c5e9d9 commit 499432e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
21 changes: 20 additions & 1 deletion packages/aws/src/services/lambdaserver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,24 @@ class LambdaHandlerTest extends WebdaAwsTest {
assert.strictEqual(res.statusCode, 403);
}

@test
async checkRequestRedirect() {
await this.handler.init();
// @ts-ignore
this.handler._requestFilters = [];
this.ensureGoodCSRF();
this.evt.queryStringParameters = { test: "Plop" };
this.handler.registerRequestFilter({
checkRequest: async () => {
throw new WebdaError.Redirect("Need Auth", "https://google.com")
}
});
let res = await this.handler.handleRequest(this.evt, this.context);
assert.strictEqual(res.statusCode, 302);
console.log(res);
assert.strictEqual(res.headers.Location, "https://google.com");
}

@test
async handleRequestCustomLaunch() {
await this.handler.handleRequest(
Expand Down Expand Up @@ -313,7 +331,8 @@ class LambdaHandlerTest extends WebdaAwsTest {
let res = await this.handler.handleRequest(this.evt, this.context);
assert.strictEqual(res.statusCode, 410);
this.badCheck = true;
await assert.rejects(() => this.handler.handleRequest(this.evt, this.context), /Unknown/);
res = await this.handler.handleRequest(this.evt, this.context);
assert.strictEqual(res.statusCode, 500);
this.newExcept = true;
res = await this.handler.handleRequest(this.evt, this.context);
assert.strictEqual(res.statusCode, 429);
Expand Down
18 changes: 6 additions & 12 deletions packages/aws/src/services/lambdaserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,17 +221,7 @@ export default class LambdaServer extends Webda {
this.log("WARN", "Request refused");
throw 403;
}
} catch (err) {
if (typeof err === "number") {
ctx.statusCode = err;
return this.handleLambdaReturn(ctx);
} else if (err instanceof WebdaError.HttpError) {
ctx.statusCode = err.getResponseCode();
this.log("TRACE", `${err.getResponseCode()}: ${err.message}`);
return this.handleLambdaReturn(ctx);
}
throw err;
}

if (protocol === "https") {
// Add the HSTS header
ctx.setHeader("Strict-Transport-Security", "max-age=31536000; includeSubDomains; preload");
Expand All @@ -258,7 +248,7 @@ export default class LambdaServer extends Webda {
return this.handleLambdaReturn(ctx);
}
await ctx.init();
try {

await ctx.execute();
return this.handleLambdaReturn(ctx);
} catch (err) {
Expand All @@ -267,6 +257,10 @@ export default class LambdaServer extends Webda {
} else if (err instanceof WebdaError.HttpError) {
this.log("DEBUG", "Sending error", err.message);
ctx.statusCode = err.getResponseCode();
// Handle redirect
if (err instanceof WebdaError.Redirect) {
ctx.setHeader("Location", err.location);
}
} else {
this.log("ERROR", err);
ctx.statusCode = 500;
Expand Down
16 changes: 15 additions & 1 deletion packages/shell/src/handlers/http.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { suite, test } from "@testdeck/mocha";
import { HttpContext, ResourceService } from "@webda/core";
import { HttpContext, ResourceService, WebdaError } from "@webda/core";
import * as assert from "assert";
import * as http from "http";
import { createChecker } from "is-in-subnet";
Expand Down Expand Up @@ -99,6 +99,16 @@ class WebdaServerTest {
headers: { origin: "bouzouf", "x-forwarded-port": "443" }
});
assert.strictEqual(res.status, 404);
stub.callsFake(() => {
throw new WebdaError.Redirect("Need Auth", "https://google.com")
})
res = await fetch(`http://localhost:${this.port}/test`, {
headers: { origin: "bouzouf", "x-forwarded-port": "443" },
redirect: "manual"
});
assert.strictEqual(res.status, 302);
assert.strictEqual(res.headers.get("Location"), "https://google.com/");

stub.callsFake(() => {
throw new Error();
});
Expand All @@ -124,6 +134,10 @@ class WebdaServerTest {
headers: { origin: "bouzouf", "x-forwarded-port": "443" }
});
assert.strictEqual(res.status, 500);
res = await fetch(`http://localhost:${this.port}/test`, {
headers: { origin: "bouzouf", "x-forwarded-port": "443" }
});


stub.restore();

Expand Down
4 changes: 4 additions & 0 deletions packages/shell/src/handlers/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ export class WebdaServer extends Webda {
err = typeof err === "number" ? new WebdaError.HttpError("Wrapped", err) : err;
if (err instanceof WebdaError.HttpError) {
ctx.statusCode = err.getResponseCode();
// Handle redirect
if (err instanceof WebdaError.Redirect) {
ctx.setHeader("Location", err.location);
}
this.log("TRACE", `${err.getResponseCode()}: ${err.message}`);
} else {
ctx.statusCode = 500;
Expand Down

0 comments on commit 499432e

Please sign in to comment.