Skip to content

Commit 42a9cad

Browse files
Merge pull request #419 from MohamedAbd0/master
Adding Support Agora Token Generator to Dart
2 parents c381f05 + 8c03990 commit 42a9cad

12 files changed

Lines changed: 592 additions & 0 deletions
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Miscellaneous
2+
*.class
3+
*.log
4+
*.pyc
5+
*.swp
6+
.DS_Store
7+
.atom/
8+
.buildlog/
9+
.history
10+
.svn/
11+
migrate_working_dir/
12+
13+
# IntelliJ related
14+
*.iml
15+
*.ipr
16+
*.iws
17+
.idea/
18+
19+
# The .vscode folder contains launch configuration and tasks you configure in
20+
# VS Code which you may wish to be included in version control, so this line
21+
# is commented out by default.
22+
#.vscode/
23+
24+
# Flutter/Dart/Pub related
25+
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
26+
/pubspec.lock
27+
**/doc/api/
28+
.dart_tool/
29+
.flutter-plugins
30+
.flutter-plugins-dependencies
31+
build/
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## 0.0.1
2+
3+
- Initial release of Agora Token Generator for Flutter/Dart
4+
- Support for generating RTC tokens with user ID or account
5+
- Support for generating RTM tokens
6+
- Implementation of AccessToken 2.0 (007 prefix format)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Agora Token Generator
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Agora Token Generator
2+
3+
A Flutter package for generating Agora Dynamic Keys and Access Tokens to use with Agora.io SDKs. This package provides a pure Dart implementation of Agora's token generation mechanism.
4+
5+
## Features
6+
7+
- Generate RTC tokens for Agora Real-Time Communications
8+
- Generate RTM tokens for Agora Real-Time Messaging
9+
- Support for the latest token format (AccessToken2 - 007)
10+
- No native dependencies
11+
12+
## Installation
13+
14+
Add this to your package's `pubspec.yaml` file:
15+
16+
```yaml
17+
dependencies:
18+
agora_token_generator: ^0.0.1
19+
```
20+
21+
Then run:
22+
23+
```bash
24+
flutter pub get
25+
```
26+
27+
## Usage
28+
29+
### RTC Token
30+
31+
Generate a token for Agora RTC service with a numeric user ID:
32+
33+
```dart
34+
import 'package:agora_token_generator/agora_token_generator.dart';
35+
36+
void main() {
37+
String appId = 'YOUR_APP_ID';
38+
String appCertificate = 'YOUR_APP_CERTIFICATE';
39+
String channelName = 'test_channel';
40+
int uid = 12345;
41+
int tokenExpireSeconds = 3600; // Token expires in 1 hour
42+
43+
String token = RtcTokenBuilder.buildTokenWithUid(
44+
appId: appId,
45+
appCertificate: appCertificate,
46+
channelName: channelName,
47+
uid: uid,
48+
tokenExpireSeconds: tokenExpireSeconds,
49+
);
50+
51+
print('RTC Token: $token');
52+
}
53+
```
54+
55+
Generate a token for Agora RTC service with a string user account:
56+
57+
```dart
58+
import 'package:agora_token_generator/agora_token_generator.dart';
59+
60+
void main() {
61+
String appId = 'YOUR_APP_ID';
62+
String appCertificate = 'YOUR_APP_CERTIFICATE';
63+
String channelName = 'test_channel';
64+
String userAccount = 'user123';
65+
int tokenExpireSeconds = 3600; // Token expires in 1 hour
66+
67+
String token = RtcTokenBuilder.buildTokenWithAccount(
68+
appId: appId,
69+
appCertificate: appCertificate,
70+
channelName: channelName,
71+
account: userAccount,
72+
tokenExpireSeconds: tokenExpireSeconds,
73+
);
74+
75+
print('RTC Token with User Account: $token');
76+
}
77+
```
78+
79+
### RTM Token
80+
81+
Generate a token for Agora RTM service:
82+
83+
```dart
84+
import 'package:agora_token_generator/agora_token_generator.dart';
85+
86+
void main() {
87+
String appId = 'YOUR_APP_ID';
88+
String appCertificate = 'YOUR_APP_CERTIFICATE';
89+
String userId = 'user123';
90+
int tokenExpireSeconds = 3600; // Token expires in 1 hour
91+
92+
String token = RtmTokenBuilder.buildToken(
93+
appId: appId,
94+
appCertificate: appCertificate,
95+
userId: userId,
96+
tokenExpireSeconds: tokenExpireSeconds,
97+
);
98+
99+
print('RTM Token: $token');
100+
}
101+
```
102+
103+
## Getting Agora App ID and App Certificate
104+
105+
To use this token generator, you need to get an App ID and App Certificate from Agora:
106+
107+
1. Create an account at [https://console.agora.io/](https://console.agora.io/)
108+
2. Create a new project
109+
3. Get the App ID from the project dashboard
110+
4. Generate an App Certificate for your project (Keep it safe and don't share it!)
111+
112+
## About Agora Token Authentication
113+
114+
Agora uses token-based authentication to secure connections to their services. This package implements the latest token format (AccessToken2 - 007).
115+
116+
## License
117+
118+
This project is licensed under the MIT License - see the LICENSE file for details.
119+
120+
## Acknowledgments
121+
122+
This Flutter/Dart implementation is based on Agora's official authentication mechanism as demonstrated in their SDKs for other languages.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
include: package:flutter_lints/flutter.yaml
2+
3+
# Additional information about this file can be found at
4+
# https://dart.dev/guides/language/analysis-options
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import 'package:agora_token_generator/agora_token_generator.dart';
2+
3+
void main() {
4+
// Example values
5+
String appId = 'your-app-id';
6+
String appCertificate = 'your-app-certificate';
7+
String channelName = 'test-channel';
8+
int uid = 12345;
9+
String userAccount = 'test-user';
10+
int tokenExpireSeconds = 3600; // 1 hour
11+
12+
// Generate RTC token with numeric UID
13+
String rtcToken = RtcTokenBuilder.buildTokenWithUid(
14+
appId: appId,
15+
appCertificate: appCertificate,
16+
channelName: channelName,
17+
uid: uid,
18+
tokenExpireSeconds: tokenExpireSeconds,
19+
);
20+
print('RTC Token with numeric UID: $rtcToken');
21+
22+
// Generate RTC token with user account
23+
String rtcTokenWithAccount = RtcTokenBuilder.buildTokenWithAccount(
24+
appId: appId,
25+
appCertificate: appCertificate,
26+
channelName: channelName,
27+
account: userAccount,
28+
tokenExpireSeconds: tokenExpireSeconds,
29+
);
30+
print('RTC Token with user account: $rtcTokenWithAccount');
31+
32+
// Generate RTM token
33+
String rtmToken = RtmTokenBuilder.buildToken(
34+
appId: appId,
35+
appCertificate: appCertificate,
36+
userId: userAccount,
37+
tokenExpireSeconds: tokenExpireSeconds,
38+
);
39+
print('RTM Token: $rtmToken');
40+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
library agora_token_generator;
2+
3+
export 'src/access_token.dart';
4+
export 'src/rtc_token_builder.dart';
5+
export 'src/rtm_token_builder.dart';

0 commit comments

Comments
 (0)