Skip to content

Instantly share code, notes, and snippets.

@nhancv nhancv/sign.html Secret

Last active Mar 14, 2020
Embed
What would you like to do?
Sign URL CloudFront S3
<!DOCTYPE html>
<html lang="en-US">
<head>
<script type="text/javascript">
/*************************************************
* // query string: ?foo=lorem&bar=&baz
var foo = getParameterByName('foo'); // "lorem"
var bar = getParameterByName('bar'); // "" (present with empty value)
var baz = getParameterByName('baz'); // "" (present with no value)
var qux = getParameterByName('qux'); // null (absent)
*/
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
};
// Delete all cookies
function deleteAllCookies() {
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
var eqPos = cookie.indexOf("=");
var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
}
// Check signature
var Policy = getParameterByName("BPolicy");
var KeyPairId = getParameterByName("BKey-Pair-Id");
var Signature = getParameterByName("BSignature");
if(Policy && KeyPairId && Signature) {
document.cookie = "CloudFront-Policy=" + Policy + "; path=/";
document.cookie = "CloudFront-Key-Pair-Id=" + KeyPairId + "; path=/";
document.cookie = "CloudFront-Signature=" + Signature + "; path=/";
window.location.href = "/index.html"
} else {
deleteAllCookies();
window.location.href = "/";
}
</script>
</head>
</html>
<body></body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.