1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | const crypto = require('crypto');
|
7 | const config = require('./config');
|
8 |
|
9 | function digest(message, key) {
|
10 | return crypto.createHmac("sha256", new Buffer(key, "hex")).update(message).digest('hex');
|
11 | }
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | function escapeToLower(url) {
|
19 | return encodeURIComponent(url).replace(/%../g, function(match) {
|
20 | return match.toLowerCase();
|
21 | });
|
22 | }
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | module.exports = function(options) {
|
44 | const tokenName = options.token_name ? options.token_name : "__cld_token__";
|
45 | if (options.expiration == null) {
|
46 | if (options.duration != null) {
|
47 | let start = options.start_time != null ? options.start_time : Math.round(Date.now() / 1000);
|
48 | options.expiration = start + options.duration;
|
49 | } else {
|
50 | throw new Error("Must provide either expiration or duration");
|
51 | }
|
52 | }
|
53 | let tokenParts = [];
|
54 | if (options.ip != null) {
|
55 | tokenParts.push(`ip=${options.ip}`);
|
56 | }
|
57 | if (options.start_time != null) {
|
58 | tokenParts.push(`st=${options.start_time}`);
|
59 | }
|
60 | tokenParts.push(`exp=${options.expiration}`);
|
61 | if (options.acl != null) {
|
62 | tokenParts.push(`acl=${escapeToLower(options.acl)}`);
|
63 | }
|
64 | let toSign = [...tokenParts];
|
65 | if (options.url) {
|
66 | let url = escapeToLower(options.url);
|
67 | toSign.push(`url=${url}`);
|
68 | }
|
69 | let auth = digest(toSign.join("~"), options.key);
|
70 | tokenParts.push(`hmac=${auth}`);
|
71 | return `${tokenName}=${tokenParts.join('~')}`;
|
72 | };
|