agora-token 2.0.0
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 +36 -0
- package/index.d.ts +232 -0
- package/index.js +7 -0
- package/package.json +19 -0
- package/sample/AccessToken2Sample.js +18 -0
- package/sample/EducationTokenSample.js +21 -0
- package/sample/FpaTokenBuilderSample.js +7 -0
- package/sample/README.md +12 -0
- package/sample/RtcTokenBuilder2Sample.js +28 -0
- package/sample/RtcTokenBuilderSample.js +25 -0
- package/sample/RtmTokenBuilder2Sample.js +8 -0
- package/sample/RtmTokenBuilderSample.js +14 -0
- package/sample.js +46 -0
- package/server/DemoServer.js +66 -0
- package/server/README.md +26 -0
- package/server/package-lock.json +242 -0
- package/server/package.json +14 -0
- package/src/AccessToken.js +237 -0
- package/src/AccessToken2.js +414 -0
- package/src/DynamicKey5.js +173 -0
- package/src/EducationTokenBuilder.js +80 -0
- package/src/FpaTokenBuilder.js +24 -0
- package/src/RtcTokenBuilder.js +75 -0
- package/src/RtcTokenBuilder2.js +197 -0
- package/src/RtmTokenBuilder.js +31 -0
- package/src/RtmTokenBuilder2.js +28 -0
- package/src/SignalingToken.js +29 -0
- package/test/AccessToken2Test.js +110 -0
- package/test/AccessTokenTest.js +77 -0
- package/test/DynamicKeyTest.js +47 -0
- package/test/EducationTokenBuilderTest.js +45 -0
- package/test/FpaTokenBuilderTest.js +22 -0
- package/test/RtcTokenBuilder2Test.js +140 -0
- package/test/RtmTokenBuilder2Test.js +24 -0
- package/test/RtmTokenBuilderTest.js +24 -0
package/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# Authenticate Users with a token
|
|
2
|
+
|
|
3
|
+
To enhance communication security, Agora uses tokens to authenticate users before they access the Agora service, or joining an RTC channel.
|
|
4
|
+
|
|
5
|
+
## Code structure
|
|
6
|
+
|
|
7
|
+
Under the `nodejs` directory:
|
|
8
|
+
|
|
9
|
+
* `/src/` contains the source code for generating a token, where `RtcTokenBuilder.js` is used for generating an RTC token, and `RtmTokenBuilder.js` is used for generating an RTM token.
|
|
10
|
+
* `/sample/` contains the sample code for generating a token, where `RtcTokenBuilderSample.js` is used for generating an RTC token, and `RtmTokenBuilderSample.js` is used for generating an RTM token.
|
|
11
|
+
|
|
12
|
+
## Generate a token with the sample code
|
|
13
|
+
|
|
14
|
+
This section takes `RtcTokenBuilderSample.js` as an example to show how to generate a token with the sample code.
|
|
15
|
+
|
|
16
|
+
Before proceeding, ensure that you have installed the LTS version of Node.js.
|
|
17
|
+
|
|
18
|
+
1. Run the following command to install the Node.js dependencies:
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
npm install
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
2. Download or clone the [Tools](https://github.com/AgoraIO/Tools) repository.
|
|
25
|
+
|
|
26
|
+
3. Open the `DynamicKey/AgoraDynamicKey/nodejs/sample/RtcTokenBuilderSample.js` file, replace the value of `appID`, `appCertificate`, `channelName`, and `uid` with your own, and comment out the code snippets of `buildTokenWithUserAccount`.
|
|
27
|
+
|
|
28
|
+
4. Open your Terminal, navigate to the same directory that holds `RtcTokenBuilderSample.js`, and run the following command. The token is generated and printed in your Terminal window.
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
node RtcTokenBuilderSample.js
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Reference
|
|
35
|
+
|
|
36
|
+
For a complete authentication flow between the app server and app client, see [Authenticate Your Users with Tokens]().
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
|
|
2
|
+
export namespace RtcRole {
|
|
3
|
+
export const PUBLISHER: number;
|
|
4
|
+
export const SUBSCRIBER: number;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export namespace RtcTokenBuilder {
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Builds an RTC token using an Integer uid.
|
|
11
|
+
* @param {*} appId The App ID issued to you by Agora.
|
|
12
|
+
* @param {*} appCertificate Certificate of the application that you registered in the Agora Dashboard.
|
|
13
|
+
* @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:
|
|
14
|
+
* - The 26 lowercase English letters: a to z.
|
|
15
|
+
* - The 26 uppercase English letters: A to Z.
|
|
16
|
+
* - The 10 digits: 0 to 9.
|
|
17
|
+
* - The space.
|
|
18
|
+
* - "!", "#", "$", "%", "&", "(", ")", "+", "-", ":", ";", "<", "=", ".", ">", "?", "@", "[", "]", "^", "_", " {", "}", "|", "~", ",".
|
|
19
|
+
* @param {*} uid User ID. A 32-bit unsigned integer with a value ranging from 1 to (2^32-1).
|
|
20
|
+
* @param {*} role See #userRole.
|
|
21
|
+
* - 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 [Hosting-in](https://docs.agora.io/en/Agora%20Platform/terms?platform=All%20Platforms#hosting-in). In order for this role to take effect, please contact our support team to enable authentication for Hosting-in for you. Otherwise, Role_Subscriber still has the same privileges as Role_Publisher.
|
|
23
|
+
* @param {*} token_expire 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 token_expire as 600(seconds)
|
|
24
|
+
* @param {*} privilege_expire represented by the number of seconds elapsed since now. If, for example, you want to enable your privilege for 10 minutes, set privilege_expire as 600(seconds). * @return The new Token.
|
|
25
|
+
*/
|
|
26
|
+
export function buildTokenWithUid(appId: string, appCertificate: string, channelName: string, uid: string | number, role: number, token_expire: number, privilege_expire: number): string;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Builds an RTC token using an Integer uid.
|
|
30
|
+
* @param {*} appId The App ID issued to you by Agora.
|
|
31
|
+
* @param {*} appCertificate Certificate of the application that you registered in the Agora Dashboard.
|
|
32
|
+
* @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:
|
|
33
|
+
* - The 26 lowercase English letters: a to z.
|
|
34
|
+
* - The 26 uppercase English letters: A to Z.
|
|
35
|
+
* - The 10 digits: 0 to 9.
|
|
36
|
+
* - The space.
|
|
37
|
+
* - "!", "#", "$", "%", "&", "(", ")", "+", "-", ":", ";", "<", "=", ".", ">", "?", "@", "[", "]", "^", "_", " {", "}", "|", "~", ",".
|
|
38
|
+
* @param {*} account The user account.
|
|
39
|
+
* @param {*} role See #userRole.
|
|
40
|
+
* - 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 [Hosting-in](https://docs.agora.io/en/Agora%20Platform/terms?platform=All%20Platforms#hosting-in). In order for this role to take effect, please contact our support team to enable authentication for Hosting-in for you. Otherwise, Role_Subscriber still has the same privileges as Role_Publisher.
|
|
42
|
+
* @param {*} token_expire 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 token_expire as 600(seconds)
|
|
43
|
+
* @param {*} privilege_expire represented by the number of seconds elapsed since now. If, for example, you want to enable your privilege for 10 minutes, set privilege_expire as 600(seconds).
|
|
44
|
+
* @return The new Token.
|
|
45
|
+
*/
|
|
46
|
+
export function buildTokenWithUserAccount(appId: string, appCertificate: string, channelName: string, account: string | number, role: number, token_expire: number, privilege_expire: number): string;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Generates an RTC token with the specified privilege.
|
|
50
|
+
*
|
|
51
|
+
* This method supports generating a token with the following privileges:
|
|
52
|
+
* - Joining an RTC channel.
|
|
53
|
+
* - Publishing audio in an RTC channel.
|
|
54
|
+
* - Publishing video in an RTC channel.
|
|
55
|
+
* - Publishing data streams in an RTC channel.
|
|
56
|
+
*
|
|
57
|
+
* The privileges for publishing audio, video, and data streams in an RTC channel apply only if you have
|
|
58
|
+
* enabled co-host authentication.
|
|
59
|
+
*
|
|
60
|
+
* A user can have multiple privileges. Each privilege is valid for a maximum of 24 hours.
|
|
61
|
+
* The SDK triggers the onTokenPrivilegeWillExpire and onRequestToken callbacks when the token is about to expire
|
|
62
|
+
* or has expired. The callbacks do not report the specific privilege affected, and you need to maintain
|
|
63
|
+
* the respective timestamp for each privilege in your app logic. After receiving the callback, you need
|
|
64
|
+
* to generate a new token, and then call renewToken to pass the new token to the SDK, or call joinChannel to re-join
|
|
65
|
+
* the channel.
|
|
66
|
+
*
|
|
67
|
+
* @note
|
|
68
|
+
* Agora recommends setting a reasonable timestamp for each privilege according to your scenario.
|
|
69
|
+
* Suppose the expiration timestamp for joining the channel is set earlier than that for publishing audio.
|
|
70
|
+
* When the token for joining the channel expires, the user is immediately kicked off the RTC channel
|
|
71
|
+
* and cannot publish any audio stream, even though the timestamp for publishing audio has not expired.
|
|
72
|
+
*
|
|
73
|
+
* @param appId The App ID of your Agora project.
|
|
74
|
+
* @param appCertificate The App Certificate of your Agora project.
|
|
75
|
+
* @param channelName The unique channel name for the Agora RTC session in string format. The string length must be less than 64 bytes. The channel name may contain the following characters:
|
|
76
|
+
* - All lowercase English letters: a to z.
|
|
77
|
+
* - All uppercase English letters: A to Z.
|
|
78
|
+
* - All numeric characters: 0 to 9.
|
|
79
|
+
* - The space character.
|
|
80
|
+
* - "!", "#", "$", "%", "&", "(", ")", "+", "-", ":", ";", "<", "=", ".", ">", "?", "@", "[", "]", "^", "_", " {", "}", "|", "~", ",".
|
|
81
|
+
* @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
|
+
* @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 token_expire as 600(seconds).
|
|
84
|
+
* @param joinChannelPrivilegeExpire The Unix timestamp when the privilege for joining the channel expires, represented
|
|
85
|
+
* by the sum of the current timestamp plus the valid time period of the token. For example, if you set joinChannelPrivilegeExpire as the
|
|
86
|
+
* current timestamp plus 600 seconds, the token expires in 10 minutes.
|
|
87
|
+
* @param pubAudioPrivilegeExpire The Unix timestamp when the privilege for publishing audio expires, represented
|
|
88
|
+
* by the sum of the current timestamp plus the valid time period of the token. For example, if you set pubAudioPrivilegeExpire as the
|
|
89
|
+
* current timestamp plus 600 seconds, the token expires in 10 minutes. If you do not want to enable this privilege,
|
|
90
|
+
* set pubAudioPrivilegeExpire as the current Unix timestamp.
|
|
91
|
+
* @param pubVideoPrivilegeExpire The Unix timestamp when the privilege for publishing video expires, represented
|
|
92
|
+
* by the sum of the current timestamp plus the valid time period of the token. For example, if you set pubVideoPrivilegeExpire as the
|
|
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
|
|
100
|
+
*/
|
|
101
|
+
export function buildTokenWithUidAndPrivilege(appId: string, appCertificate: string, channelName: string, uid: string | number,
|
|
102
|
+
tokenExpire: number, joinChannelPrivilegeExpire: number, pubAudioPrivilegeExpire: number,
|
|
103
|
+
pubVideoPrivilegeExpire: number, pubDataStreamPrivilegeExpire: number): string;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Generates an RTC token with the specified privilege.
|
|
107
|
+
*
|
|
108
|
+
* This method supports generating a token with the following privileges:
|
|
109
|
+
* - Joining an RTC channel.
|
|
110
|
+
* - Publishing audio in an RTC channel.
|
|
111
|
+
* - Publishing video in an RTC channel.
|
|
112
|
+
* - Publishing data streams in an RTC channel.
|
|
113
|
+
*
|
|
114
|
+
* The privileges for publishing audio, video, and data streams in an RTC channel apply only if you have
|
|
115
|
+
* enabled co-host authentication.
|
|
116
|
+
*
|
|
117
|
+
* A user can have multiple privileges. Each privilege is valid for a maximum of 24 hours.
|
|
118
|
+
* The SDK triggers the onTokenPrivilegeWillExpire and onRequestToken callbacks when the token is about to expire
|
|
119
|
+
* or has expired. The callbacks do not report the specific privilege affected, and you need to maintain
|
|
120
|
+
* the respective timestamp for each privilege in your app logic. After receiving the callback, you need
|
|
121
|
+
* to generate a new token, and then call renewToken to pass the new token to the SDK, or call joinChannel to re-join
|
|
122
|
+
* the channel.
|
|
123
|
+
*
|
|
124
|
+
* @note
|
|
125
|
+
* Agora recommends setting a reasonable timestamp for each privilege according to your scenario.
|
|
126
|
+
* Suppose the expiration timestamp for joining the channel is set earlier than that for publishing audio.
|
|
127
|
+
* When the token for joining the channel expires, the user is immediately kicked off the RTC channel
|
|
128
|
+
* and cannot publish any audio stream, even though the timestamp for publishing audio has not expired.
|
|
129
|
+
*
|
|
130
|
+
* @param appId The App ID of your Agora project.
|
|
131
|
+
* @param appCertificate The App Certificate of your Agora project.
|
|
132
|
+
* @param channelName The unique channel name for the Agora RTC session in string format. The string length must be less than 64 bytes. The channel name may contain the following characters:
|
|
133
|
+
* - All lowercase English letters: a to z.
|
|
134
|
+
* - All uppercase English letters: A to Z.
|
|
135
|
+
* - All numeric characters: 0 to 9.
|
|
136
|
+
* - The space character.
|
|
137
|
+
* - "!", "#", "$", "%", "&", "(", ")", "+", "-", ":", ";", "<", "=", ".", ">", "?", "@", "[", "]", "^", "_", " {", "}", "|", "~", ",".
|
|
138
|
+
* @param userAccount The user account.
|
|
139
|
+
* @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 token_expire as 600(seconds).
|
|
141
|
+
* @param joinChannelPrivilegeExpire The Unix timestamp when the privilege for joining the channel expires, represented
|
|
142
|
+
* by the sum of the current timestamp plus the valid time period of the token. For example, if you set joinChannelPrivilegeExpire as the
|
|
143
|
+
* current timestamp plus 600 seconds, the token expires in 10 minutes.
|
|
144
|
+
* @param pubAudioPrivilegeExpire The Unix timestamp when the privilege for publishing audio expires, represented
|
|
145
|
+
* by the sum of the current timestamp plus the valid time period of the token. For example, if you set pubAudioPrivilegeExpire as the
|
|
146
|
+
* current timestamp plus 600 seconds, the token expires in 10 minutes. If you do not want to enable this privilege,
|
|
147
|
+
* set pubAudioPrivilegeExpire as the current Unix timestamp.
|
|
148
|
+
* @param pubVideoPrivilegeExpire The Unix timestamp when the privilege for publishing video expires, represented
|
|
149
|
+
* by the sum of the current timestamp plus the valid time period of the token. For example, if you set pubVideoPrivilegeExpire as the
|
|
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.
|
|
157
|
+
*/
|
|
158
|
+
export function BuildTokenWithUserAccountAndPrivilege(appId: string, appCertificate: string, channelName: string, account: string | number,
|
|
159
|
+
tokenExpire: number, joinChannelPrivilegeExpire: number, pubAudioPrivilegeExpire: number,
|
|
160
|
+
pubVideoPrivilegeExpire: number, pubDataStreamPrivilegeExpire: number): string;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export namespace RtmTokenBuilder {
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Build the RTM token.
|
|
167
|
+
*
|
|
168
|
+
* @param appId The App ID issued to you by Agora. Apply for a new App ID from
|
|
169
|
+
* Agora Dashboard if it is missing from your kit. See Get an App ID.
|
|
170
|
+
* @param appCertificate Certificate of the application that you registered in
|
|
171
|
+
* the Agora Dashboard. See Get an App Certificate.
|
|
172
|
+
* @param userId The user's account, max length is 64 Bytes.
|
|
173
|
+
* @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 expireTimestamp as 600(seconds).
|
|
175
|
+
* @return The RTM token.
|
|
176
|
+
*/
|
|
177
|
+
export function buildToken(appId: string, appCertificate: string, userId: string | number, expire: number): string;
|
|
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
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
RtcTokenBuilder: require('./src/RtcTokenBuilder2').RtcTokenBuilder,
|
|
3
|
+
RtcRole: require('./src/RtcTokenBuilder2').Role,
|
|
4
|
+
RtmTokenBuilder: require('./src/RtmTokenBuilder2').RtmTokenBuilder,
|
|
5
|
+
EducationTokenBuilder: require('./src/EducationTokenBuilder').EducationTokenBuilder,
|
|
6
|
+
FpaTokenBuilder: require('./src/FpaTokenBuilder').FpaTokenBuilder
|
|
7
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "agora-token",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"homepage": "https://github.com/AgoraIO/Tools/tree/master/DynamicKey/AgoraDynamicKey/nodejs",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
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
|
+
"nodeunit": "^0.11.3"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const {AccessToken2, ServiceRtc} = require('../src/AccessToken2')
|
|
2
|
+
const RtcRole = require('../src/RtcTokenBuilder').Role;
|
|
3
|
+
|
|
4
|
+
const appID = '970CA35de60c44645bbae8a215061b33';
|
|
5
|
+
const appCertificate = '5CFd2fd1755d40ecb72977518be15d3b';
|
|
6
|
+
const channelName = '7d72365eb983485397e3e3f9d460bdda';
|
|
7
|
+
const uid = 2882341273;
|
|
8
|
+
const account = "2882341273";
|
|
9
|
+
const role = RtcRole.PUBLISHER;
|
|
10
|
+
|
|
11
|
+
const expirationTimeInSeconds = 3600
|
|
12
|
+
|
|
13
|
+
const currentTimestamp = Math.floor(Date.now() / 1000)
|
|
14
|
+
|
|
15
|
+
let token = new AccessToken2(appID, appCertificate, currentTimestamp, expirationTimeInSeconds)
|
|
16
|
+
let rtc_service = new ServiceRtc(channelName, uid)
|
|
17
|
+
token.add_service(rtc_service)
|
|
18
|
+
console.log(token.build())
|
|
@@ -0,0 +1,21 @@
|
|
|
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
|
|
14
|
+
|
|
15
|
+
const tokenA = EduTokenBuilder.buildRoomUserToken(appId, appCertificate, roomUuid, userUuid, role, expire)
|
|
16
|
+
console.log("Build room user token: " + tokenA);
|
|
17
|
+
const tokenB = EduTokenBuilder.buildUserToken(appId, appCertificate, userUuid, expire)
|
|
18
|
+
console.log("Build user token " + tokenB);
|
|
19
|
+
const tokenC = EduTokenBuilder.buildAppToken(appId, appCertificate, expire)
|
|
20
|
+
console.log("Build app token: " + tokenC);
|
|
21
|
+
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
const FpaTokenBuilder = require('../src/FpaTokenBuilder').FpaTokenBuilder
|
|
2
|
+
|
|
3
|
+
const appID = '970CA35de60c44645bbae8a215061b33'
|
|
4
|
+
const appCertificate = '5CFd2fd1755d40ecb72977518be15d3b'
|
|
5
|
+
|
|
6
|
+
let token = FpaTokenBuilder.buildToken(appID, appCertificate)
|
|
7
|
+
console.log("Token with FPA service: " + token)
|
package/sample/README.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
- **RtcTokenBuilder.js**: Source code for generating a token for the following SDKs:
|
|
2
|
+
- Agora Native SDK v2.1+
|
|
3
|
+
|
|
4
|
+
- Agora Web SDK v2.4+
|
|
5
|
+
|
|
6
|
+
- Agora Recording SDK v2.1+
|
|
7
|
+
|
|
8
|
+
- Agora RTSA SDK
|
|
9
|
+
|
|
10
|
+
> The Agora RTSA SDK supports joining multiple channels. If you join multiple channels at the same time, then you MUST generate a specific token for each channel you join.
|
|
11
|
+
- **RtmTokenBuilder.js**: Source code for generating a token for the Agora RTM SDK.
|
|
12
|
+
- **AccessToken.js**: Implements all the underlying algorithms for generating a token. Both **RtcTokenBuilder.js** and **RtmTokenBuilder.js** are a wrapper of **AccessToken.js** and have much easier-to-use APIs. We recommend using **RtcTokenBuilder.js** for generating an RTC token or **RtmTokenBuilder.js** for an RTM token.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const RtcTokenBuilder = require('../src/RtcTokenBuilder2').RtcTokenBuilder
|
|
2
|
+
const RtcRole = require('../src/RtcTokenBuilder2').Role
|
|
3
|
+
|
|
4
|
+
const appID = '970CA35de60c44645bbae8a215061b33'
|
|
5
|
+
const appCertificate = '5CFd2fd1755d40ecb72977518be15d3b'
|
|
6
|
+
const channelName = '7d72365eb983485397e3e3f9d460bdda'
|
|
7
|
+
const uid = 2882341273
|
|
8
|
+
const account = "2882341273"
|
|
9
|
+
const role = RtcRole.PUBLISHER
|
|
10
|
+
const expirationInSeconds = 3600
|
|
11
|
+
|
|
12
|
+
const tokenExpirationInSecond = 3600
|
|
13
|
+
const privilegeExpirationInSecond = 3600
|
|
14
|
+
// Build token with uid
|
|
15
|
+
const tokenA = RtcTokenBuilder.buildTokenWithUid(appID, appCertificate, channelName, uid, role, tokenExpirationInSecond, privilegeExpirationInSecond)
|
|
16
|
+
console.log("Token with int uid: " + tokenA)
|
|
17
|
+
|
|
18
|
+
// Build token with user account
|
|
19
|
+
const tokenB = RtcTokenBuilder.buildTokenWithUserAccount(appID, appCertificate, channelName, account, role, tokenExpirationInSecond, privilegeExpirationInSecond)
|
|
20
|
+
console.log("Token with user account: " + tokenB)
|
|
21
|
+
|
|
22
|
+
const tokenC = RtcTokenBuilder.buildTokenWithUidAndPrivilege(appID, appCertificate, channelName, uid,
|
|
23
|
+
expirationInSeconds, expirationInSeconds, expirationInSeconds, expirationInSeconds, expirationInSeconds)
|
|
24
|
+
console.log("Token with int uid and privilege:" + tokenC)
|
|
25
|
+
|
|
26
|
+
const tokenD = RtcTokenBuilder.BuildTokenWithUserAccountAndPrivilege(appID, appCertificate, channelName, account,
|
|
27
|
+
expirationInSeconds, expirationInSeconds, expirationInSeconds, expirationInSeconds, expirationInSeconds)
|
|
28
|
+
console.log("Token with user account and privilege:" + tokenD)
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const RtcTokenBuilder = require('../src/RtcTokenBuilder').RtcTokenBuilder;
|
|
2
|
+
const RtcRole = require('../src/RtcTokenBuilder').Role;
|
|
3
|
+
|
|
4
|
+
const appID = '970CA35de60c44645bbae8a215061b33';
|
|
5
|
+
const appCertificate = '5CFd2fd1755d40ecb72977518be15d3b';
|
|
6
|
+
const channelName = '7d72365eb983485397e3e3f9d460bdda';
|
|
7
|
+
const uid = 2882341273;
|
|
8
|
+
const account = "2882341273";
|
|
9
|
+
const role = RtcRole.PUBLISHER;
|
|
10
|
+
|
|
11
|
+
const expirationTimeInSeconds = 3600
|
|
12
|
+
|
|
13
|
+
const currentTimestamp = Math.floor(Date.now() / 1000)
|
|
14
|
+
|
|
15
|
+
const privilegeExpiredTs = currentTimestamp + expirationTimeInSeconds
|
|
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.
|
|
18
|
+
|
|
19
|
+
// Build token with uid
|
|
20
|
+
const tokenA = RtcTokenBuilder.buildTokenWithUid(appID, appCertificate, channelName, uid, role, privilegeExpiredTs);
|
|
21
|
+
console.log("Token With Integer Number Uid: " + tokenA);
|
|
22
|
+
|
|
23
|
+
// Build token with user account
|
|
24
|
+
const tokenB = RtcTokenBuilder.buildTokenWithAccount(appID, appCertificate, channelName, account, role, privilegeExpiredTs);
|
|
25
|
+
console.log("Token With UserAccount: " + tokenB);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
const RtmTokenBuilder = require("../src/RtmTokenBuilder2").RtmTokenBuilder;
|
|
2
|
+
const appId = "970CA35de60c44645bbae8a215061b33";
|
|
3
|
+
const appCertificate = "5CFd2fd1755d40ecb72977518be15d3b";
|
|
4
|
+
const userId = "test_user_id";
|
|
5
|
+
const expirationInSeconds = 3600;
|
|
6
|
+
|
|
7
|
+
const token = RtmTokenBuilder.buildToken(appId, appCertificate, userId, expirationInSeconds);
|
|
8
|
+
console.log("Rtm Token: " + token);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const RtmTokenBuilder = require('../src/RtmTokenBuilder').RtmTokenBuilder;
|
|
2
|
+
const RtmRole = require('../src/RtmTokenBuilder').Role;
|
|
3
|
+
const Priviledges = require('../src/AccessToken').priviledges;
|
|
4
|
+
const appID = "970CA35de60c44645bbae8a215061b33";
|
|
5
|
+
const appCertificate = "5CFd2fd1755d40ecb72977518be15d3b";
|
|
6
|
+
const account = "test_user_id";
|
|
7
|
+
|
|
8
|
+
const expirationTimeInSeconds = 3600
|
|
9
|
+
const currentTimestamp = Math.floor(Date.now() / 1000)
|
|
10
|
+
|
|
11
|
+
const privilegeExpiredTs = currentTimestamp + expirationTimeInSeconds
|
|
12
|
+
|
|
13
|
+
const token = RtmTokenBuilder.buildToken(appID, appCertificate, account, RtmRole, privilegeExpiredTs);
|
|
14
|
+
console.log("Rtm Token: " + token);
|
package/sample.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
const {RtcTokenBuilder, RtmTokenBuilder, RtcRole, RtmRole} = require('./index')
|
|
2
|
+
|
|
3
|
+
const generateRtcToken = () => {
|
|
4
|
+
// Rtc Examples
|
|
5
|
+
const appID = '970CA35de60c44645bbae8a215061b33';
|
|
6
|
+
const appCertificate = '5CFd2fd1755d40ecb72977518be15d3b';
|
|
7
|
+
const channelName = '7d72365eb983485397e3e3f9d460bdda';
|
|
8
|
+
const uid = 2882341273;
|
|
9
|
+
const account = "2882341273";
|
|
10
|
+
const role = RtcRole.PUBLISHER;
|
|
11
|
+
|
|
12
|
+
const expirationTimeInSeconds = 3600
|
|
13
|
+
|
|
14
|
+
const currentTimestamp = Math.floor(Date.now() / 1000)
|
|
15
|
+
|
|
16
|
+
const privilegeExpiredTs = currentTimestamp + expirationTimeInSeconds
|
|
17
|
+
|
|
18
|
+
// IMPORTANT! Build token with either the uid or with the user account. Comment out the option you do not want to use below.
|
|
19
|
+
|
|
20
|
+
// Build token with uid
|
|
21
|
+
const tokenA = RtcTokenBuilder.buildTokenWithUid(appID, appCertificate, channelName, uid, role, privilegeExpiredTs);
|
|
22
|
+
console.log("Token With Integer Number Uid: " + tokenA);
|
|
23
|
+
|
|
24
|
+
// Build token with user account
|
|
25
|
+
const tokenB = RtcTokenBuilder.buildTokenWithAccount(appID, appCertificate, channelName, account, role, privilegeExpiredTs);
|
|
26
|
+
console.log("Token With UserAccount: " + tokenB);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
const generateRtmToken = () => {
|
|
31
|
+
// Rtm Examples
|
|
32
|
+
const appID = "970CA35de60c44645bbae8a215061b33";
|
|
33
|
+
const appCertificate = "5CFd2fd1755d40ecb72977518be15d3b";
|
|
34
|
+
const account = "test_user_id";
|
|
35
|
+
|
|
36
|
+
const expirationTimeInSeconds = 3600
|
|
37
|
+
const currentTimestamp = Math.floor(Date.now() / 1000)
|
|
38
|
+
|
|
39
|
+
const privilegeExpiredTs = currentTimestamp + expirationTimeInSeconds
|
|
40
|
+
|
|
41
|
+
const token = RtmTokenBuilder.buildToken(appID, appCertificate, account, RtmRole, privilegeExpiredTs);
|
|
42
|
+
console.log("Rtm Token: " + token);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
generateRtcToken()
|
|
46
|
+
generateRtmToken()
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
//var fs = require('fs');
|
|
2
|
+
//var https = require('https');
|
|
3
|
+
var http = require('http');
|
|
4
|
+
var express = require('express');
|
|
5
|
+
var {RtcTokenBuilder, RtmTokenBuilder, RtcRole, RtmRole} = require('agora-access-token')
|
|
6
|
+
|
|
7
|
+
var PORT = 8080;
|
|
8
|
+
|
|
9
|
+
// Fill the appID and appCertificate key given by Agora.io
|
|
10
|
+
var appID = "<YOUR APP ID>";
|
|
11
|
+
var appCertificate = "<YOUR APP CERTIFICATE>";
|
|
12
|
+
|
|
13
|
+
// token expire time, hardcode to 3600 seconds = 1 hour
|
|
14
|
+
var expirationTimeInSeconds = 3600
|
|
15
|
+
var role = RtcRole.PUBLISHER
|
|
16
|
+
|
|
17
|
+
var app = express();
|
|
18
|
+
app.disable('x-powered-by');
|
|
19
|
+
app.set('port', PORT);
|
|
20
|
+
app.use(express.favicon());
|
|
21
|
+
app.use(app.router);
|
|
22
|
+
|
|
23
|
+
var generateRtcToken = function(req, resp) {
|
|
24
|
+
var currentTimestamp = Math.floor(Date.now() / 1000)
|
|
25
|
+
var privilegeExpiredTs = currentTimestamp + expirationTimeInSeconds
|
|
26
|
+
var channelName = req.query.channelName;
|
|
27
|
+
// use 0 if uid is not specified
|
|
28
|
+
var uid = req.query.uid || 0
|
|
29
|
+
if (!channelName) {
|
|
30
|
+
return resp.status(400).json({ 'error': 'channel name is required' }).send();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
var key = RtcTokenBuilder.buildTokenWithUid(appID, appCertificate, channelName, uid, role, privilegeExpiredTs);
|
|
35
|
+
|
|
36
|
+
resp.header("Access-Control-Allow-Origin", "*")
|
|
37
|
+
//resp.header("Access-Control-Allow-Origin", "http://ip:port")
|
|
38
|
+
return resp.json({ 'key': key }).send();
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
var generateRtmToken = function(req, resp) {
|
|
42
|
+
var currentTimestamp = Math.floor(Date.now() / 1000)
|
|
43
|
+
var privilegeExpiredTs = currentTimestamp + expirationTimeInSeconds
|
|
44
|
+
var account = req.query.account;
|
|
45
|
+
if (!account) {
|
|
46
|
+
return resp.status(400).json({ 'error': 'account is required' }).send();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
var key = RtmTokenBuilder.buildToken(appID, appCertificate, account, RtmRole, privilegeExpiredTs);
|
|
50
|
+
|
|
51
|
+
resp.header("Access-Control-Allow-Origin", "*")
|
|
52
|
+
//resp.header("Access-Control-Allow-Origin", "http://ip:port")
|
|
53
|
+
return resp.json({ 'key': key }).send();
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
app.get('/rtcToken', generateRtcToken);
|
|
57
|
+
app.get('/rtmToken', generateRtmToken);
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
http.createServer(app).listen(app.get('port'), function() {
|
|
61
|
+
console.log('AgoraSignServer starts at ' + app.get('port'));
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
//https.createServer(credentials, app).listen(app.get('port') + 1, function() {
|
|
65
|
+
// console.log('AgoraSignServer starts at ' + (app.get('port') + 1));
|
|
66
|
+
//});
|
package/server/README.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# How to use
|
|
2
|
+
## Fill in your vendor information
|
|
3
|
+
Open *DemoServer.js* and replace <YOUR APP ID> and <YOUR APP CERTIFICATE> with your value
|
|
4
|
+
```
|
|
5
|
+
// Fill the appID and appCertificate key given by Agora.io
|
|
6
|
+
var appID = "<YOUR APP ID>";
|
|
7
|
+
var appCertificate = "<YOUR APP CERTIFICATE>";
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
## Install Dependencies
|
|
11
|
+
|
|
12
|
+
```shell
|
|
13
|
+
npm i
|
|
14
|
+
node DemoServer.js
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Generate Token
|
|
18
|
+
### Generate RTC Token
|
|
19
|
+
```shell
|
|
20
|
+
curl http://localhost:8080/rtcToken?channelName=test
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### Generate RTM Token
|
|
24
|
+
```shell
|
|
25
|
+
curl http://localhost:8080/rtmToken?account=testAccount
|
|
26
|
+
```
|