agora-token 2.0.4 → 2.0.5
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/index.d.ts +43 -0
- package/package.json +1 -1
- package/sample/AccessToken2Sample.js +29 -19
- package/sample/RtcTokenBuilder2Sample.js +16 -0
- package/src/RtcTokenBuilder2.js +60 -0
package/index.d.ts
CHANGED
|
@@ -357,6 +357,49 @@ export namespace RtcTokenBuilder {
|
|
|
357
357
|
tokenExpire: number,
|
|
358
358
|
privilegeExpire: number
|
|
359
359
|
): string
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Build an RTC and RTM token with account.
|
|
363
|
+
* @param {*} appId The App ID issued to you by Agora.
|
|
364
|
+
* @param {*} appCertificate Certificate of the application that you registered in the Agora Dashboard.
|
|
365
|
+
* @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:
|
|
366
|
+
* - The 26 lowercase English letters: a to z.
|
|
367
|
+
* - The 26 uppercase English letters: A to Z.
|
|
368
|
+
* - The 10 digits: 0 to 9.
|
|
369
|
+
* - The space.
|
|
370
|
+
* - "!", "#", "$", "%", "&", "(", ")", "+", "-", ":", ";", "<", "=", ".", ">", "?", "@", "[", "]", "^", "_", " {", "}", "|", "~", ",".
|
|
371
|
+
* @param {*} rtcAccount The RTC user's account, max length is 255 Bytes.
|
|
372
|
+
* @param {*} rtcRole See #userRole.
|
|
373
|
+
* - Role.PUBLISHER; RECOMMENDED. Use this role for a voice/video call or a live broadcast.
|
|
374
|
+
* - 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.
|
|
375
|
+
* @param {*} rtcTokenExpire 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)
|
|
376
|
+
* @param {*} joinChannelPrivilegeExpire represented by the number of seconds elapsed since now.
|
|
377
|
+
* If, for example, you want to join channel and expect stay in the channel for 10 minutes, set joinChannelPrivilegeExpire as 600(seconds).
|
|
378
|
+
* @param {*} pubAudioPrivilegeExpire represented by the number of seconds elapsed since now.
|
|
379
|
+
* If, for example, you want to enable publish audio privilege for 10 minutes, set pubAudioPrivilegeExpire as 600(seconds).
|
|
380
|
+
* @param {*} pubVideoPrivilegeExpire represented by the number of seconds elapsed since now.
|
|
381
|
+
* If, for example, you want to enable publish video privilege for 10 minutes, set pubVideoPrivilegeExpire as 600(seconds).
|
|
382
|
+
* @param {*} pubDataStreamPrivilegeExpire represented by the number of seconds elapsed since now.
|
|
383
|
+
* If, for example, you want to enable publish data stream privilege for 10 minutes, set pubDataStreamPrivilegeExpire as 600(seconds).
|
|
384
|
+
* @param rtmUserId: The RTM user's account, max length is 255 Bytes.
|
|
385
|
+
* @param rtmTokenExpire: represented by the number of seconds elapsed since now. If, for example,
|
|
386
|
+
* you want to access the Agora Service within 10 minutes after the token is generated, set rtmTokenExpire as 600(seconds).
|
|
387
|
+
* * @return The RTC and RTM Token.
|
|
388
|
+
*/
|
|
389
|
+
export function buildTokenWithRtm2(
|
|
390
|
+
appId: string,
|
|
391
|
+
appCertificate: string,
|
|
392
|
+
channelName: string,
|
|
393
|
+
rtcAccount: string | number,
|
|
394
|
+
rtcRole: number,
|
|
395
|
+
rtcTokenExpire: number,
|
|
396
|
+
joinChannelPrivilegeExpire: number,
|
|
397
|
+
pubAudioPrivilegeExpire: number,
|
|
398
|
+
pubVideoPrivilegeExpire: number,
|
|
399
|
+
pubDataStreamPrivilegeExpire: number,
|
|
400
|
+
rtmUserId: string | number,
|
|
401
|
+
rtmTokenExpire: number
|
|
402
|
+
): string
|
|
360
403
|
}
|
|
361
404
|
|
|
362
405
|
export namespace RtmTokenBuilder {
|
package/package.json
CHANGED
|
@@ -1,26 +1,36 @@
|
|
|
1
|
-
const { AccessToken2, ServiceRtc } = require(
|
|
2
|
-
const RtcRole = require(
|
|
1
|
+
const { AccessToken2, ServiceRtc } = require('../src/AccessToken2')
|
|
2
|
+
const RtcRole = require('../src/RtcTokenBuilder2').Role
|
|
3
3
|
|
|
4
4
|
// Need to set environment variable AGORA_APP_ID
|
|
5
|
-
const appId = process.env.AGORA_APP_ID
|
|
5
|
+
const appId = process.env.AGORA_APP_ID
|
|
6
6
|
// Need to set environment variable AGORA_APP_CERTIFICATE
|
|
7
|
-
const appCertificate = process.env.AGORA_APP_CERTIFICATE
|
|
7
|
+
const appCertificate = process.env.AGORA_APP_CERTIFICATE
|
|
8
8
|
|
|
9
|
-
const channelName =
|
|
10
|
-
const uid = 2882341273
|
|
11
|
-
const account =
|
|
12
|
-
const role = RtcRole.PUBLISHER
|
|
13
|
-
const
|
|
14
|
-
const
|
|
9
|
+
const channelName = '7d72365eb983485397e3e3f9d460bdda'
|
|
10
|
+
const uid = 2882341273
|
|
11
|
+
// const account = '2882341273'
|
|
12
|
+
const role = RtcRole.PUBLISHER
|
|
13
|
+
const tokenExpirationInSecond = 3600
|
|
14
|
+
const joinChannelPrivilegeExpireInSeconds = 3600
|
|
15
|
+
const pubAudioPrivilegeExpireInSeconds = 3600
|
|
16
|
+
const pubVideoPrivilegeExpireInSeconds = 3600
|
|
17
|
+
const pubDataStreamPrivilegeExpireInSeconds = 3600
|
|
15
18
|
|
|
16
|
-
console.log(
|
|
17
|
-
console.log(
|
|
18
|
-
if (appId == undefined || appId ==
|
|
19
|
-
console.log(
|
|
20
|
-
process.exit(1)
|
|
19
|
+
console.log('App Id:', appId)
|
|
20
|
+
console.log('App Certificate:', appCertificate)
|
|
21
|
+
if (appId == undefined || appId == '' || appCertificate == undefined || appCertificate == '') {
|
|
22
|
+
console.log('Need to set environment variable AGORA_APP_ID and AGORA_APP_CERTIFICATE')
|
|
23
|
+
process.exit(1)
|
|
21
24
|
}
|
|
22
25
|
|
|
23
|
-
let token = new AccessToken2(appId, appCertificate,
|
|
24
|
-
let
|
|
25
|
-
|
|
26
|
-
|
|
26
|
+
let token = new AccessToken2(appId, appCertificate, 0, tokenExpirationInSecond)
|
|
27
|
+
let serviceRtc = new ServiceRtc(channelName, uid)
|
|
28
|
+
serviceRtc.add_privilege(ServiceRtc.kPrivilegeJoinChannel, joinChannelPrivilegeExpireInSeconds)
|
|
29
|
+
if (role == RtcRole.PUBLISHER) {
|
|
30
|
+
serviceRtc.add_privilege(ServiceRtc.kPrivilegePublishAudioStream, pubAudioPrivilegeExpireInSeconds)
|
|
31
|
+
serviceRtc.add_privilege(ServiceRtc.kPrivilegePublishVideoStream, pubVideoPrivilegeExpireInSeconds)
|
|
32
|
+
serviceRtc.add_privilege(ServiceRtc.kPrivilegePublishDataStream, pubDataStreamPrivilegeExpireInSeconds)
|
|
33
|
+
}
|
|
34
|
+
token.add_service(serviceRtc)
|
|
35
|
+
|
|
36
|
+
console.log('Token: ', token.build())
|
|
@@ -84,3 +84,19 @@ const tokenWithRtm = RtcTokenBuilder.buildTokenWithRtm(
|
|
|
84
84
|
privilegeExpirationInSecond
|
|
85
85
|
)
|
|
86
86
|
console.log('Token with RTM:', tokenWithRtm)
|
|
87
|
+
|
|
88
|
+
const tokenWithRtm2 = RtcTokenBuilder.buildTokenWithRtm2(
|
|
89
|
+
appId,
|
|
90
|
+
appCertificate,
|
|
91
|
+
channelName,
|
|
92
|
+
account,
|
|
93
|
+
role,
|
|
94
|
+
tokenExpirationInSecond,
|
|
95
|
+
joinChannelPrivilegeExpireInSeconds,
|
|
96
|
+
pubAudioPrivilegeExpireInSeconds,
|
|
97
|
+
pubVideoPrivilegeExpireInSeconds,
|
|
98
|
+
pubDataStreamPrivilegeExpireInSeconds,
|
|
99
|
+
account,
|
|
100
|
+
tokenExpirationInSecond
|
|
101
|
+
)
|
|
102
|
+
console.log('Token with RTM:', tokenWithRtm2)
|
package/src/RtcTokenBuilder2.js
CHANGED
|
@@ -268,6 +268,66 @@ class RtcTokenBuilder {
|
|
|
268
268
|
|
|
269
269
|
return token.build()
|
|
270
270
|
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Build an RTC and RTM token with account.
|
|
274
|
+
* @param {*} appId The App ID issued to you by Agora.
|
|
275
|
+
* @param {*} appCertificate Certificate of the application that you registered in the Agora Dashboard.
|
|
276
|
+
* @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:
|
|
277
|
+
* - The 26 lowercase English letters: a to z.
|
|
278
|
+
* - The 26 uppercase English letters: A to Z.
|
|
279
|
+
* - The 10 digits: 0 to 9.
|
|
280
|
+
* - The space.
|
|
281
|
+
* - "!", "#", "$", "%", "&", "(", ")", "+", "-", ":", ";", "<", "=", ".", ">", "?", "@", "[", "]", "^", "_", " {", "}", "|", "~", ",".
|
|
282
|
+
* @param {*} rtcAccount The RTC user's account, max length is 255 Bytes.
|
|
283
|
+
* @param {*} rtcRole See #userRole.
|
|
284
|
+
* - Role.PUBLISHER; RECOMMENDED. Use this role for a voice/video call or a live broadcast.
|
|
285
|
+
* - 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.
|
|
286
|
+
* @param {*} rtcTokenExpire 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)
|
|
287
|
+
* @param {*} joinChannelPrivilegeExpire represented by the number of seconds elapsed since now.
|
|
288
|
+
* If, for example, you want to join channel and expect stay in the channel for 10 minutes, set joinChannelPrivilegeExpire as 600(seconds).
|
|
289
|
+
* @param {*} pubAudioPrivilegeExpire represented by the number of seconds elapsed since now.
|
|
290
|
+
* If, for example, you want to enable publish audio privilege for 10 minutes, set pubAudioPrivilegeExpire as 600(seconds).
|
|
291
|
+
* @param {*} pubVideoPrivilegeExpire represented by the number of seconds elapsed since now.
|
|
292
|
+
* If, for example, you want to enable publish video privilege for 10 minutes, set pubVideoPrivilegeExpire as 600(seconds).
|
|
293
|
+
* @param {*} pubDataStreamPrivilegeExpire represented by the number of seconds elapsed since now.
|
|
294
|
+
* If, for example, you want to enable publish data stream privilege for 10 minutes, set pubDataStreamPrivilegeExpire as 600(seconds).
|
|
295
|
+
* @param {*} rtmUserId: The RTM user's account, max length is 255 Bytes.
|
|
296
|
+
* @param {*} rtmTokenExpire: represented by the number of seconds elapsed since now. If, for example,
|
|
297
|
+
* you want to access the Agora Service within 10 minutes after the token is generated, set rtmTokenExpire as 600(seconds).
|
|
298
|
+
* * @return The RTC and RTM Token.
|
|
299
|
+
*/
|
|
300
|
+
static buildTokenWithRtm2(
|
|
301
|
+
appId,
|
|
302
|
+
appCertificate,
|
|
303
|
+
channelName,
|
|
304
|
+
rtcAccount,
|
|
305
|
+
rtcRole,
|
|
306
|
+
rtcTokenExpire,
|
|
307
|
+
joinChannelPrivilegeExpire,
|
|
308
|
+
pubAudioPrivilegeExpire,
|
|
309
|
+
pubVideoPrivilegeExpire,
|
|
310
|
+
pubDataStreamPrivilegeExpire,
|
|
311
|
+
rtmUserId,
|
|
312
|
+
rtmTokenExpire
|
|
313
|
+
) {
|
|
314
|
+
let token = new AccessToken(appId, appCertificate, 0, rtcTokenExpire)
|
|
315
|
+
|
|
316
|
+
let serviceRtc = new ServiceRtc(channelName, rtcAccount)
|
|
317
|
+
serviceRtc.add_privilege(ServiceRtc.kPrivilegeJoinChannel, joinChannelPrivilegeExpire)
|
|
318
|
+
if (rtcRole == Role.PUBLISHER) {
|
|
319
|
+
serviceRtc.add_privilege(ServiceRtc.kPrivilegePublishAudioStream, pubAudioPrivilegeExpire)
|
|
320
|
+
serviceRtc.add_privilege(ServiceRtc.kPrivilegePublishVideoStream, pubVideoPrivilegeExpire)
|
|
321
|
+
serviceRtc.add_privilege(ServiceRtc.kPrivilegePublishDataStream, pubDataStreamPrivilegeExpire)
|
|
322
|
+
}
|
|
323
|
+
token.add_service(serviceRtc)
|
|
324
|
+
|
|
325
|
+
let serviceRtm = new ServiceRtm(rtmUserId)
|
|
326
|
+
serviceRtm.add_privilege(ServiceRtm.kPrivilegeLogin, rtmTokenExpire)
|
|
327
|
+
token.add_service(serviceRtm)
|
|
328
|
+
|
|
329
|
+
return token.build()
|
|
330
|
+
}
|
|
271
331
|
}
|
|
272
332
|
|
|
273
333
|
module.exports.RtcTokenBuilder = RtcTokenBuilder
|