Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add suport for whatwg url policy #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions lib/cloudfrontUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,45 @@ var _ = require('lodash');
var CannedPolicy = require('./CannedPolicy');

/**
* Build an AWS signed URL.
* Build an AWS signed URL using WHATWG URL policy
*
* @param {String} CloudFront resource URL
* @param {Object} Signature parameters
* @param {Object} params Signature parameters
* @return {String} Signed CloudFront URL
* @throws {TypeError}
*/
function getSignedWhatwgUrl(params) {
if (!params.url || !params.pathname) {
throw new TypeError('Invalid options; url and pathname are required.');
}

var cfUrl = params.url + params.pathname;

var privateKey = _getPrivateKey(params);
var policy = _createPolicy(
cfUrl, _getExpireTime(params), _getIpRange(params));
var signature = _createPolicySignature(policy, privateKey);
var policyStr = new Buffer(policy.toJSON()).toString('base64');

var whatwgUrl = new url.URL(params.pathname, params.url);
whatwgUrl.searchParams.append('Expires', policy.expireTime);
whatwgUrl.searchParams.append('Policy', normalizeBase64(policyStr));
whatwgUrl.searchParams.append('Signature', normalizeBase64(signature));
whatwgUrl.searchParams.append('Key-Pair-Id', params.keypairId);
return whatwgUrl.toString();
}

/**
* Build an AWS signed URL using the legacy nodejs URL object.
*
* @param {String} [cfUrl] CloudFront resource URL - optional, if omitted, params.url and params.pathname are required
* @param {Object} params Signature parameters
* @return {String} Signed CloudFront URL
*/
function getSignedUrl(cfUrl, params) {
if (arguments.length === 1) {
return getSignedWhatwgUrl(cfUrl);
}

var privateKey = _getPrivateKey(params);
var policy = _createPolicy(
cfUrl, _getExpireTime(params), _getIpRange(params));
Expand Down Expand Up @@ -207,6 +239,7 @@ function _getPrivateKey(params) {

exports.getSignedCookies = getSignedCookies;
exports.getSignedUrl = getSignedUrl;
exports.getSignedWhatwgUrl = getSignedWhatwgUrl;
exports.getSignedRTMPUrl = getSignedRTMPUrl;
exports.normalizeSignature = normalizeSignature;
exports.normalizeBase64 = normalizeBase64;
Expand Down