agora-token 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,242 @@
1
+ {
2
+ "name": "agora_sign_server",
3
+ "version": "0.0.1",
4
+ "lockfileVersion": 1,
5
+ "requires": true,
6
+ "dependencies": {
7
+ "agora-access-token": {
8
+ "version": "2.0.1",
9
+ "resolved": "https://registry.npmjs.org/agora-access-token/-/agora-access-token-2.0.1.tgz",
10
+ "integrity": "sha512-ZWyvUG5BoX0lGBjQUZbnixdiudlJG+5Oxht/UCgQmnBzNEq0TrHoIpV7Nlwy/vKAgl8mHmVGwtBC4Jm7l6t4nA==",
11
+ "requires": {
12
+ "crc-32": "1.2.0",
13
+ "cuint": "0.2.2"
14
+ }
15
+ },
16
+ "batch": {
17
+ "version": "0.5.0",
18
+ "resolved": "https://registry.npmjs.org/batch/-/batch-0.5.0.tgz",
19
+ "integrity": "sha1-/S4Fp6XWlrTbkxQBPihdj/NVfsM="
20
+ },
21
+ "buffer-crc32": {
22
+ "version": "0.2.1",
23
+ "resolved": "https://registry.npmjs.org/buffer-crc32/-/buffer-crc32-0.2.1.tgz",
24
+ "integrity": "sha1-vj5TgvwCttYySVasGvmKqYsIU0w="
25
+ },
26
+ "bytes": {
27
+ "version": "0.2.1",
28
+ "resolved": "https://registry.npmjs.org/bytes/-/bytes-0.2.1.tgz",
29
+ "integrity": "sha1-VVsIq8sGP4l1kFMCUj5M1P/f3zE="
30
+ },
31
+ "commander": {
32
+ "version": "1.3.2",
33
+ "resolved": "https://registry.npmjs.org/commander/-/commander-1.3.2.tgz",
34
+ "integrity": "sha1-io8w7GcKb91kr1LxkUuQfXnq1bU=",
35
+ "requires": {
36
+ "keypress": "0.1.0"
37
+ }
38
+ },
39
+ "connect": {
40
+ "version": "2.12.0",
41
+ "resolved": "https://registry.npmjs.org/connect/-/connect-2.12.0.tgz",
42
+ "integrity": "sha1-Mdj6DcrN8ZCNgivSkjvootKn7Zo=",
43
+ "requires": {
44
+ "batch": "0.5.0",
45
+ "buffer-crc32": "0.2.1",
46
+ "bytes": "0.2.1",
47
+ "cookie": "0.1.0",
48
+ "cookie-signature": "1.0.1",
49
+ "debug": "0.8.1",
50
+ "fresh": "0.2.0",
51
+ "methods": "0.1.0",
52
+ "multiparty": "2.2.0",
53
+ "negotiator": "0.3.0",
54
+ "pause": "0.0.1",
55
+ "qs": "0.6.6",
56
+ "raw-body": "1.1.2",
57
+ "send": "0.1.4",
58
+ "uid2": "0.0.3"
59
+ }
60
+ },
61
+ "cookie": {
62
+ "version": "0.1.0",
63
+ "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.1.0.tgz",
64
+ "integrity": "sha1-kOtGndzpBchm3mh+/EMTHYgB+dA="
65
+ },
66
+ "cookie-signature": {
67
+ "version": "1.0.1",
68
+ "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.1.tgz",
69
+ "integrity": "sha1-ROByFIrwHm6OJK+/EmkNaK5pjss="
70
+ },
71
+ "core-util-is": {
72
+ "version": "1.0.2",
73
+ "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz",
74
+ "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac="
75
+ },
76
+ "crc-32": {
77
+ "version": "1.2.0",
78
+ "resolved": "https://registry.npmjs.org/crc-32/-/crc-32-1.2.0.tgz",
79
+ "integrity": "sha512-1uBwHxF+Y/4yF5G48fwnKq6QsIXheor3ZLPT80yGBV1oEUwpPojlEhQbWKVw1VwcTQyMGHK1/XMmTjmlsmTTGA==",
80
+ "requires": {
81
+ "exit-on-epipe": "1.0.1",
82
+ "printj": "1.1.2"
83
+ }
84
+ },
85
+ "cuint": {
86
+ "version": "0.2.2",
87
+ "resolved": "https://registry.npmjs.org/cuint/-/cuint-0.2.2.tgz",
88
+ "integrity": "sha1-QICG1AlVDCYxFVYZ6fp7ytw7mRs="
89
+ },
90
+ "debug": {
91
+ "version": "0.8.1",
92
+ "resolved": "https://registry.npmjs.org/debug/-/debug-0.8.1.tgz",
93
+ "integrity": "sha1-IP9NJvXkIstoobrLu2EDmtjBwTA="
94
+ },
95
+ "exit-on-epipe": {
96
+ "version": "1.0.1",
97
+ "resolved": "https://registry.npmjs.org/exit-on-epipe/-/exit-on-epipe-1.0.1.tgz",
98
+ "integrity": "sha512-h2z5mrROTxce56S+pnvAV890uu7ls7f1kEvVGJbw1OlFH3/mlJ5bkXu0KRyW94v37zzHPiUd55iLn3DA7TjWpw=="
99
+ },
100
+ "express": {
101
+ "version": "3.4.8",
102
+ "resolved": "https://registry.npmjs.org/express/-/express-3.4.8.tgz",
103
+ "integrity": "sha1-qnqJht4HBTM39Lxe2aZFPZzI4uE=",
104
+ "requires": {
105
+ "buffer-crc32": "0.2.1",
106
+ "commander": "1.3.2",
107
+ "connect": "2.12.0",
108
+ "cookie": "0.1.0",
109
+ "cookie-signature": "1.0.1",
110
+ "debug": "0.8.1",
111
+ "fresh": "0.2.0",
112
+ "merge-descriptors": "0.0.1",
113
+ "methods": "0.1.0",
114
+ "mkdirp": "0.3.5",
115
+ "range-parser": "0.0.4",
116
+ "send": "0.1.4"
117
+ }
118
+ },
119
+ "fresh": {
120
+ "version": "0.2.0",
121
+ "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.2.0.tgz",
122
+ "integrity": "sha1-v9lALPPfEsSkwxDHn5mj3eE9NKc="
123
+ },
124
+ "inherits": {
125
+ "version": "2.0.4",
126
+ "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",
127
+ "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ=="
128
+ },
129
+ "isarray": {
130
+ "version": "0.0.1",
131
+ "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz",
132
+ "integrity": "sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8="
133
+ },
134
+ "keypress": {
135
+ "version": "0.1.0",
136
+ "resolved": "https://registry.npmjs.org/keypress/-/keypress-0.1.0.tgz",
137
+ "integrity": "sha1-SjGI1CkbZrT2XtuZ+AaqmuKTWSo="
138
+ },
139
+ "merge-descriptors": {
140
+ "version": "0.0.1",
141
+ "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-0.0.1.tgz",
142
+ "integrity": "sha1-L/CYDJJM+B0LXR+2ARd8uLtWwNA="
143
+ },
144
+ "methods": {
145
+ "version": "0.1.0",
146
+ "resolved": "https://registry.npmjs.org/methods/-/methods-0.1.0.tgz",
147
+ "integrity": "sha1-M11Cnu/SG3us8unJIqjSvRSjDk8="
148
+ },
149
+ "mime": {
150
+ "version": "1.2.11",
151
+ "resolved": "https://registry.npmjs.org/mime/-/mime-1.2.11.tgz",
152
+ "integrity": "sha1-WCA+7Ybjpe8XrtK32evUfwpg3RA="
153
+ },
154
+ "mkdirp": {
155
+ "version": "0.3.5",
156
+ "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.3.5.tgz",
157
+ "integrity": "sha1-3j5fiWHIjHh+4TaN+EmsRBPsqNc="
158
+ },
159
+ "multiparty": {
160
+ "version": "2.2.0",
161
+ "resolved": "https://registry.npmjs.org/multiparty/-/multiparty-2.2.0.tgz",
162
+ "integrity": "sha1-pWfCrwAK0i3I8qZT2Rl4rh9TFvQ=",
163
+ "requires": {
164
+ "readable-stream": "1.1.14",
165
+ "stream-counter": "0.2.0"
166
+ }
167
+ },
168
+ "negotiator": {
169
+ "version": "0.3.0",
170
+ "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.3.0.tgz",
171
+ "integrity": "sha1-cG1pLv7d9XTVfqn7GriaT6fuj2A="
172
+ },
173
+ "pause": {
174
+ "version": "0.0.1",
175
+ "resolved": "https://registry.npmjs.org/pause/-/pause-0.0.1.tgz",
176
+ "integrity": "sha1-HUCLP9t2kjuVQ9lvtMnf1TXZy10="
177
+ },
178
+ "printj": {
179
+ "version": "1.1.2",
180
+ "resolved": "https://registry.npmjs.org/printj/-/printj-1.1.2.tgz",
181
+ "integrity": "sha512-zA2SmoLaxZyArQTOPj5LXecR+RagfPSU5Kw1qP+jkWeNlrq+eJZyY2oS68SU1Z/7/myXM4lo9716laOFAVStCQ=="
182
+ },
183
+ "qs": {
184
+ "version": "0.6.6",
185
+ "resolved": "https://registry.npmjs.org/qs/-/qs-0.6.6.tgz",
186
+ "integrity": "sha1-bgFQmP9RlouKPIGQAdXyyJvEsQc="
187
+ },
188
+ "range-parser": {
189
+ "version": "0.0.4",
190
+ "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-0.0.4.tgz",
191
+ "integrity": "sha1-wEJ//vUcEKy6B4KkbJYC50T/Ygs="
192
+ },
193
+ "raw-body": {
194
+ "version": "1.1.2",
195
+ "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-1.1.2.tgz",
196
+ "integrity": "sha1-x0swBN6l3v0WlhcRBqx0DsMdYr4=",
197
+ "requires": {
198
+ "bytes": "0.2.1"
199
+ }
200
+ },
201
+ "readable-stream": {
202
+ "version": "1.1.14",
203
+ "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz",
204
+ "integrity": "sha1-fPTFTvZI44EwhMY23SB54WbAgdk=",
205
+ "requires": {
206
+ "core-util-is": "1.0.2",
207
+ "inherits": "2.0.4",
208
+ "isarray": "0.0.1",
209
+ "string_decoder": "0.10.31"
210
+ }
211
+ },
212
+ "send": {
213
+ "version": "0.1.4",
214
+ "resolved": "https://registry.npmjs.org/send/-/send-0.1.4.tgz",
215
+ "integrity": "sha1-vnDY0b4B3mGCGvE3gLUDRaT3Gr0=",
216
+ "requires": {
217
+ "debug": "0.8.1",
218
+ "fresh": "0.2.0",
219
+ "mime": "1.2.11",
220
+ "range-parser": "0.0.4"
221
+ }
222
+ },
223
+ "stream-counter": {
224
+ "version": "0.2.0",
225
+ "resolved": "https://registry.npmjs.org/stream-counter/-/stream-counter-0.2.0.tgz",
226
+ "integrity": "sha1-3tJmVWMZyLDiIoErnPOyb6fZR94=",
227
+ "requires": {
228
+ "readable-stream": "1.1.14"
229
+ }
230
+ },
231
+ "string_decoder": {
232
+ "version": "0.10.31",
233
+ "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz",
234
+ "integrity": "sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ="
235
+ },
236
+ "uid2": {
237
+ "version": "0.0.3",
238
+ "resolved": "https://registry.npmjs.org/uid2/-/uid2-0.0.3.tgz",
239
+ "integrity": "sha1-SDEm4Rd03y9xuLY53NeZw3YWK4I="
240
+ }
241
+ }
242
+ }
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "agora_sign_server",
3
+ "version": "0.0.1",
4
+ "private": true,
5
+ "scripts": {
6
+ "start": "node app.js"
7
+ },
8
+ "dependencies": {
9
+ "agora-access-token": "^2.0.1",
10
+ "express": "3.4.8"
11
+ },
12
+ "main": "DemoServer.js",
13
+ "author": "agora"
14
+ }
@@ -0,0 +1,237 @@
1
+ var crypto = require("crypto");
2
+ var crc32 = require("crc-32");
3
+ var UINT32 = require("cuint").UINT32;
4
+ var version = "006";
5
+ var randomInt = Math.floor(Math.random() * 0xffffffff);
6
+ const VERSION_LENGTH = 3;
7
+ const APP_ID_LENGTH = 32;
8
+
9
+ var AccessToken = function (appID, appCertificate, channelName, uid) {
10
+ let token = this;
11
+ this.appID = appID;
12
+ this.appCertificate = appCertificate;
13
+ this.channelName = channelName;
14
+ this.messages = {};
15
+ this.salt = randomInt;
16
+ this.ts = Math.floor(new Date() / 1000) + 24 * 3600;
17
+ if (uid === 0) {
18
+ this.uid = "";
19
+ } else {
20
+ this.uid = `${uid}`;
21
+ }
22
+
23
+ this.build = function () {
24
+ var m = Message({
25
+ salt: token.salt,
26
+ ts: token.ts,
27
+ messages: token.messages
28
+ }).pack();
29
+
30
+ var toSign = Buffer.concat([Buffer.from(token.appID, "utf8"), Buffer.from(token.channelName, "utf8"), Buffer.from(token.uid, "utf8"), m]);
31
+
32
+ var signature = encodeHMac(token.appCertificate, toSign);
33
+ var crc_channel = UINT32(crc32.str(token.channelName)).and(UINT32(0xffffffff)).toNumber();
34
+ var crc_uid = UINT32(crc32.str(token.uid)).and(UINT32(0xffffffff)).toNumber();
35
+ var content = AccessTokenContent({
36
+ signature: signature,
37
+ crc_channel: crc_channel,
38
+ crc_uid: crc_uid,
39
+ m: m
40
+ }).pack();
41
+ return version + token.appID + content.toString("base64");
42
+ };
43
+
44
+ this.addPriviledge = function (priviledge, expireTimestamp) {
45
+ token.messages[priviledge] = expireTimestamp;
46
+ };
47
+
48
+ this.fromString = function (originToken) {
49
+ try {
50
+ originVersion = originToken.substr(0, VERSION_LENGTH);
51
+ if (originVersion != version) {
52
+ return false;
53
+ }
54
+ this.appID = originToken.substr(VERSION_LENGTH, APP_ID_LENGTH);
55
+ var originContent = originToken.substr(VERSION_LENGTH + APP_ID_LENGTH);
56
+ var originContentDecodedBuf = Buffer.from(originContent, "base64");
57
+
58
+ var content = unPackContent(originContentDecodedBuf);
59
+ this.signature = content.signature;
60
+ this.crc_channel_name = content.crc_channel_name;
61
+ this.crc_uid = content.crc_uid;
62
+ this.m = content.m;
63
+
64
+ var msgs = unPackMessages(this.m);
65
+ this.salt = msgs.salt;
66
+ this.ts = msgs.ts;
67
+ this.messages = msgs.messages;
68
+ } catch (err) {
69
+ console.log(err);
70
+ return false;
71
+ }
72
+
73
+ return true;
74
+ };
75
+ };
76
+
77
+ module.exports.version = version;
78
+ module.exports.AccessToken = AccessToken;
79
+ module.exports.priviledges = {
80
+ kJoinChannel: 1,
81
+ kPublishAudioStream: 2,
82
+ kPublishVideoStream: 3,
83
+ kPublishDataStream: 4,
84
+ kRtmLogin: 1000
85
+ };
86
+
87
+ var encodeHMac = function (key, message) {
88
+ return crypto.createHmac("sha256", key).update(message).digest();
89
+ };
90
+
91
+ var ByteBuf = function () {
92
+ var that = {
93
+ buffer: Buffer.alloc(1024),
94
+ position: 0
95
+ };
96
+
97
+ that.buffer.fill(0);
98
+
99
+ that.pack = function () {
100
+ var out = Buffer.alloc(that.position);
101
+ that.buffer.copy(out, 0, 0, out.length);
102
+ return out;
103
+ };
104
+
105
+ that.putUint16 = function (v) {
106
+ that.buffer.writeUInt16LE(v, that.position);
107
+ that.position += 2;
108
+ return that;
109
+ };
110
+
111
+ that.putUint32 = function (v) {
112
+ that.buffer.writeUInt32LE(v, that.position);
113
+ that.position += 4;
114
+ return that;
115
+ };
116
+
117
+ that.putBytes = function (bytes) {
118
+ that.putUint16(bytes.length);
119
+ bytes.copy(that.buffer, that.position);
120
+ that.position += bytes.length;
121
+ return that;
122
+ };
123
+
124
+ that.putString = function (str) {
125
+ return that.putBytes(Buffer.from(str));
126
+ };
127
+
128
+ that.putTreeMap = function (map) {
129
+ if (!map) {
130
+ that.putUint16(0);
131
+ return that;
132
+ }
133
+
134
+ that.putUint16(Object.keys(map).length);
135
+ for (var key in map) {
136
+ that.putUint16(key);
137
+ that.putString(map[key]);
138
+ }
139
+
140
+ return that;
141
+ };
142
+
143
+ that.putTreeMapUInt32 = function (map) {
144
+ if (!map) {
145
+ that.putUint16(0);
146
+ return that;
147
+ }
148
+
149
+ that.putUint16(Object.keys(map).length);
150
+ for (var key in map) {
151
+ that.putUint16(key);
152
+ that.putUint32(map[key]);
153
+ }
154
+
155
+ return that;
156
+ };
157
+
158
+ return that;
159
+ };
160
+
161
+ var ReadByteBuf = function (bytes) {
162
+ var that = {
163
+ buffer: bytes,
164
+ position: 0
165
+ };
166
+
167
+ that.getUint16 = function () {
168
+ var ret = that.buffer.readUInt16LE(that.position);
169
+ that.position += 2;
170
+ return ret;
171
+ };
172
+
173
+ that.getUint32 = function () {
174
+ var ret = that.buffer.readUInt32LE(that.position);
175
+ that.position += 4;
176
+ return ret;
177
+ };
178
+
179
+ that.getString = function () {
180
+ var len = that.getUint16();
181
+
182
+ var out = Buffer.alloc(len);
183
+ that.buffer.copy(out, 0, that.position, that.position + len);
184
+ that.position += len;
185
+ return out;
186
+ };
187
+
188
+ that.getTreeMapUInt32 = function () {
189
+ var map = {};
190
+ var len = that.getUint16();
191
+ for (var i = 0; i < len; i++) {
192
+ var key = that.getUint16();
193
+ var value = that.getUint32();
194
+ map[key] = value;
195
+ }
196
+ return map;
197
+ };
198
+
199
+ return that;
200
+ };
201
+ var AccessTokenContent = function (options) {
202
+ options.pack = function () {
203
+ var out = new ByteBuf();
204
+ return out.putString(options.signature).putUint32(options.crc_channel).putUint32(options.crc_uid).putString(options.m).pack();
205
+ };
206
+
207
+ return options;
208
+ };
209
+
210
+ var Message = function (options) {
211
+ options.pack = function () {
212
+ var out = new ByteBuf();
213
+ var val = out.putUint32(options.salt).putUint32(options.ts).putTreeMapUInt32(options.messages).pack();
214
+ return val;
215
+ };
216
+
217
+ return options;
218
+ };
219
+
220
+ var unPackContent = function (bytes) {
221
+ var readbuf = new ReadByteBuf(bytes);
222
+ return AccessTokenContent({
223
+ signature: readbuf.getString(),
224
+ crc_channel_name: readbuf.getUint32(),
225
+ crc_uid: readbuf.getUint32(),
226
+ m: readbuf.getString()
227
+ });
228
+ };
229
+
230
+ var unPackMessages = function (bytes) {
231
+ var readbuf = new ReadByteBuf(bytes);
232
+ return Message({
233
+ salt: readbuf.getUint32(),
234
+ ts: readbuf.getUint32(),
235
+ messages: readbuf.getTreeMapUInt32()
236
+ });
237
+ };