|
/** |
|
* aws-signed-cookie-from-signed-url.js |
|
* Include this file in the landing page of the CloudFront assets where a signed url (from another domain) would direct users to |
|
* If the url is signed (i.e. has Policy, Signature and Key-Pair-Id params) it will create cookies for those attributes based on AWS rules |
|
* This will allow the internal navigation of CloudFront assets without the need of signed urls for each asset |
|
*/ |
|
|
|
/** |
|
* |
|
*/ |
|
function getUrlVars() { |
|
var vars = {}; |
|
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) { |
|
vars[key] = value; |
|
}); |
|
return vars; |
|
} |
|
|
|
/** |
|
* |
|
* @param parameter |
|
* @param defaultvalue |
|
* @returns {*} |
|
*/ |
|
function getUrlParam(parameter, defaultvalue){ |
|
var urlparameter = defaultvalue; |
|
if(window.location.href.indexOf(parameter) > -1){ |
|
urlparameter = getUrlVars()[parameter]; |
|
} |
|
return urlparameter; |
|
} |
|
|
|
/** |
|
* |
|
* @param cname |
|
* @param cvalue |
|
* @param exdays |
|
*/ |
|
function setCookie(cname, cvalue, exdays) { |
|
var d = new Date(); |
|
d.setTime(d.getTime() + (exdays*24*60*60*1000)); |
|
var expires = "expires="+ d.toUTCString(); |
|
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/"; |
|
} |
|
|
|
var Policy = getUrlParam('Policy', ''); |
|
var Signature = getUrlParam('Signature', ''); |
|
var KeyPairId = getUrlParam('Key-Pair-Id', ''); |
|
|
|
if (Policy != '' && Signature != '' && KeyPairId != '') { |
|
//delete cookie first |
|
setCookie('CloudFront-Policy', '', 0); |
|
setCookie('CloudFront-Signature', '', 0); |
|
setCookie('CloudFront-Key-Pair-Id', '', 0); |
|
|
|
//set the cookie |
|
setCookie('CloudFront-Policy', Policy, 1); |
|
setCookie('CloudFront-Signature', Signature, 1); |
|
setCookie('CloudFront-Key-Pair-Id', KeyPairId, 1); |
|
} |
Yes! forgot to mention that - i was using a Custom policy - did not try it with a Canned policy but i'm it can be made to work with one with some minor modification. Glad to hear it's working for you.