Created
May 20, 2022 10:33
-
-
Save ArthurKnoep/5730ed49850793a13269b09de96dc1ac to your computer and use it in GitHub Desktop.
Modifying the CloudFront Cache Key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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