Skip to content

Commit

Permalink
Update CloudFront cookie properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
EarthlingDavey committed Dec 6, 2024
1 parent d776ebd commit de29d18
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
4 changes: 2 additions & 2 deletions conf/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ server {
proxy_pass http://localhost:2000/bucket-test;
}

location /set-cf-cookie {
location /access-archive {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:2000/set-cf-cookie;
proxy_pass http://localhost:2000/access-archive;
}

location / {
Expand Down
38 changes: 21 additions & 17 deletions conf/node/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { parseBody } from "./middleware.js";
import { checkAccess as checkS3Access } from "./controllers/s3.js";
import {
getCdnUrl,
getKeyPairId,
getCookies,
getDateLessThan,
} from "./controllers/cloudfront.js";
Expand Down Expand Up @@ -68,41 +67,46 @@ app.post("/bucket-test", async function (_req, res, next) {
}
});

app.post("/set-cf-cookie", async function (request, response) {

app.post("/spider", function (req, res) {
// Start the main function - without awiting for the result.
main(req.mirror);
// Handle the response
res.status(200).sendFile(path.join("/usr/share/nginx/html/working.html"));
});

app.get("/access-archive", async function (req, res, next) {
try {
// Get the current domain from the request
const appHost = request.get("X-Forwarded-Host") || request.get("host");

const appHost = req.headers["x-forwarded-host"] || req.headers["host"];

// Get the CloudFront CDN URL
const cdnUrl = getCdnUrl(appHost);

// Get the CloudFront cookies
const cookies = getCookies({
resource: `${cdnUrl.origin}/*`,
dateLessThan: getDateLessThan(),
});

// Set the cookies on the response
Object.entries(cookies).forEach(([name, value]) => {
response.cookie(name, value, {
res.cookie(name, value, {
path: "/",
domain: cdnUrl.host,
secure: true,
sameSite: "None",
sameSite: "Lax",
httpOnly: true,
});
});

// Send a metadata html tag to redirect to the cdnUrl
const html = `<html><head><meta http-equiv="refresh" content="0; url=${cdnUrl.origin}" /></head></html>`;

res.status(200).send(html);
} catch (err) {
next(err);
}

response.status(200).send({ appHost, cdnHost });
});

app.post("/spider", function (request, response) {
// Start the main function - without awiting for the result.
main(request.mirror);
// Handle the response
response
.status(200)
.sendFile(path.join("/usr/share/nginx/html/working.html"));
});

app.listen(port);

0 comments on commit de29d18

Please sign in to comment.