Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove signed cookie cache because of adding IP address condition. @EarthlingDavey #33

Merged
merged 2 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 6 additions & 35 deletions conf/node/controllers/cloudfront.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,8 @@ import {
cloudFrontPrivateKey as privateKey,
} from "../constants.js";

/**
* @typedef {Object} CookieSet
* @property {import('@aws-sdk/cloudfront-signer').CloudfrontSignedCookiesOutput} value
* @property {number} dateLessThan - epoch in milliseconds
*/

/**
* @typedef {Object} Cache
* @property {string|null} keyPairId - will only change on server restart
* @property {[key: string]: CookieSet} cookieSets
*/

/** @type {Cache} */
const cache = {
keyPairId: null,
cookieSets: {},
};
/** @type {string} */
let cachedKeyPairId = null;

/**
* Infer the CloudFront CDN URL from the app host
Expand Down Expand Up @@ -53,8 +38,8 @@ export const getCdnUrl = (appUrl) => {

export const getKeyPairId = () => {
// Return the cached value if it exists
if (cache.keyPairId) {
return cache.keyPairId;
if (cachedKeyPairId) {
return cachedKeyPairId;
}

// Get sha256 hash of the public key, and get the first 8 characters.
Expand All @@ -72,7 +57,7 @@ export const getKeyPairId = () => {
throw new Error("Key pair ID not found");
}

cache.keyPairId = keyPairId;
cachedKeyPairId = keyPairId;

return keyPairId;
};
Expand Down Expand Up @@ -102,19 +87,11 @@ export const getDateLessThan = () => {
* @param {Object} props
* @param {string} props.resource
* @param {number} props.dateLessThan
* @param {string} props.ipAddress
* @returns {import('@aws-sdk/cloudfront-signer').CloudfrontSignedCookiesOutput} cookies - The signed CloudFront cookies
*/

export const getCookies = ({ resource, dateLessThan, ipAddress }) => {
// Check if the cache has a value for the resource
const cachedValue =
cache.cookieSets?.[resource]?.dateLessThan === dateLessThan;

// Return the cached value if it exists
if (cachedValue) {
return cachedValue;
}

const policy = {
Statement: [
{
Expand All @@ -139,11 +116,5 @@ export const getCookies = ({ resource, dateLessThan, ipAddress }) => {
policy: policyString,
});

// Set the cache
cache.cookieSets[resource] = {
dateLessThan,
value: signedCookies,
};

return signedCookies;
};
6 changes: 3 additions & 3 deletions conf/node/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ app.get("/access-archive", async function (req, res, next) {
});
});

// 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>`;
// http://app.archive.intranet.docker/access-archive

res.status(200).send(html);
// Redirect to the CDN URL.
res.redirect(cdnUrl.origin);
} catch (err) {
next(err);
}
Expand Down
Loading