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