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
package/README.md
CHANGED
|
@@ -2,6 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
To enhance communication security, Agora uses tokens to authenticate users before they access the Agora service, or joining an RTC channel.
|
|
4
4
|
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
This is a [Node.js](https://nodejs.org/en/) module available through the
|
|
8
|
+
[npm registry](https://www.npmjs.com/).
|
|
9
|
+
|
|
10
|
+
Before installing, [download and install Node.js](https://nodejs.org/en/download/).
|
|
11
|
+
|
|
12
|
+
Installation is done using the
|
|
13
|
+
[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
|
|
14
|
+
|
|
15
|
+
```console
|
|
16
|
+
$ npm install agora-token
|
|
17
|
+
```
|
|
18
|
+
|
|
5
19
|
## Code structure
|
|
6
20
|
|
|
7
21
|
Under the `nodejs` directory:
|
package/index.d.ts
CHANGED
|
@@ -1,11 +1,163 @@
|
|
|
1
|
+
export namespace ApaasTokenBuilder {
|
|
2
|
+
/**
|
|
3
|
+
* build user room token
|
|
4
|
+
* @param appId The App ID issued to you by Agora. Apply for a new App ID from
|
|
5
|
+
* Agora Dashboard if it is missing from your kit. See Get an App ID.
|
|
6
|
+
* @param appCertificate Certificate of the application that you registered in
|
|
7
|
+
* the Agora Dashboard. See Get an App Certificate.
|
|
8
|
+
* @param roomUuid The room's id, must be unique.
|
|
9
|
+
* @param userUuid The user's id, must be unique.
|
|
10
|
+
* @param role The user's role.
|
|
11
|
+
* @param expire represented by the number of seconds elapsed since now. If, for example, you want to access the
|
|
12
|
+
* Agora Service within 10 minutes after the token is generated, set expire as 600(seconds).
|
|
13
|
+
* @return The user room token.
|
|
14
|
+
*/
|
|
15
|
+
export function buildRoomUserToken(
|
|
16
|
+
appId: string,
|
|
17
|
+
appCertificate: string,
|
|
18
|
+
roomUuid: string,
|
|
19
|
+
userUuid: string | number,
|
|
20
|
+
role: number,
|
|
21
|
+
expire: number
|
|
22
|
+
): string
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* build user token
|
|
26
|
+
* @param appId The App ID issued to you by Agora. Apply for a new App ID from
|
|
27
|
+
* Agora Dashboard if it is missing from your kit. See Get an App ID.
|
|
28
|
+
* @param appCertificate Certificate of the application that you registered in
|
|
29
|
+
* the Agora Dashboard. See Get an App Certificate.
|
|
30
|
+
* @param userUuid The user's id, must be unique.
|
|
31
|
+
* @param expire represented by the number of seconds elapsed since now. If, for example, you want to access the
|
|
32
|
+
* Agora Service within 10 minutes after the token is generated, set expire as 600(seconds).
|
|
33
|
+
* @return The user token.
|
|
34
|
+
*/
|
|
35
|
+
export function buildUserToken(
|
|
36
|
+
appId: string,
|
|
37
|
+
appCertificate: string,
|
|
38
|
+
userUuid: string | number,
|
|
39
|
+
expire: number
|
|
40
|
+
): string
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* build app token
|
|
44
|
+
* @param appId The App ID issued to you by Agora. Apply for a new App ID from
|
|
45
|
+
* Agora Dashboard if it is missing from your kit. See Get an App ID.
|
|
46
|
+
* @param appCertificate Certificate of the application that you registered in
|
|
47
|
+
* the Agora Dashboard. See Get an App Certificate.
|
|
48
|
+
* @param expire represented by the number of seconds elapsed since now. If, for example, you want to access the
|
|
49
|
+
* Agora Service within 10 minutes after the token is generated, set expire as 600(seconds).
|
|
50
|
+
* @return The app token.
|
|
51
|
+
*/
|
|
52
|
+
export function buildAppToken(appId: string, appCertificate: string, expire: number): string
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export namespace ChatTokenBuilder {
|
|
56
|
+
/**
|
|
57
|
+
* Build the Chat user token.
|
|
58
|
+
*
|
|
59
|
+
* @param appId The App ID issued to you by Agora. Apply for a new App ID from
|
|
60
|
+
* Agora Dashboard if it is missing from your kit. See Get an App ID.
|
|
61
|
+
* @param appCertificate Certificate of the application that you registered in
|
|
62
|
+
* the Agora Dashboard. See Get an App Certificate.
|
|
63
|
+
* @param userUuid The user's id, must be unique.
|
|
64
|
+
* @param expire represented by the number of seconds elapsed since now. If, for example, you want to access the
|
|
65
|
+
* Agora Service within 10 minutes after the token is generated, set expire as 600(seconds).
|
|
66
|
+
* @return The chat user token.
|
|
67
|
+
*/
|
|
68
|
+
export function buildUserToken(
|
|
69
|
+
appId: string,
|
|
70
|
+
appCertificate: string,
|
|
71
|
+
userId: string | number,
|
|
72
|
+
expire: number
|
|
73
|
+
): string
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Build the Chat App token.
|
|
77
|
+
*
|
|
78
|
+
* @param appId The App ID issued to you by Agora. Apply for a new App ID from
|
|
79
|
+
* Agora Dashboard if it is missing from your kit. See Get an App ID.
|
|
80
|
+
* @param appCertificate Certificate of the application that you registered in
|
|
81
|
+
* the Agora Dashboard. See Get an App Certificate.
|
|
82
|
+
* @param expire represented by the number of seconds elapsed since now. If, for example, you want to access the
|
|
83
|
+
* Agora Service within 10 minutes after the token is generated, set expire as 600(seconds).
|
|
84
|
+
* @return The chat App token.
|
|
85
|
+
*/
|
|
86
|
+
export function buildAppToken(appId: string, appCertificate: string, expire: number): string
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export namespace EducationTokenBuilder {
|
|
90
|
+
/**
|
|
91
|
+
* build user room token
|
|
92
|
+
* @param appId The App ID issued to you by Agora. Apply for a new App ID from
|
|
93
|
+
* Agora Dashboard if it is missing from your kit. See Get an App ID.
|
|
94
|
+
* @param appCertificate Certificate of the application that you registered in
|
|
95
|
+
* the Agora Dashboard. See Get an App Certificate.
|
|
96
|
+
* @param roomUuid The room's id, must be unique.
|
|
97
|
+
* @param userUuid The user's id, must be unique.
|
|
98
|
+
* @param role The user's role.
|
|
99
|
+
* @param expire represented by the number of seconds elapsed since now. If, for example, you want to access the
|
|
100
|
+
* Agora Service within 10 minutes after the token is generated, set expire as 600(seconds).
|
|
101
|
+
* @return The user room token.
|
|
102
|
+
*/
|
|
103
|
+
export function buildRoomUserToken(
|
|
104
|
+
appId: string,
|
|
105
|
+
appCertificate: string,
|
|
106
|
+
roomUuid: string,
|
|
107
|
+
userUuid: string | number,
|
|
108
|
+
role: number,
|
|
109
|
+
expire: number
|
|
110
|
+
): string
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* build user token
|
|
114
|
+
* @param appId The App ID issued to you by Agora. Apply for a new App ID from
|
|
115
|
+
* Agora Dashboard if it is missing from your kit. See Get an App ID.
|
|
116
|
+
* @param appCertificate Certificate of the application that you registered in
|
|
117
|
+
* the Agora Dashboard. See Get an App Certificate.
|
|
118
|
+
* @param userUuid The user's id, must be unique.
|
|
119
|
+
* @param expire represented by the number of seconds elapsed since now. If, for example, you want to access the
|
|
120
|
+
* Agora Service within 10 minutes after the token is generated, set expire as 600(seconds).
|
|
121
|
+
* @return The user token.
|
|
122
|
+
*/
|
|
123
|
+
export function buildUserToken(
|
|
124
|
+
appId: string,
|
|
125
|
+
appCertificate: string,
|
|
126
|
+
userUuid: string | number,
|
|
127
|
+
expire: number
|
|
128
|
+
): string
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* build app token
|
|
132
|
+
* @param appId The App ID issued to you by Agora. Apply for a new App ID from
|
|
133
|
+
* Agora Dashboard if it is missing from your kit. See Get an App ID.
|
|
134
|
+
* @param appCertificate Certificate of the application that you registered in
|
|
135
|
+
* the Agora Dashboard. See Get an App Certificate.
|
|
136
|
+
* @param expire represented by the number of seconds elapsed since now. If, for example, you want to access the
|
|
137
|
+
* Agora Service within 10 minutes after the token is generated, set expire as 600(seconds).
|
|
138
|
+
* @return The app token.
|
|
139
|
+
*/
|
|
140
|
+
export function buildAppToken(appId: string, appCertificate: string, expire: number): string
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export namespace FpaTokenBuilder {
|
|
144
|
+
/**
|
|
145
|
+
* Build the FPA token.
|
|
146
|
+
* @param appId The App ID issued to you by Agora. Apply for a new App ID from
|
|
147
|
+
* Agora Dashboard if it is missing from your kit. See Get an App ID.
|
|
148
|
+
* @param appCertificate Certificate of the application that you registered in
|
|
149
|
+
* the Agora Dashboard. See Get an App Certificate.
|
|
150
|
+
* @return The FPA token.
|
|
151
|
+
*/
|
|
152
|
+
export function buildToken(appId: string, appCertificate: string): string
|
|
153
|
+
}
|
|
1
154
|
|
|
2
155
|
export namespace RtcRole {
|
|
3
|
-
export const PUBLISHER: number
|
|
4
|
-
export const SUBSCRIBER: number
|
|
156
|
+
export const PUBLISHER: number
|
|
157
|
+
export const SUBSCRIBER: number
|
|
5
158
|
}
|
|
6
159
|
|
|
7
160
|
export namespace RtcTokenBuilder {
|
|
8
|
-
|
|
9
161
|
/**
|
|
10
162
|
* Builds an RTC token using an Integer uid.
|
|
11
163
|
* @param {*} appId The App ID issued to you by Agora.
|
|
@@ -19,14 +171,23 @@ export namespace RtcTokenBuilder {
|
|
|
19
171
|
* @param {*} uid User ID. A 32-bit unsigned integer with a value ranging from 1 to (2^32-1).
|
|
20
172
|
* @param {*} role See #userRole.
|
|
21
173
|
* - Role.PUBLISHER; RECOMMENDED. Use this role for a voice/video call or a live broadcast.
|
|
22
|
-
* - Role.SUBSCRIBER: ONLY use this role if your live-broadcast scenario requires authentication for [
|
|
23
|
-
* @param {*}
|
|
24
|
-
* @param {*}
|
|
174
|
+
* - Role.SUBSCRIBER: ONLY use this role if your live-broadcast scenario requires authentication for [Co-host](https://docs.agora.io/en/video-calling/get-started/authentication-workflow?#co-host-token-authentication). In order for this role to take effect, please contact our support team to enable authentication for Co-host for you. Otherwise, Role_Subscriber still has the same privileges as Role_Publisher.
|
|
175
|
+
* @param {*} tokenExpire epresented by the number of seconds elapsed since now. If, for example, you want to access the Agora Service within 10 minutes after the token is generated, set tokenExpire as 600(seconds)
|
|
176
|
+
* @param {*} privilegeExpire represented by the number of seconds elapsed since now. If, for example, you want to enable your privilege for 10 minutes, set privilegeExpire as 600(seconds).
|
|
177
|
+
* @return The RTC Token.
|
|
25
178
|
*/
|
|
26
|
-
|
|
179
|
+
export function buildTokenWithUid(
|
|
180
|
+
appId: string,
|
|
181
|
+
appCertificate: string,
|
|
182
|
+
channelName: string,
|
|
183
|
+
uid: string | number,
|
|
184
|
+
role: number,
|
|
185
|
+
tokenExpire: number,
|
|
186
|
+
privilegeExpire: number
|
|
187
|
+
): string
|
|
27
188
|
|
|
28
189
|
/**
|
|
29
|
-
* Builds an RTC token
|
|
190
|
+
* Builds an RTC token with account.
|
|
30
191
|
* @param {*} appId The App ID issued to you by Agora.
|
|
31
192
|
* @param {*} appCertificate Certificate of the application that you registered in the Agora Dashboard.
|
|
32
193
|
* @param {*} channelName The unique channel name for the AgoraRTC session in the string format. The string length must be less than 64 bytes. Supported character scopes are:
|
|
@@ -38,14 +199,22 @@ export namespace RtcTokenBuilder {
|
|
|
38
199
|
* @param {*} account The user account.
|
|
39
200
|
* @param {*} role See #userRole.
|
|
40
201
|
* - Role.PUBLISHER; RECOMMENDED. Use this role for a voice/video call or a live broadcast.
|
|
41
|
-
* - Role.SUBSCRIBER: ONLY use this role if your live-broadcast scenario requires authentication for [
|
|
42
|
-
* @param {*}
|
|
43
|
-
* @param {*}
|
|
44
|
-
* @return The
|
|
202
|
+
* - Role.SUBSCRIBER: ONLY use this role if your live-broadcast scenario requires authentication for [Co-host](https://docs.agora.io/en/video-calling/get-started/authentication-workflow?#co-host-token-authentication). In order for this role to take effect, please contact our support team to enable authentication for Co-host for you. Otherwise, Role_Subscriber still has the same privileges as Role_Publisher.
|
|
203
|
+
* @param {*} tokenExpire epresented by the number of seconds elapsed since now. If, for example, you want to access the Agora Service within 10 minutes after the token is generated, set tokenExpire as 600(seconds)
|
|
204
|
+
* @param {*} privilegeExpire represented by the number of seconds elapsed since now. If, for example, you want to enable your privilege for 10 minutes, set privilegeExpire as 600(seconds).
|
|
205
|
+
* @return The RTC Token.
|
|
45
206
|
*/
|
|
46
|
-
|
|
207
|
+
export function buildTokenWithUserAccount(
|
|
208
|
+
appId: string,
|
|
209
|
+
appCertificate: string,
|
|
210
|
+
channelName: string,
|
|
211
|
+
account: string | number,
|
|
212
|
+
role: number,
|
|
213
|
+
tokenExpire: number,
|
|
214
|
+
privilegeExpire: number
|
|
215
|
+
): string
|
|
47
216
|
|
|
48
|
-
|
|
217
|
+
/**
|
|
49
218
|
* Generates an RTC token with the specified privilege.
|
|
50
219
|
*
|
|
51
220
|
* This method supports generating a token with the following privileges:
|
|
@@ -80,27 +249,28 @@ export namespace RtcTokenBuilder {
|
|
|
80
249
|
* - "!", "#", "$", "%", "&", "(", ")", "+", "-", ":", ";", "<", "=", ".", ">", "?", "@", "[", "]", "^", "_", " {", "}", "|", "~", ",".
|
|
81
250
|
* @param uid The user ID. A 32-bit unsigned integer with a value range from 1 to (2^32 - 1). It must be unique. Set uid as 0, if you do not want to authenticate the user ID, that is, any uid from the app client can join the channel.
|
|
82
251
|
* @param tokenExpire represented by the number of seconds elapsed since now. If, for example, you want to access the
|
|
83
|
-
* Agora Service within 10 minutes after the token is generated, set
|
|
84
|
-
* @param joinChannelPrivilegeExpire
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
* current timestamp plus 600 seconds, the token expires in 10 minutes. If you do not want to enable this privilege,
|
|
94
|
-
* set pubVideoPrivilegeExpire as the current Unix timestamp.
|
|
95
|
-
* @param pubDataStreamPrivilegeExpire The Unix timestamp when the privilege for publishing data streams expires, represented
|
|
96
|
-
* by the sum of the current timestamp plus the valid time period of the token. For example, if you set pubDataStreamPrivilegeExpire as the
|
|
97
|
-
* current timestamp plus 600 seconds, the token expires in 10 minutes. If you do not want to enable this privilege,
|
|
98
|
-
* set pubDataStreamPrivilegeExpire as the current Unix timestamp.
|
|
99
|
-
* @return The new Token
|
|
252
|
+
* Agora Service within 10 minutes after the token is generated, set tokenExpire as 600(seconds).
|
|
253
|
+
* @param joinChannelPrivilegeExpire represented by the number of seconds elapsed since now.
|
|
254
|
+
* If, for example, you want to join channel and expect stay in the channel for 10 minutes, set joinChannelPrivilegeExpire as 600(seconds).
|
|
255
|
+
* @param pubAudioPrivilegeExpire represented by the number of seconds elapsed since now.
|
|
256
|
+
* If, for example, you want to enable publish audio privilege for 10 minutes, set pubAudioPrivilegeExpire as 600(seconds).
|
|
257
|
+
* @param pubVideoPrivilegeExpire represented by the number of seconds elapsed since now.
|
|
258
|
+
* If, for example, you want to enable publish video privilege for 10 minutes, set pubVideoPrivilegeExpire as 600(seconds).
|
|
259
|
+
* @param pubDataStreamPrivilegeExpire represented by the number of seconds elapsed since now.
|
|
260
|
+
* If, for example, you want to enable publish data stream privilege for 10 minutes, set pubDataStreamPrivilegeExpire as 600(seconds).
|
|
261
|
+
* @return The RTC Token
|
|
100
262
|
*/
|
|
101
|
-
export function buildTokenWithUidAndPrivilege(
|
|
102
|
-
|
|
103
|
-
|
|
263
|
+
export function buildTokenWithUidAndPrivilege(
|
|
264
|
+
appId: string,
|
|
265
|
+
appCertificate: string,
|
|
266
|
+
channelName: string,
|
|
267
|
+
uid: string | number,
|
|
268
|
+
tokenExpire: number,
|
|
269
|
+
joinChannelPrivilegeExpire: number,
|
|
270
|
+
pubAudioPrivilegeExpire: number,
|
|
271
|
+
pubVideoPrivilegeExpire: number,
|
|
272
|
+
pubDataStreamPrivilegeExpire: number
|
|
273
|
+
): string
|
|
104
274
|
|
|
105
275
|
/**
|
|
106
276
|
* Generates an RTC token with the specified privilege.
|
|
@@ -137,31 +307,59 @@ export namespace RtcTokenBuilder {
|
|
|
137
307
|
* - "!", "#", "$", "%", "&", "(", ")", "+", "-", ":", ";", "<", "=", ".", ">", "?", "@", "[", "]", "^", "_", " {", "}", "|", "~", ",".
|
|
138
308
|
* @param userAccount The user account.
|
|
139
309
|
* @param tokenExpire represented by the number of seconds elapsed since now. If, for example, you want to access the
|
|
140
|
-
* Agora Service within 10 minutes after the token is generated, set
|
|
141
|
-
* @param joinChannelPrivilegeExpire
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
* current timestamp plus 600 seconds, the token expires in 10 minutes. If you do not want to enable this privilege,
|
|
151
|
-
* set pubVideoPrivilegeExpire as the current Unix timestamp.
|
|
152
|
-
* @param pubDataStreamPrivilegeExpire The Unix timestamp when the privilege for publishing data streams expires, represented
|
|
153
|
-
* by the sum of the current timestamp plus the valid time period of the token. For example, if you set pubDataStreamPrivilegeExpire as the
|
|
154
|
-
* current timestamp plus 600 seconds, the token expires in 10 minutes. If you do not want to enable this privilege,
|
|
155
|
-
* set pubDataStreamPrivilegeExpire as the current Unix timestamp.
|
|
156
|
-
* @return The new Token.
|
|
310
|
+
* Agora Service within 10 minutes after the token is generated, set tokenExpire as 600(seconds).
|
|
311
|
+
* @param joinChannelPrivilegeExpire represented by the number of seconds elapsed since now.
|
|
312
|
+
* If, for example, you want to join channel and expect stay in the channel for 10 minutes, set joinChannelPrivilegeExpire as 600(seconds).
|
|
313
|
+
* @param pubAudioPrivilegeExpire represented by the number of seconds elapsed since now.
|
|
314
|
+
* If, for example, you want to enable publish audio privilege for 10 minutes, set pubAudioPrivilegeExpire as 600(seconds).
|
|
315
|
+
* @param pubVideoPrivilegeExpire represented by the number of seconds elapsed since now.
|
|
316
|
+
* If, for example, you want to enable publish video privilege for 10 minutes, set pubVideoPrivilegeExpire as 600(seconds).
|
|
317
|
+
* @param pubDataStreamPrivilegeExpire represented by the number of seconds elapsed since now.
|
|
318
|
+
* If, for example, you want to enable publish data stream privilege for 10 minutes, set pubDataStreamPrivilegeExpire as 600(seconds).
|
|
319
|
+
* @return The RTC Token.
|
|
157
320
|
*/
|
|
158
|
-
export function BuildTokenWithUserAccountAndPrivilege(
|
|
159
|
-
|
|
160
|
-
|
|
321
|
+
export function BuildTokenWithUserAccountAndPrivilege(
|
|
322
|
+
appId: string,
|
|
323
|
+
appCertificate: string,
|
|
324
|
+
channelName: string,
|
|
325
|
+
account: string | number,
|
|
326
|
+
tokenExpire: number,
|
|
327
|
+
joinChannelPrivilegeExpire: number,
|
|
328
|
+
pubAudioPrivilegeExpire: number,
|
|
329
|
+
pubVideoPrivilegeExpire: number,
|
|
330
|
+
pubDataStreamPrivilegeExpire: number
|
|
331
|
+
): string
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Build an RTC and RTM token with account.
|
|
335
|
+
* @param {*} appId The App ID issued to you by Agora.
|
|
336
|
+
* @param {*} appCertificate Certificate of the application that you registered in the Agora Dashboard.
|
|
337
|
+
* @param {*} channelName The unique channel name for the AgoraRTC session in the string format. The string length must be less than 64 bytes. Supported character scopes are:
|
|
338
|
+
* - The 26 lowercase English letters: a to z.
|
|
339
|
+
* - The 26 uppercase English letters: A to Z.
|
|
340
|
+
* - The 10 digits: 0 to 9.
|
|
341
|
+
* - The space.
|
|
342
|
+
* - "!", "#", "$", "%", "&", "(", ")", "+", "-", ":", ";", "<", "=", ".", ">", "?", "@", "[", "]", "^", "_", " {", "}", "|", "~", ",".
|
|
343
|
+
* @param {*} account The user account.
|
|
344
|
+
* @param {*} role See #userRole.
|
|
345
|
+
* - Role.PUBLISHER; RECOMMENDED. Use this role for a voice/video call or a live broadcast.
|
|
346
|
+
* - Role.SUBSCRIBER: ONLY use this role if your live-broadcast scenario requires authentication for [Co-host](https://docs.agora.io/en/video-calling/get-started/authentication-workflow?#co-host-token-authentication). In order for this role to take effect, please contact our support team to enable authentication for Co-host for you. Otherwise, Role_Subscriber still has the same privileges as Role_Publisher.
|
|
347
|
+
* @param {*} tokenExpire epresented by the number of seconds elapsed since now. If, for example, you want to access the Agora Service within 10 minutes after the token is generated, set tokenExpire as 600(seconds)
|
|
348
|
+
* @param {*} privilegeExpire represented by the number of seconds elapsed since now. If, for example, you want to enable your privilege for 10 minutes, set privilegeExpire as 600(seconds).
|
|
349
|
+
* @return The RTC and RTM Token.
|
|
350
|
+
*/
|
|
351
|
+
export function buildTokenWithRtm(
|
|
352
|
+
appId: string,
|
|
353
|
+
appCertificate: string,
|
|
354
|
+
channelName: string,
|
|
355
|
+
account: string | number,
|
|
356
|
+
role: number,
|
|
357
|
+
tokenExpire: number,
|
|
358
|
+
privilegeExpire: number
|
|
359
|
+
): string
|
|
161
360
|
}
|
|
162
361
|
|
|
163
362
|
export namespace RtmTokenBuilder {
|
|
164
|
-
|
|
165
363
|
/**
|
|
166
364
|
* Build the RTM token.
|
|
167
365
|
*
|
|
@@ -171,91 +369,8 @@ export namespace RtmTokenBuilder {
|
|
|
171
369
|
* the Agora Dashboard. See Get an App Certificate.
|
|
172
370
|
* @param userId The user's account, max length is 64 Bytes.
|
|
173
371
|
* @param expire represented by the number of seconds elapsed since now. If, for example, you want to access the
|
|
174
|
-
* Agora Service within 10 minutes after the token is generated, set
|
|
372
|
+
* Agora Service within 10 minutes after the token is generated, set expire as 600(seconds).
|
|
175
373
|
* @return The RTM token.
|
|
176
374
|
*/
|
|
177
|
-
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
export namespace EducationTokenBuilder {
|
|
181
|
-
/**
|
|
182
|
-
* build user room token
|
|
183
|
-
* @param appId The App ID issued to you by Agora. Apply for a new App ID from
|
|
184
|
-
* Agora Dashboard if it is missing from your kit. See Get an App ID.
|
|
185
|
-
* @param appCertificate Certificate of the application that you registered in
|
|
186
|
-
* the Agora Dashboard. See Get an App Certificate.
|
|
187
|
-
* @param roomUuid The room's id, must be unique.
|
|
188
|
-
* @param userUuid The user's id, must be unique.
|
|
189
|
-
* @param role The user's role, such as 0(invisible), 1(teacher), 2(student), 3(assistant), 4(observer) etc.
|
|
190
|
-
* @param expire represented by the number of seconds elapsed since now. If, for example, you want to access the
|
|
191
|
-
* Agora Service within 10 minutes after the token is generated, set expireTimestamp as 600(seconds).
|
|
192
|
-
* @return The education user room token.
|
|
193
|
-
*/
|
|
194
|
-
export function buildRoomUserToken(appId: string, appCertificate: string, roomUuid: string, userUuid: string | number, role: number, expire: number): string;
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* build user individual token
|
|
198
|
-
* @param appId The App ID issued to you by Agora. Apply for a new App ID from
|
|
199
|
-
* Agora Dashboard if it is missing from your kit. See Get an App ID.
|
|
200
|
-
* @param appCertificate Certificate of the application that you registered in
|
|
201
|
-
* the Agora Dashboard. See Get an App Certificate.
|
|
202
|
-
* @param userUuid The user's id, must be unique.
|
|
203
|
-
* @param expire represented by the number of seconds elapsed since now. If, for example, you want to access the
|
|
204
|
-
* Agora Service within 10 minutes after the token is generated, set expireTimestamp as 600(seconds).
|
|
205
|
-
* @return The education user token.
|
|
206
|
-
*/
|
|
207
|
-
export function buildUserToken(appId: string, appCertificate: string, userUuid: string | number, expire: number): string;
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* build app global token
|
|
211
|
-
* @param appId The App ID issued to you by Agora. Apply for a new App ID from
|
|
212
|
-
* Agora Dashboard if it is missing from your kit. See Get an App ID.
|
|
213
|
-
* @param appCertificate Certificate of the application that you registered in
|
|
214
|
-
* the Agora Dashboard. See Get an App Certificate.
|
|
215
|
-
* @param expire represented by the number of seconds elapsed since now. If, for example, you want to access the
|
|
216
|
-
* Agora Service within 10 minutes after the token is generated, set expireTimestamp as 600(seconds).
|
|
217
|
-
* @return The education global token.
|
|
218
|
-
*/
|
|
219
|
-
export function buildAppToken(appId: string, appCertificate: string, expire: number): string;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
export namespace FpaTokenBuilder {
|
|
223
|
-
/**
|
|
224
|
-
* Build the FPA token.
|
|
225
|
-
* @param appId The App ID issued to you by Agora. Apply for a new App ID from
|
|
226
|
-
* Agora Dashboard if it is missing from your kit. See Get an App ID.
|
|
227
|
-
* @param appCertificate Certificate of the application that you registered in
|
|
228
|
-
* the Agora Dashboard. See Get an App Certificate.
|
|
229
|
-
* @return The FPA token.
|
|
230
|
-
*/
|
|
231
|
-
export function buildToken(appId: string, appCertificate: string): string;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
export namespace ChatTokenBuilder {
|
|
235
|
-
/**
|
|
236
|
-
* Build the Chat user token.
|
|
237
|
-
*
|
|
238
|
-
* @param appId The App ID issued to you by Agora. Apply for a new App ID from
|
|
239
|
-
* Agora Dashboard if it is missing from your kit. See Get an App ID.
|
|
240
|
-
* @param appCertificate Certificate of the application that you registered in
|
|
241
|
-
* the Agora Dashboard. See Get an App Certificate.
|
|
242
|
-
* @param userUuid The user's id, must be unique.
|
|
243
|
-
* @param expire represented by the number of seconds elapsed since now. If, for example, you want to access the
|
|
244
|
-
* Agora Service within 10 minutes after the token is generated, set expireTimestamp as 600(seconds).
|
|
245
|
-
* @return The chat user token.
|
|
246
|
-
*/
|
|
247
|
-
export function buildUserToken(appId: string, appCertificate: string, userId: string | number, expire: number): string;
|
|
248
|
-
|
|
249
|
-
/**
|
|
250
|
-
* Build the Chat App token.
|
|
251
|
-
*
|
|
252
|
-
* @param appId The App ID issued to you by Agora. Apply for a new App ID from
|
|
253
|
-
* Agora Dashboard if it is missing from your kit. See Get an App ID.
|
|
254
|
-
* @param appCertificate Certificate of the application that you registered in
|
|
255
|
-
* the Agora Dashboard. See Get an App Certificate.
|
|
256
|
-
* @param expire represented by the number of seconds elapsed since now. If, for example, you want to access the
|
|
257
|
-
* Agora Service within 10 minutes after the token is generated, set expireTimestamp as 600(seconds).
|
|
258
|
-
* @return The chat App token.
|
|
259
|
-
*/
|
|
260
|
-
export function buildAppToken(appId: string, appCertificate: string, expire: number): string;
|
|
375
|
+
export function buildToken(appId: string, appCertificate: string, userId: string | number, expire: number): string
|
|
261
376
|
}
|
package/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
module.exports = {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
2
|
+
ApaasTokenBuilder: require('./src/ApaasTokenBuilder').ApaasTokenBuilder,
|
|
3
|
+
ChatTokenBuilder: require('./src/ChatTokenBuilder').ChatTokenBuilder,
|
|
4
|
+
EducationTokenBuilder: require('./src/EducationTokenBuilder').EducationTokenBuilder,
|
|
5
|
+
FpaTokenBuilder: require('./src/FpaTokenBuilder').FpaTokenBuilder,
|
|
6
|
+
RtcRole: require('./src/RtcTokenBuilder2').Role,
|
|
7
|
+
RtcTokenBuilder: require('./src/RtcTokenBuilder2').RtcTokenBuilder,
|
|
8
|
+
RtmTokenBuilder: require('./src/RtmTokenBuilder2').RtmTokenBuilder
|
|
9
|
+
}
|
package/package.json
CHANGED
|
@@ -1,19 +1,24 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
2
|
+
"name": "agora-token",
|
|
3
|
+
"version": "2.0.4",
|
|
4
|
+
"description": "",
|
|
5
|
+
"homepage": "https://github.com/AgoraIO/Tools/tree/master/DynamicKey/AgoraDynamicKey/nodejs",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "nodeunit test/*.js"
|
|
9
|
+
},
|
|
10
|
+
"types": "index.d.ts",
|
|
11
|
+
"author": "",
|
|
12
|
+
"license": "ISC",
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"crc-32": "^1.2.0",
|
|
15
|
+
"cuint": "0.2.2",
|
|
16
|
+
"md5": "^2.3.0"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@eslint/js": "^9.8.0",
|
|
20
|
+
"eslint": "^9.8.0",
|
|
21
|
+
"globals": "^15.8.0",
|
|
22
|
+
"nodeunit": "^0.11.3"
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -1,18 +1,26 @@
|
|
|
1
|
-
const {AccessToken2, ServiceRtc} = require(
|
|
2
|
-
const RtcRole = require(
|
|
1
|
+
const { AccessToken2, ServiceRtc } = require("../src/AccessToken2");
|
|
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);
|
|
10
15
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
16
|
+
console.log("App Id:", appId);
|
|
17
|
+
console.log("App Certificate:", appCertificate);
|
|
18
|
+
if (appId == undefined || appId == "" || appCertificate == undefined || appCertificate == "") {
|
|
19
|
+
console.log("Need to set environment variable AGORA_APP_ID and AGORA_APP_CERTIFICATE");
|
|
20
|
+
process.exit(1);
|
|
21
|
+
}
|
|
14
22
|
|
|
15
|
-
let token = new AccessToken2(
|
|
16
|
-
let rtc_service = new ServiceRtc(channelName, uid)
|
|
17
|
-
token.add_service(rtc_service)
|
|
18
|
-
console.log(token.build())
|
|
23
|
+
let token = new AccessToken2(appId, appCertificate, currentTimestamp, expirationTimeInSeconds);
|
|
24
|
+
let rtc_service = new ServiceRtc(channelName, uid);
|
|
25
|
+
token.add_service(rtc_service);
|
|
26
|
+
console.log("Token: ", token.build());
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const ApaasTokenBuilder = require('../src/ApaasTokenBuilder').ApaasTokenBuilder
|
|
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
|
+
|
|
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 = ApaasTokenBuilder.buildRoomUserToken(appId, appCertificate, roomUuid, userUuid, role, expire)
|
|
21
|
+
console.log('Apaas room user token:', tokenRoomUserToken)
|
|
22
|
+
|
|
23
|
+
const tokenUserToken = ApaasTokenBuilder.buildUserToken(appId, appCertificate, userUuid, expire)
|
|
24
|
+
console.log('Apaas user token:', tokenUserToken)
|
|
25
|
+
|
|
26
|
+
const tokenAppToken = ApaasTokenBuilder.buildAppToken(appId, appCertificate, expire)
|
|
27
|
+
console.log('Apaas app token:', tokenAppToken)
|