forked from AgoraIO/Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccessToken.js
More file actions
258 lines (219 loc) · 6.76 KB
/
Copy pathAccessToken.js
File metadata and controls
258 lines (219 loc) · 6.76 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
var crypto = require('crypto');
var crc32 = require('crc-32');
var UINT32 = require('cuint').UINT32;
var version = "006";
var randomInt = Math.floor(Math.random() * 0xFFFFFFFF);
const VERSION_LENGTH = 3;
const APP_ID_LENGTH = 32;
var AccessToken = function (appID, appCertificate, channelName, uid) {
let token = this;
this.appID = appID;
this.appCertificate = appCertificate;
this.channelName = channelName;
this.messages = {};
this.salt = randomInt;
this.ts = Math.floor(new Date() / 1000) + (24 * 3600);
if (uid === 0) {
this.uid = "";
} else {
this.uid = `${uid}`;
}
this.build = function () {
var m = Message({
salt: token.salt
, ts: token.ts
, messages: token.messages
}).pack();
var toSign = Buffer.concat(
[Buffer.from(token.appID, 'utf8'),
Buffer.from(token.channelName, 'utf8'),
Buffer.from(token.uid, 'utf8'),
m]);
var signature = encodeHMac(token.appCertificate, toSign);
var crc_channel = UINT32(crc32.str(token.channelName)).and(UINT32(0xffffffff)).toNumber();
var crc_uid = UINT32(crc32.str(token.uid)).and(UINT32(0xffffffff)).toNumber();
var content = AccessTokenContent({
signature: signature,
crc_channel: crc_channel,
crc_uid: crc_uid,
m: m
}).pack();
return (version + token.appID + content.toString('base64'));
}
this.addPriviledge = function (priviledge, expireTimestamp) {
token.messages[priviledge] = expireTimestamp;
};
this.fromString = function (originToken) {
try {
originVersion = originToken.substr(0, VERSION_LENGTH);
if(originVersion != version) {
return false;
}
var originAppID = originToken.substr(VERSION_LENGTH, (VERSION_LENGTH + APP_ID_LENGTH));
var originContent = originToken.substr((VERSION_LENGTH + APP_ID_LENGTH));
var originContentDecodedBuf = Buffer.from(originContent, 'base64');
var content = unPackContent(originContentDecodedBuf);
this.signature = content.signature;
this.crc_channel_name = content.crc_channel_name;
this.crc_uid = content.crc_uid;
this.m = content.m;
var msgs = unPackMessages(this.m);
this.salt = msgs.salt;
this.ts = msgs.ts;
this.messages = msgs.messages;
} catch (err) {
console.log(err);
return false;
}
return true;
};
};
module.exports.version = version;
module.exports.AccessToken = AccessToken;
module.exports.priviledges = {
kJoinChannel: 1,
kPublishAudioStream: 2,
kPublishVideoStream: 3,
kPublishDataStream: 4,
kPublishAudiocdn: 5,
kPublishVideoCdn: 6,
kRequestPublishAudioStream: 7,
kRequestPublishVideoStream: 8,
kRequestPublishDataStream: 9,
kInvitePublishAudioStream: 10,
kInvitePublishVideoStream: 11,
kInvitePublishDataStream: 12,
kAdministrateChannel: 101,
kRtmLogin: 1000
};
var encodeHMac = function (key, message) {
return crypto.createHmac('sha256', key).update(message).digest();
};
var ByteBuf = function () {
var that = {
buffer: Buffer.alloc(1024)
, position: 0
};
that.buffer.fill(0);
that.pack = function () {
var out = Buffer.alloc(that.position);
that.buffer.copy(out, 0, 0, out.length);
return out;
};
that.putUint16 = function (v) {
that.buffer.writeUInt16LE(v, that.position);
that.position += 2;
return that;
};
that.putUint32 = function (v) {
that.buffer.writeUInt32LE(v, that.position);
that.position += 4;
return that;
};
that.putBytes = function (bytes) {
that.putUint16(bytes.length);
bytes.copy(that.buffer, that.position);
that.position += bytes.length;
return that;
};
that.putString = function (str) {
return that.putBytes(Buffer.from(str));
};
that.putTreeMap = function (map) {
if (!map) {
that.putUint16(0);
return that;
}
that.putUint16(Object.keys(map).length);
for (var key in map) {
that.putUint16(key);
that.putString(map[key]);
}
return that;
};
that.putTreeMapUInt32 = function (map) {
if (!map) {
that.putUint16(0);
return that;
}
that.putUint16(Object.keys(map).length);
for (var key in map) {
that.putUint16(key);
that.putUint32(map[key]);
}
return that;
};
return that;
}
var ReadByteBuf = function(bytes) {
var that = {
buffer: bytes
, position: 0
};
that.getUint16 = function () {
var ret = that.buffer.readUInt16LE(that.position);
that.position += 2;
return ret;
};
that.getUint32 = function () {
var ret = that.buffer.readUInt32LE(that.position);
that.position += 4;
return ret;
};
that.getString = function () {
var len = that.getUint16();
var out = Buffer.alloc(len);
that.buffer.copy(out, 0, that.position, (that.position + len));
that.position += len;
return out;
};
that.getTreeMapUInt32 = function () {
var map = {};
var len = that.getUint16();
for( var i = 0; i < len; i++) {
var key = that.getUint16();
var value = that.getUint32();
map[key] = value;
}
return map;
};
return that;
}
var AccessTokenContent = function (options) {
options.pack = function () {
var out = new ByteBuf();
return out.putString(options.signature)
.putUint32(options.crc_channel)
.putUint32(options.crc_uid)
.putString(options.m).pack();
}
return options;
}
var Message = function (options) {
options.pack = function () {
var out = new ByteBuf();
var val = out
.putUint32(options.salt)
.putUint32(options.ts)
.putTreeMapUInt32(options.messages).pack();
return val;
}
return options;
}
var unPackContent = function(bytes) {
var readbuf = new ReadByteBuf(bytes);
return AccessTokenContent({
signature: readbuf.getString(),
crc_channel_name: readbuf.getUint32(),
crc_uid: readbuf.getUint32(),
m: readbuf.getString()
});
}
var unPackMessages = function(bytes) {
var readbuf = new ReadByteBuf(bytes);
return Message({
salt: readbuf.getUint32(),
ts: readbuf.getUint32(),
messages: readbuf.getTreeMapUInt32()
});
}