agora-token 2.0.0 → 2.0.2
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 +29 -0
- package/index.js +2 -1
- package/package.json +1 -1
- package/sample/ChatTokenBuilderSample.js +11 -0
- package/src/ChatTokenBuilder.js +46 -0
- package/test/ChatTokenBuilderTest.js +34 -0
package/index.d.ts
CHANGED
|
@@ -230,3 +230,32 @@ export namespace FpaTokenBuilder {
|
|
|
230
230
|
*/
|
|
231
231
|
export function buildToken(appId: string, appCertificate: string): string;
|
|
232
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;
|
|
261
|
+
}
|
package/index.js
CHANGED
|
@@ -3,5 +3,6 @@ module.exports = {
|
|
|
3
3
|
RtcRole: require('./src/RtcTokenBuilder2').Role,
|
|
4
4
|
RtmTokenBuilder: require('./src/RtmTokenBuilder2').RtmTokenBuilder,
|
|
5
5
|
EducationTokenBuilder: require('./src/EducationTokenBuilder').EducationTokenBuilder,
|
|
6
|
-
FpaTokenBuilder: require('./src/FpaTokenBuilder').FpaTokenBuilder
|
|
6
|
+
FpaTokenBuilder: require('./src/FpaTokenBuilder').FpaTokenBuilder,
|
|
7
|
+
ChatTokenBuilder: require('./src/ChatTokenBuilder').ChatTokenBuilder
|
|
7
8
|
};
|
package/package.json
CHANGED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const ChatTokenBuilder = require("../src/ChatTokenBuilder").ChatTokenBuilder;
|
|
2
|
+
const appId = "970CA35de60c44645bbae8a215061b33";
|
|
3
|
+
const appCertificate = "5CFd2fd1755d40ecb72977518be15d3b";
|
|
4
|
+
const userUuid = "a7180cb0-1d4a-11ed-9210-89ff47c9da5e";
|
|
5
|
+
const expirationInSeconds = 600;
|
|
6
|
+
|
|
7
|
+
const userToken = ChatTokenBuilder.buildUserToken(appId, appCertificate, userUuid, expirationInSeconds);
|
|
8
|
+
console.log("Chat User Token: " + userToken + "\n");
|
|
9
|
+
|
|
10
|
+
const appToken = ChatTokenBuilder.buildAppToken(appId, appCertificate, expirationInSeconds)
|
|
11
|
+
console.log("Chat App Token: " + appToken);
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
const AccessToken = require("../src/AccessToken2").AccessToken2;
|
|
2
|
+
const ServiceChat = require("../src/AccessToken2").ServiceChat;
|
|
3
|
+
|
|
4
|
+
class ChatTokenBuilder {
|
|
5
|
+
/**
|
|
6
|
+
* Build the Chat user token.
|
|
7
|
+
*
|
|
8
|
+
* @param appId The App ID issued to you by Agora. Apply for a new App ID from
|
|
9
|
+
* Agora Dashboard if it is missing from your kit. See Get an App ID.
|
|
10
|
+
* @param appCertificate Certificate of the application that you registered in
|
|
11
|
+
* the Agora Dashboard. See Get an App Certificate.
|
|
12
|
+
* @param userUuid The user's id, must be unique.
|
|
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 expireTimestamp as 600(seconds).
|
|
15
|
+
* @return The chat user token.
|
|
16
|
+
*/
|
|
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();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Build the Chat App token.
|
|
27
|
+
*
|
|
28
|
+
* @param appId The App ID issued to you by Agora. Apply for a new App ID from
|
|
29
|
+
* Agora Dashboard if it is missing from your kit. See Get an App ID.
|
|
30
|
+
* @param appCertificate Certificate of the application that you registered in
|
|
31
|
+
* the Agora Dashboard. See Get an App Certificate.
|
|
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 expireTimestamp as 600(seconds).
|
|
34
|
+
* @return The chat App token.
|
|
35
|
+
*/
|
|
36
|
+
static buildAppToken(appId, appCertificate, expire) {
|
|
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
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
exports.ChatTokenBuilder = ChatTokenBuilder;
|
|
46
|
+
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* run this test with command:
|
|
3
|
+
* nodeunit ChatTokenBuilderTest.js
|
|
4
|
+
* see https://github.com/caolan/nodeunit
|
|
5
|
+
*/
|
|
6
|
+
const ChatTokenBuilder = require("../src/ChatTokenBuilder").ChatTokenBuilder;
|
|
7
|
+
const { AccessToken2, ServiceChat, kChatServiceType } = require("../src/AccessToken2");
|
|
8
|
+
|
|
9
|
+
const appId = "970CA35de60c44645bbae8a215061b33";
|
|
10
|
+
const appCertificate = "5CFd2fd1755d40ecb72977518be15d3b";
|
|
11
|
+
const userUuid = "2882341273";
|
|
12
|
+
const expire = 600;
|
|
13
|
+
|
|
14
|
+
exports.buildUserToken_Test = function (test) {
|
|
15
|
+
let token = ChatTokenBuilder.buildUserToken(appId, appCertificate, userUuid, expire);
|
|
16
|
+
let accessToken = new AccessToken2("", "", 0, 0);
|
|
17
|
+
accessToken.from_string(token);
|
|
18
|
+
test.equal(appId, accessToken.appId);
|
|
19
|
+
test.equal(expire, accessToken.expire);
|
|
20
|
+
test.equal(userUuid, accessToken.services[kChatServiceType].__user_id);
|
|
21
|
+
test.equal(expire, accessToken.services[kChatServiceType].__privileges[ServiceChat.kPrivilegeUser]);
|
|
22
|
+
test.done();
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
exports.buildAppToken_Test = function (test) {
|
|
26
|
+
let token = ChatTokenBuilder.buildAppToken(appId, appCertificate, expire);
|
|
27
|
+
let accessToken = new AccessToken2("", "", 0, 0);
|
|
28
|
+
accessToken.from_string(token);
|
|
29
|
+
test.equal(appId, accessToken.appId);
|
|
30
|
+
test.equal(expire, accessToken.expire);
|
|
31
|
+
test.equal(expire, accessToken.services[kChatServiceType].__privileges[ServiceChat.kPrivilegeApp]);
|
|
32
|
+
test.done();
|
|
33
|
+
}
|
|
34
|
+
|