-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudfront-signer.js
73 lines (61 loc) · 2.01 KB
/
cloudfront-signer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var util = require('util');
export.keyPath = 'PATH_TO_KEY_FILE';
export.keyPairId = 'YOUR_KEY_PAIR_ID';
function cannedPolicyStream(url, offSet) {
//offSet time to expires in seconds.
//Create this policy for cloudfront before it can be signed.
var expire = Date().valueOf() + offSet;
var cannedPolicy = {
Statement: [{
Resource: url,
Condition: {
DateLessThan: {
"AWS:EpochTime": expireOffSet
}
}
}]
};
return JSON.stringify(canndedPeolicy);
}
function encodeBase64(cannedPolicy) {
var encodedPolicy = new Buffer(cannedPolicy).toString('base64')
//Remove any URL unsafe characters +, =, / from encoded string.
//Replace with -, _, ~
var unsafeReplace = ['+','=','/'];
var safeToReplace = ['-','_','~'];
for ( var i = 0; i < unsafeReplace.length; i++ ) {
endcodedPolicy = encodedPolicy.replace(unsafeReplace, safeToReplace);
}
return encodedPolicy;
}
function signUrl(enocdedPolicy, privateKey) {
var require('fs');
var privateKey = fs.readFileSync('/path/to/privateKey.txt','ascii');
var crypto = require('crypto');
var signObj = crypto.createSign('RSA-SHA1');
signObj.update(encodedUrl);
var encodedSignature = signObj.sign(privateKey, 'base64');
return encodedSignature;
}
function createUrl(encodedSignature, keyPairId, expire) {
//Generate custom policy by passing encodedPolicy.
//Generate canned policy expire.
var qs = require('querystring');
var generatedUrl = {
SIGNATURE_PARAM: encodedSignature,
KEY_PAIR_ID_PARAM: keyPairId
};
if (expire) {
//If you are using a canned policy.
generatedUrl.EXPIRES_PARAM = expire;
}
//encode the query parametes to work around flash player.
return encodeURICompenent(qs.stringify(generatedUrl));
}
export.signUrl = function(url, expire, callback) {
var policy = encodeBase64(cannedPolicyStream(url, expire));
var signnature = signUrl(policy, keypath);
var signedUrl = creatUrl(url, signature, expire)
callback(signedUrl)
};
console.log(createUrl('/path/to/privateKey.txt', expire, signature, keyPairId));