agora-token 2.0.2 → 2.0.4
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.
- package/README.md +14 -0
- package/eslint.config.mjs +8 -0
- package/index.d.ts +255 -140
- package/index.js +8 -7
- package/package.json +23 -18
- package/sample/AccessToken2Sample.js +20 -12
- package/sample/ApaasTokenSample.js +27 -0
- package/sample/ChatTokenBuilderSample.js +16 -5
- package/sample/EducationTokenSample.js +33 -20
- package/sample/FpaTokenBuilderSample.js +14 -5
- package/sample/RtcTokenBuilder2Sample.js +73 -15
- package/sample/RtcTokenBuilderSample.js +21 -16
- package/sample/RtmTokenBuilder2Sample.js +14 -3
- package/sample/RtmTokenBuilderSample.js +20 -11
- package/sample.js +1 -1
- package/src/AccessToken2.js +38 -30
- package/src/ApaasTokenBuilder.js +80 -0
- package/src/ChatTokenBuilder.js +17 -18
- package/src/EducationTokenBuilder.js +21 -21
- package/src/RtcTokenBuilder.js +41 -44
- package/src/RtcTokenBuilder2.js +138 -61
- package/src/RtmTokenBuilder2.js +9 -9
- package/test/AccessToken2Test.js +111 -105
- package/test/ApaasTokenBuilderTest.js +45 -0
- package/test/EducationTokenBuilderTest.js +35 -35
|
@@ -1,11 +1,22 @@
|
|
|
1
1
|
const ChatTokenBuilder = require("../src/ChatTokenBuilder").ChatTokenBuilder;
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
|
|
3
|
+
// Need to set environment variable AGORA_APP_ID
|
|
4
|
+
const appId = process.env.AGORA_APP_ID;
|
|
5
|
+
// Need to set environment variable AGORA_APP_CERTIFICATE
|
|
6
|
+
const appCertificate = process.env.AGORA_APP_CERTIFICATE;
|
|
7
|
+
|
|
4
8
|
const userUuid = "a7180cb0-1d4a-11ed-9210-89ff47c9da5e";
|
|
5
9
|
const expirationInSeconds = 600;
|
|
6
10
|
|
|
11
|
+
console.log("App Id:", appId);
|
|
12
|
+
console.log("App Certificate:", appCertificate);
|
|
13
|
+
if (appId == undefined || appId == "" || appCertificate == undefined || appCertificate == "") {
|
|
14
|
+
console.log("Need to set environment variable AGORA_APP_ID and AGORA_APP_CERTIFICATE");
|
|
15
|
+
process.exit(1);
|
|
16
|
+
}
|
|
17
|
+
|
|
7
18
|
const userToken = ChatTokenBuilder.buildUserToken(appId, appCertificate, userUuid, expirationInSeconds);
|
|
8
|
-
console.log("Chat User Token:
|
|
19
|
+
console.log("Chat User Token:", userToken);
|
|
9
20
|
|
|
10
|
-
const appToken = ChatTokenBuilder.buildAppToken(appId, appCertificate, expirationInSeconds)
|
|
11
|
-
console.log("Chat App Token:
|
|
21
|
+
const appToken = ChatTokenBuilder.buildAppToken(appId, appCertificate, expirationInSeconds);
|
|
22
|
+
console.log("Chat App Token:", appToken);
|
|
@@ -1,21 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
* run this test with command:
|
|
3
|
-
* nodeunit test/RtcTokenBuilder2Test.js
|
|
4
|
-
* see https://github.com/caolan/nodeunit
|
|
5
|
-
*/
|
|
6
|
-
const EduTokenBuilder = require('../src/EducationTokenBuilder').EducationTokenBuilder
|
|
7
|
-
const {AccessToken2, ServiceEducation, kEducationServiceType} = require('../src/AccessToken2')
|
|
8
|
-
const appId = "970CA35de60c44645bbae8a215061b33"
|
|
9
|
-
const appCertificate = "5CFd2fd1755d40ecb72977518be15d3b"
|
|
10
|
-
const expire = 600
|
|
11
|
-
const roomUuid = "123"
|
|
12
|
-
const userUuid = "2882341273"
|
|
13
|
-
const role = 1
|
|
1
|
+
const EducationTokenBuilder = require('../src/EducationTokenBuilder').EducationTokenBuilder
|
|
14
2
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
3
|
+
// Need to set environment variable AGORA_APP_ID
|
|
4
|
+
const appId = process.env.AGORA_APP_ID
|
|
5
|
+
// Need to set environment variable AGORA_APP_CERTIFICATE
|
|
6
|
+
const appCertificate = process.env.AGORA_APP_CERTIFICATE
|
|
7
|
+
|
|
8
|
+
const expire = 600
|
|
9
|
+
const roomUuid = '123'
|
|
10
|
+
const userUuid = '2882341273'
|
|
11
|
+
const role = 1
|
|
12
|
+
|
|
13
|
+
console.log('App Id:', appId)
|
|
14
|
+
console.log('App Certificate:', appCertificate)
|
|
15
|
+
if (appId == undefined || appId == '' || appCertificate == undefined || appCertificate == '') {
|
|
16
|
+
console.log('Need to set environment variable AGORA_APP_ID and AGORA_APP_CERTIFICATE')
|
|
17
|
+
process.exit(1)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const tokenRoomUserToken = EducationTokenBuilder.buildRoomUserToken(
|
|
21
|
+
appId,
|
|
22
|
+
appCertificate,
|
|
23
|
+
roomUuid,
|
|
24
|
+
userUuid,
|
|
25
|
+
role,
|
|
26
|
+
expire
|
|
27
|
+
)
|
|
28
|
+
console.log('Education room user token:', tokenRoomUserToken)
|
|
29
|
+
|
|
30
|
+
const tokenUserToken = EducationTokenBuilder.buildUserToken(appId, appCertificate, userUuid, expire)
|
|
31
|
+
console.log('Education user token:', tokenUserToken)
|
|
32
|
+
|
|
33
|
+
const tokenAppToken = EducationTokenBuilder.buildAppToken(appId, appCertificate, expire)
|
|
34
|
+
console.log('Education app token:', tokenAppToken)
|
|
@@ -1,7 +1,16 @@
|
|
|
1
|
-
const FpaTokenBuilder = require(
|
|
1
|
+
const FpaTokenBuilder = require("../src/FpaTokenBuilder").FpaTokenBuilder;
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
const
|
|
3
|
+
// Need to set environment variable AGORA_APP_ID
|
|
4
|
+
const appId = process.env.AGORA_APP_ID;
|
|
5
|
+
// Need to set environment variable AGORA_APP_CERTIFICATE
|
|
6
|
+
const appCertificate = process.env.AGORA_APP_CERTIFICATE;
|
|
5
7
|
|
|
6
|
-
|
|
7
|
-
console.log("
|
|
8
|
+
console.log("App Id:", appId);
|
|
9
|
+
console.log("App Certificate:", appCertificate);
|
|
10
|
+
if (appId == undefined || appId == "" || appCertificate == undefined || appCertificate == "") {
|
|
11
|
+
console.log("Need to set environment variable AGORA_APP_ID and AGORA_APP_CERTIFICATE");
|
|
12
|
+
process.exit(1);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
let token = FpaTokenBuilder.buildToken(appId, appCertificate);
|
|
16
|
+
console.log("Token with FPA service:", token);
|
|
@@ -1,28 +1,86 @@
|
|
|
1
1
|
const RtcTokenBuilder = require('../src/RtcTokenBuilder2').RtcTokenBuilder
|
|
2
2
|
const RtcRole = require('../src/RtcTokenBuilder2').Role
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
const
|
|
4
|
+
// Need to set environment variable AGORA_APP_ID
|
|
5
|
+
const appId = process.env.AGORA_APP_ID
|
|
6
|
+
// Need to set environment variable AGORA_APP_CERTIFICATE
|
|
7
|
+
const appCertificate = process.env.AGORA_APP_CERTIFICATE
|
|
8
|
+
|
|
6
9
|
const channelName = '7d72365eb983485397e3e3f9d460bdda'
|
|
7
10
|
const uid = 2882341273
|
|
8
|
-
const account =
|
|
11
|
+
const account = '2882341273'
|
|
9
12
|
const role = RtcRole.PUBLISHER
|
|
10
|
-
const expirationInSeconds = 3600
|
|
11
|
-
|
|
12
13
|
const tokenExpirationInSecond = 3600
|
|
13
14
|
const privilegeExpirationInSecond = 3600
|
|
15
|
+
const joinChannelPrivilegeExpireInSeconds = 3600
|
|
16
|
+
const pubAudioPrivilegeExpireInSeconds = 3600
|
|
17
|
+
const pubVideoPrivilegeExpireInSeconds = 3600
|
|
18
|
+
const pubDataStreamPrivilegeExpireInSeconds = 3600
|
|
19
|
+
|
|
20
|
+
console.log('App Id:', appId)
|
|
21
|
+
console.log('App Certificate:', appCertificate)
|
|
22
|
+
if (appId == undefined || appId == '' || appCertificate == undefined || appCertificate == '') {
|
|
23
|
+
console.log('Need to set environment variable AGORA_APP_ID and AGORA_APP_CERTIFICATE')
|
|
24
|
+
process.exit(1)
|
|
25
|
+
}
|
|
26
|
+
|
|
14
27
|
// Build token with uid
|
|
15
|
-
const
|
|
16
|
-
|
|
28
|
+
const tokenWithUid = RtcTokenBuilder.buildTokenWithUid(
|
|
29
|
+
appId,
|
|
30
|
+
appCertificate,
|
|
31
|
+
channelName,
|
|
32
|
+
uid,
|
|
33
|
+
role,
|
|
34
|
+
tokenExpirationInSecond,
|
|
35
|
+
privilegeExpirationInSecond
|
|
36
|
+
)
|
|
37
|
+
console.log('Token with int uid:', tokenWithUid)
|
|
17
38
|
|
|
18
39
|
// Build token with user account
|
|
19
|
-
const
|
|
20
|
-
|
|
40
|
+
const tokenWithUserAccount = RtcTokenBuilder.buildTokenWithUserAccount(
|
|
41
|
+
appId,
|
|
42
|
+
appCertificate,
|
|
43
|
+
channelName,
|
|
44
|
+
account,
|
|
45
|
+
role,
|
|
46
|
+
tokenExpirationInSecond,
|
|
47
|
+
privilegeExpirationInSecond
|
|
48
|
+
)
|
|
49
|
+
console.log('Token with user account:', tokenWithUserAccount)
|
|
50
|
+
|
|
51
|
+
const tokenWithUidAndPrivilege = RtcTokenBuilder.buildTokenWithUidAndPrivilege(
|
|
52
|
+
appId,
|
|
53
|
+
appCertificate,
|
|
54
|
+
channelName,
|
|
55
|
+
uid,
|
|
56
|
+
tokenExpirationInSecond,
|
|
57
|
+
joinChannelPrivilegeExpireInSeconds,
|
|
58
|
+
pubAudioPrivilegeExpireInSeconds,
|
|
59
|
+
pubVideoPrivilegeExpireInSeconds,
|
|
60
|
+
pubDataStreamPrivilegeExpireInSeconds
|
|
61
|
+
)
|
|
62
|
+
console.log('Token with int uid and privilege:', tokenWithUidAndPrivilege)
|
|
21
63
|
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
64
|
+
const tokenWithUserAccountAndPrivilege = RtcTokenBuilder.BuildTokenWithUserAccountAndPrivilege(
|
|
65
|
+
appId,
|
|
66
|
+
appCertificate,
|
|
67
|
+
channelName,
|
|
68
|
+
account,
|
|
69
|
+
tokenExpirationInSecond,
|
|
70
|
+
joinChannelPrivilegeExpireInSeconds,
|
|
71
|
+
pubAudioPrivilegeExpireInSeconds,
|
|
72
|
+
pubVideoPrivilegeExpireInSeconds,
|
|
73
|
+
pubDataStreamPrivilegeExpireInSeconds
|
|
74
|
+
)
|
|
75
|
+
console.log('Token with user account and privilege:', tokenWithUserAccountAndPrivilege)
|
|
25
76
|
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
77
|
+
const tokenWithRtm = RtcTokenBuilder.buildTokenWithRtm(
|
|
78
|
+
appId,
|
|
79
|
+
appCertificate,
|
|
80
|
+
channelName,
|
|
81
|
+
account,
|
|
82
|
+
role,
|
|
83
|
+
tokenExpirationInSecond,
|
|
84
|
+
privilegeExpirationInSecond
|
|
85
|
+
)
|
|
86
|
+
console.log('Token with RTM:', tokenWithRtm)
|
|
@@ -1,25 +1,30 @@
|
|
|
1
|
-
const RtcTokenBuilder = require(
|
|
2
|
-
const RtcRole = require(
|
|
1
|
+
const RtcTokenBuilder = require("../src/RtcTokenBuilder").RtcTokenBuilder;
|
|
2
|
+
const RtcRole = require("../src/RtcTokenBuilder").Role;
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
|
|
4
|
+
// Need to set environment variable AGORA_APP_ID
|
|
5
|
+
const appId = process.env.AGORA_APP_ID;
|
|
6
|
+
// Need to set environment variable AGORA_APP_CERTIFICATE
|
|
7
|
+
const appCertificate = process.env.AGORA_APP_CERTIFICATE;
|
|
8
|
+
|
|
9
|
+
const channelName = "7d72365eb983485397e3e3f9d460bdda";
|
|
7
10
|
const uid = 2882341273;
|
|
8
11
|
const account = "2882341273";
|
|
9
12
|
const role = RtcRole.PUBLISHER;
|
|
13
|
+
const expirationTimeInSeconds = 3600;
|
|
14
|
+
const currentTimestamp = Math.floor(Date.now() / 1000);
|
|
15
|
+
const privilegeExpiredTs = currentTimestamp + expirationTimeInSeconds;
|
|
10
16
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
// IMPORTANT! Build token with either the uid or with the user account. Comment out the option you do not want to use below.
|
|
17
|
+
console.log("App Id:", appId);
|
|
18
|
+
console.log("App Certificate:", appCertificate);
|
|
19
|
+
if (appId == undefined || appId == "" || appCertificate == undefined || appCertificate == "") {
|
|
20
|
+
console.log("Need to set environment variable AGORA_APP_ID and AGORA_APP_CERTIFICATE");
|
|
21
|
+
process.exit(1);
|
|
22
|
+
}
|
|
18
23
|
|
|
19
24
|
// Build token with uid
|
|
20
|
-
const
|
|
21
|
-
console.log("Token With Integer Number Uid:
|
|
25
|
+
const tokenWithUid = RtcTokenBuilder.buildTokenWithUid(appId, appCertificate, channelName, uid, role, privilegeExpiredTs);
|
|
26
|
+
console.log("Token With Integer Number Uid:", tokenWithUid);
|
|
22
27
|
|
|
23
28
|
// Build token with user account
|
|
24
|
-
const
|
|
25
|
-
console.log("Token With UserAccount:
|
|
29
|
+
const tokenWithAccount = RtcTokenBuilder.buildTokenWithAccount(appId, appCertificate, channelName, account, role, privilegeExpiredTs);
|
|
30
|
+
console.log("Token With UserAccount:", tokenWithAccount);
|
|
@@ -1,8 +1,19 @@
|
|
|
1
1
|
const RtmTokenBuilder = require("../src/RtmTokenBuilder2").RtmTokenBuilder;
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
|
|
3
|
+
// Need to set environment variable AGORA_APP_ID
|
|
4
|
+
const appId = process.env.AGORA_APP_ID;
|
|
5
|
+
// Need to set environment variable AGORA_APP_CERTIFICATE
|
|
6
|
+
const appCertificate = process.env.AGORA_APP_CERTIFICATE;
|
|
7
|
+
|
|
4
8
|
const userId = "test_user_id";
|
|
5
9
|
const expirationInSeconds = 3600;
|
|
6
10
|
|
|
11
|
+
console.log("App Id:", appId);
|
|
12
|
+
console.log("App Certificate:", appCertificate);
|
|
13
|
+
if (appId == undefined || appId == "" || appCertificate == undefined || appCertificate == "") {
|
|
14
|
+
console.log("Need to set environment variable AGORA_APP_ID and AGORA_APP_CERTIFICATE");
|
|
15
|
+
process.exit(1);
|
|
16
|
+
}
|
|
17
|
+
|
|
7
18
|
const token = RtmTokenBuilder.buildToken(appId, appCertificate, userId, expirationInSeconds);
|
|
8
|
-
console.log("Rtm Token:
|
|
19
|
+
console.log("Rtm Token:", token);
|
|
@@ -1,14 +1,23 @@
|
|
|
1
|
-
const RtmTokenBuilder = require(
|
|
2
|
-
const RtmRole = require(
|
|
3
|
-
const Priviledges = require(
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const
|
|
1
|
+
const RtmTokenBuilder = require("../src/RtmTokenBuilder").RtmTokenBuilder;
|
|
2
|
+
const RtmRole = require("../src/RtmTokenBuilder").Role;
|
|
3
|
+
const Priviledges = require("../src/AccessToken").priviledges;
|
|
4
|
+
|
|
5
|
+
// Need to set environment variable AGORA_APP_ID
|
|
6
|
+
const appId = process.env.AGORA_APP_ID;
|
|
7
|
+
// Need to set environment variable AGORA_APP_CERTIFICATE
|
|
8
|
+
const appCertificate = process.env.AGORA_APP_CERTIFICATE;
|
|
7
9
|
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
+
const account = "test_user_id";
|
|
11
|
+
const expirationTimeInSeconds = 3600;
|
|
12
|
+
const currentTimestamp = Math.floor(Date.now() / 1000);
|
|
13
|
+
const privilegeExpiredTs = currentTimestamp + expirationTimeInSeconds;
|
|
10
14
|
|
|
11
|
-
|
|
15
|
+
console.log("App Id:", appId);
|
|
16
|
+
console.log("App Certificate:", appCertificate);
|
|
17
|
+
if (appId == undefined || appId == "" || appCertificate == undefined || appCertificate == "") {
|
|
18
|
+
console.log("Need to set environment variable AGORA_APP_ID and AGORA_APP_CERTIFICATE");
|
|
19
|
+
process.exit(1);
|
|
20
|
+
}
|
|
12
21
|
|
|
13
|
-
const token = RtmTokenBuilder.buildToken(
|
|
14
|
-
console.log("Rtm Token:
|
|
22
|
+
const token = RtmTokenBuilder.buildToken(appId, appCertificate, account, RtmRole, privilegeExpiredTs);
|
|
23
|
+
console.log("Rtm Token:", token);
|
package/sample.js
CHANGED
|
@@ -22,7 +22,7 @@ const generateRtcToken = () => {
|
|
|
22
22
|
console.log("Token With Integer Number Uid: " + tokenA);
|
|
23
23
|
|
|
24
24
|
// Build token with user account
|
|
25
|
-
const tokenB = RtcTokenBuilder.
|
|
25
|
+
const tokenB = RtcTokenBuilder.buildTokenWithUserAccount(appID, appCertificate, channelName, account, role, privilegeExpiredTs);
|
|
26
26
|
console.log("Token With UserAccount: " + tokenB);
|
|
27
27
|
}
|
|
28
28
|
|
package/src/AccessToken2.js
CHANGED
|
@@ -4,7 +4,7 @@ const VERSION_LENGTH = 3
|
|
|
4
4
|
const APP_ID_LENGTH = 32
|
|
5
5
|
|
|
6
6
|
const getVersion = () => {
|
|
7
|
-
return
|
|
7
|
+
return '007'
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
class Service {
|
|
@@ -77,7 +77,7 @@ const kRtmServiceType = 2
|
|
|
77
77
|
class ServiceRtm extends Service {
|
|
78
78
|
constructor(user_id) {
|
|
79
79
|
super(kRtmServiceType)
|
|
80
|
-
this.__user_id = user_id ||
|
|
80
|
+
this.__user_id = user_id || ''
|
|
81
81
|
}
|
|
82
82
|
|
|
83
83
|
pack() {
|
|
@@ -95,7 +95,6 @@ class ServiceRtm extends Service {
|
|
|
95
95
|
|
|
96
96
|
ServiceRtm.kPrivilegeLogin = 1
|
|
97
97
|
|
|
98
|
-
|
|
99
98
|
const kFpaServiceType = 4
|
|
100
99
|
|
|
101
100
|
class ServiceFpa extends Service {
|
|
@@ -115,13 +114,12 @@ class ServiceFpa extends Service {
|
|
|
115
114
|
|
|
116
115
|
ServiceFpa.kPrivilegeLogin = 1
|
|
117
116
|
|
|
118
|
-
|
|
119
117
|
const kChatServiceType = 5
|
|
120
118
|
|
|
121
119
|
class ServiceChat extends Service {
|
|
122
120
|
constructor(user_id) {
|
|
123
121
|
super(kChatServiceType)
|
|
124
|
-
this.__user_id = user_id ||
|
|
122
|
+
this.__user_id = user_id || ''
|
|
125
123
|
}
|
|
126
124
|
|
|
127
125
|
pack() {
|
|
@@ -140,13 +138,13 @@ class ServiceChat extends Service {
|
|
|
140
138
|
ServiceChat.kPrivilegeUser = 1
|
|
141
139
|
ServiceChat.kPrivilegeApp = 2
|
|
142
140
|
|
|
143
|
-
const
|
|
141
|
+
const kApaasServiceType = 7
|
|
144
142
|
|
|
145
|
-
class
|
|
143
|
+
class ServiceApaas extends Service {
|
|
146
144
|
constructor(roomUuid, userUuid, role) {
|
|
147
|
-
super(
|
|
148
|
-
this.__room_uuid = roomUuid ||
|
|
149
|
-
this.__user_uuid = userUuid ||
|
|
145
|
+
super(kApaasServiceType)
|
|
146
|
+
this.__room_uuid = roomUuid || ''
|
|
147
|
+
this.__user_uuid = userUuid || ''
|
|
150
148
|
this.__role = role || -1
|
|
151
149
|
}
|
|
152
150
|
|
|
@@ -167,10 +165,9 @@ class ServiceEducation extends Service {
|
|
|
167
165
|
}
|
|
168
166
|
}
|
|
169
167
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
168
|
+
ServiceApaas.PRIVILEGE_ROOM_USER = 1
|
|
169
|
+
ServiceApaas.PRIVILEGE_USER = 2
|
|
170
|
+
ServiceApaas.PRIVILEGE_APP = 3
|
|
174
171
|
|
|
175
172
|
class AccessToken2 {
|
|
176
173
|
constructor(appId, appCertificate, issueTs, expire) {
|
|
@@ -179,7 +176,7 @@ class AccessToken2 {
|
|
|
179
176
|
this.issueTs = issueTs || new Date().getTime() / 1000
|
|
180
177
|
this.expire = expire
|
|
181
178
|
// salt ranges in (1, 99999999)
|
|
182
|
-
this.salt = Math.floor(Math.random() *
|
|
179
|
+
this.salt = Math.floor(Math.random() * 99999999) + 1
|
|
183
180
|
this.services = {}
|
|
184
181
|
}
|
|
185
182
|
|
|
@@ -190,7 +187,7 @@ class AccessToken2 {
|
|
|
190
187
|
}
|
|
191
188
|
|
|
192
189
|
__build_check() {
|
|
193
|
-
let is_uuid =
|
|
190
|
+
let is_uuid = data => {
|
|
194
191
|
if (data.length !== APP_ID_LENGTH) {
|
|
195
192
|
return false
|
|
196
193
|
}
|
|
@@ -198,7 +195,7 @@ class AccessToken2 {
|
|
|
198
195
|
return !!buf
|
|
199
196
|
}
|
|
200
197
|
|
|
201
|
-
const {appId, appCertificate, services} = this
|
|
198
|
+
const { appId, appCertificate, services } = this
|
|
202
199
|
if (!is_uuid(appId) || !is_uuid(appCertificate)) {
|
|
203
200
|
return false
|
|
204
201
|
}
|
|
@@ -215,15 +212,17 @@ class AccessToken2 {
|
|
|
215
212
|
|
|
216
213
|
build() {
|
|
217
214
|
if (!this.__build_check()) {
|
|
218
|
-
return
|
|
215
|
+
return ''
|
|
219
216
|
}
|
|
220
217
|
|
|
221
218
|
let signing = this.__signing()
|
|
222
|
-
let signing_info = new ByteBuf()
|
|
219
|
+
let signing_info = new ByteBuf()
|
|
220
|
+
.putString(this.appId)
|
|
223
221
|
.putUint32(this.issueTs)
|
|
224
222
|
.putUint32(this.expire)
|
|
225
223
|
.putUint32(this.salt)
|
|
226
|
-
.putUint16(Object.keys(this.services).length)
|
|
224
|
+
.putUint16(Object.keys(this.services).length)
|
|
225
|
+
.pack()
|
|
227
226
|
Object.values(this.services).forEach(service => {
|
|
228
227
|
signing_info = Buffer.concat([signing_info, service.pack()])
|
|
229
228
|
})
|
|
@@ -268,8 +267,8 @@ var encodeHMac = function (key, message) {
|
|
|
268
267
|
|
|
269
268
|
var ByteBuf = function () {
|
|
270
269
|
var that = {
|
|
271
|
-
buffer: Buffer.alloc(1024)
|
|
272
|
-
|
|
270
|
+
buffer: Buffer.alloc(1024),
|
|
271
|
+
position: 0
|
|
273
272
|
}
|
|
274
273
|
|
|
275
274
|
that.buffer.fill(0)
|
|
@@ -349,8 +348,8 @@ var ByteBuf = function () {
|
|
|
349
348
|
|
|
350
349
|
var ReadByteBuf = function (bytes) {
|
|
351
350
|
var that = {
|
|
352
|
-
buffer: bytes
|
|
353
|
-
|
|
351
|
+
buffer: bytes,
|
|
352
|
+
position: 0
|
|
354
353
|
}
|
|
355
354
|
|
|
356
355
|
that.getUint16 = function () {
|
|
@@ -375,7 +374,7 @@ var ReadByteBuf = function (bytes) {
|
|
|
375
374
|
var len = that.getUint16()
|
|
376
375
|
|
|
377
376
|
var out = Buffer.alloc(len)
|
|
378
|
-
that.buffer.copy(out, 0, that.position,
|
|
377
|
+
that.buffer.copy(out, 0, that.position, that.position + len)
|
|
379
378
|
that.position += len
|
|
380
379
|
return out
|
|
381
380
|
}
|
|
@@ -402,13 +401,22 @@ var ReadByteBuf = function (bytes) {
|
|
|
402
401
|
}
|
|
403
402
|
|
|
404
403
|
AccessToken2.kServices = {}
|
|
404
|
+
AccessToken2.kServices[kApaasServiceType] = ServiceApaas
|
|
405
|
+
AccessToken2.kServices[kChatServiceType] = ServiceChat
|
|
406
|
+
AccessToken2.kServices[kFpaServiceType] = ServiceFpa
|
|
405
407
|
AccessToken2.kServices[kRtcServiceType] = ServiceRtc
|
|
406
408
|
AccessToken2.kServices[kRtmServiceType] = ServiceRtm
|
|
407
|
-
AccessToken2.kServices[kFpaServiceType] = ServiceFpa
|
|
408
|
-
AccessToken2.kServices[kChatServiceType] = ServiceChat
|
|
409
|
-
AccessToken2.kServices[kEducationServiceType] = ServiceEducation
|
|
410
409
|
|
|
411
410
|
module.exports = {
|
|
412
|
-
AccessToken2,
|
|
413
|
-
|
|
411
|
+
AccessToken2,
|
|
412
|
+
kApaasServiceType,
|
|
413
|
+
kChatServiceType,
|
|
414
|
+
kFpaServiceType,
|
|
415
|
+
kRtcServiceType,
|
|
416
|
+
kRtmServiceType,
|
|
417
|
+
ServiceApaas,
|
|
418
|
+
ServiceChat,
|
|
419
|
+
ServiceFpa,
|
|
420
|
+
ServiceRtc,
|
|
421
|
+
ServiceRtm
|
|
414
422
|
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
const md5 = require('md5')
|
|
2
|
+
|
|
3
|
+
const AccessToken = require('./AccessToken2').AccessToken2
|
|
4
|
+
const ServiceApaas = require('./AccessToken2').ServiceApaas
|
|
5
|
+
const ServiceChat = require('./AccessToken2').ServiceChat
|
|
6
|
+
const ServiceRtm = require('./AccessToken2').ServiceRtm
|
|
7
|
+
|
|
8
|
+
class ApaasTokenBuilder {
|
|
9
|
+
/**
|
|
10
|
+
* build user room token
|
|
11
|
+
* @param appId The App ID issued to you by Agora. Apply for a new App ID from
|
|
12
|
+
* Agora Dashboard if it is missing from your kit. See Get an App ID.
|
|
13
|
+
* @param appCertificate Certificate of the application that you registered in
|
|
14
|
+
* the Agora Dashboard. See Get an App Certificate.
|
|
15
|
+
* @param roomUuid The room's id, must be unique.
|
|
16
|
+
* @param userUuid The user's id, must be unique.
|
|
17
|
+
* @param role The user's role.
|
|
18
|
+
* @param expire represented by the number of seconds elapsed since now. If, for example, you want to access the
|
|
19
|
+
* Agora Service within 10 minutes after the token is generated, set expire as 600(seconds).
|
|
20
|
+
* @return The user room token.
|
|
21
|
+
*/
|
|
22
|
+
static buildRoomUserToken(appId, appCertificate, roomUuid, userUuid, role, expire) {
|
|
23
|
+
let accessToken = new AccessToken(appId, appCertificate, 0, expire)
|
|
24
|
+
|
|
25
|
+
let chatUserId = md5(userUuid)
|
|
26
|
+
let apaasService = new ServiceApaas(roomUuid, userUuid, role)
|
|
27
|
+
accessToken.add_service(apaasService)
|
|
28
|
+
|
|
29
|
+
let rtmService = new ServiceRtm(userUuid)
|
|
30
|
+
rtmService.add_privilege(ServiceRtm.kPrivilegeLogin, expire)
|
|
31
|
+
accessToken.add_service(rtmService)
|
|
32
|
+
|
|
33
|
+
let chatService = new ServiceChat(chatUserId)
|
|
34
|
+
chatService.add_privilege(ServiceChat.kPrivilegeUser, expire)
|
|
35
|
+
accessToken.add_service(chatService)
|
|
36
|
+
|
|
37
|
+
return accessToken.build()
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* build user token
|
|
42
|
+
* @param appId The App ID issued to you by Agora. Apply for a new App ID from
|
|
43
|
+
* Agora Dashboard if it is missing from your kit. See Get an App ID.
|
|
44
|
+
* @param appCertificate Certificate of the application that you registered in
|
|
45
|
+
* the Agora Dashboard. See Get an App Certificate.
|
|
46
|
+
* @param userUuid The user's id, must be unique.
|
|
47
|
+
* @param expire represented by the number of seconds elapsed since now. If, for example, you want to access the
|
|
48
|
+
* Agora Service within 10 minutes after the token is generated, set expire as 600(seconds).
|
|
49
|
+
* @return The user token.
|
|
50
|
+
*/
|
|
51
|
+
static buildUserToken(appId, appCertificate, userUuid, expire) {
|
|
52
|
+
let accessToken = new AccessToken(appId, appCertificate, 0, expire)
|
|
53
|
+
let apaasService = new ServiceApaas('', userUuid)
|
|
54
|
+
apaasService.add_privilege(ServiceApaas.PRIVILEGE_USER, expire)
|
|
55
|
+
accessToken.add_service(apaasService)
|
|
56
|
+
|
|
57
|
+
return accessToken.build()
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* build app token
|
|
62
|
+
* @param appId The App ID issued to you by Agora. Apply for a new App ID from
|
|
63
|
+
* Agora Dashboard if it is missing from your kit. See Get an App ID.
|
|
64
|
+
* @param appCertificate Certificate of the application that you registered in
|
|
65
|
+
* the Agora Dashboard. See Get an App Certificate.
|
|
66
|
+
* @param expire represented by the number of seconds elapsed since now. If, for example, you want to access the
|
|
67
|
+
* Agora Service within 10 minutes after the token is generated, set expire as 600(seconds).
|
|
68
|
+
* @return The app token.
|
|
69
|
+
*/
|
|
70
|
+
static buildAppToken(appId, appCertificate, expire) {
|
|
71
|
+
let accessToken = new AccessToken(appId, appCertificate, 0, expire)
|
|
72
|
+
let apaasService = new ServiceApaas()
|
|
73
|
+
apaasService.add_privilege(ServiceApaas.PRIVILEGE_APP, expire)
|
|
74
|
+
accessToken.add_service(apaasService)
|
|
75
|
+
|
|
76
|
+
return accessToken.build()
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
module.exports.ApaasTokenBuilder = ApaasTokenBuilder
|
package/src/ChatTokenBuilder.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
const AccessToken = require(
|
|
2
|
-
const ServiceChat = require(
|
|
1
|
+
const AccessToken = require('../src/AccessToken2').AccessToken2
|
|
2
|
+
const ServiceChat = require('../src/AccessToken2').ServiceChat
|
|
3
3
|
|
|
4
4
|
class ChatTokenBuilder {
|
|
5
5
|
/**
|
|
@@ -11,15 +11,15 @@ class ChatTokenBuilder {
|
|
|
11
11
|
* the Agora Dashboard. See Get an App Certificate.
|
|
12
12
|
* @param userUuid The user's id, must be unique.
|
|
13
13
|
* @param expire represented by the number of seconds elapsed since now. If, for example, you want to access the
|
|
14
|
-
* Agora Service within 10 minutes after the token is generated, set
|
|
15
|
-
* @return The
|
|
14
|
+
* Agora Service within 10 minutes after the token is generated, set expire as 600(seconds).
|
|
15
|
+
* @return The Chat User token.
|
|
16
16
|
*/
|
|
17
17
|
static buildUserToken(appId, appCertificate, userUuid, expire) {
|
|
18
|
-
const token = new AccessToken(appId, appCertificate, null, expire)
|
|
19
|
-
const serviceChat = new ServiceChat(userUuid)
|
|
20
|
-
serviceChat.add_privilege(ServiceChat.kPrivilegeUser, expire)
|
|
21
|
-
token.add_service(serviceChat)
|
|
22
|
-
return token.build()
|
|
18
|
+
const token = new AccessToken(appId, appCertificate, null, expire)
|
|
19
|
+
const serviceChat = new ServiceChat(userUuid)
|
|
20
|
+
serviceChat.add_privilege(ServiceChat.kPrivilegeUser, expire)
|
|
21
|
+
token.add_service(serviceChat)
|
|
22
|
+
return token.build()
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
/**
|
|
@@ -30,17 +30,16 @@ class ChatTokenBuilder {
|
|
|
30
30
|
* @param appCertificate Certificate of the application that you registered in
|
|
31
31
|
* the Agora Dashboard. See Get an App Certificate.
|
|
32
32
|
* @param expire represented by the number of seconds elapsed since now. If, for example, you want to access the
|
|
33
|
-
* Agora Service within 10 minutes after the token is generated, set
|
|
34
|
-
* @return The
|
|
33
|
+
* Agora Service within 10 minutes after the token is generated, set expire as 600(seconds).
|
|
34
|
+
* @return The Chat App token.
|
|
35
35
|
*/
|
|
36
36
|
static buildAppToken(appId, appCertificate, expire) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
37
|
+
const token = new AccessToken(appId, appCertificate, null, expire)
|
|
38
|
+
const serviceChat = new ServiceChat()
|
|
39
|
+
serviceChat.add_privilege(ServiceChat.kPrivilegeApp, expire)
|
|
40
|
+
token.add_service(serviceChat)
|
|
41
|
+
return token.build()
|
|
42
42
|
}
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
exports.ChatTokenBuilder = ChatTokenBuilder
|
|
46
|
-
|
|
45
|
+
exports.ChatTokenBuilder = ChatTokenBuilder
|