Skip to content

Instantly share code, notes, and snippets.

@nhancv

nhancv/sign.html Secret

Last active March 14, 2020 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhancv/db9ff484f6bcdf4b5e72bf38cb701d7f to your computer and use it in GitHub Desktop.
Save nhancv/db9ff484f6bcdf4b5e72bf38cb701d7f to your computer and use it in GitHub Desktop.
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