Skip to content

Instantly share code, notes, and snippets.

@ArthurKnoep
Created May 20, 2022 10:33
Show Gist options
  • Save ArthurKnoep/5730ed49850793a13269b09de96dc1ac to your computer and use it in GitHub Desktop.
Save ArthurKnoep/5730ed49850793a13269b09de96dc1ac to your computer and use it in GitHub Desktop.
Modifying the CloudFront Cache Key
const crypto = require('crypto');
function hashText(input) {
return crypto.createHash('md5').update(input).digest('hex');
}
function handler(event) {
var request = event.request;
var headers = request.headers;
delete headers['username-cache-key'];
if (headers['authorization']) {
var authorizations = headers['authorization'].split(' ');
if (authorizations.length === 2 && authorizations[0].toLowerCase() === 'basic') {
var credentials = atob(authorizations[1]).split(':');
headers['username-cache-key'] = { value: hashText(credentials[0]) };
}
}
return request;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment