To enhance communication security, Agora supports two authentication mechanisms to verify users before they access Agora services: by App ID or by token.
Authentication | Description | Scenario |
---|---|---|
App ID | All clients in a channel use App IDs for authentication. | Scenarios with low security requirements. |
Token | All clients in a channel use tokens for authentication. | Scenarios with high security requirements. |
Before accessing Agora services, you need to initialize the service by providing an App ID.
After signing up for a developer account, you can access Agora Console, where you can create multiple projects. Each project has an App ID, which is the unique identifier of the project. If others steal your App ID, they can use it in their own projects. Therefore, using an App ID for authentication is less secure. Agora recommends using an App ID for authentication only in a test environment or if your project has low security requirements.
To get an App ID, do the following:
Sign up for a developer account at Agora Console. See Sign up for an Agora account.
Click in the left navigation menu to enter the Project Management page.
Click Create.
Enter your project name, choose the App ID authentication mechanism in the dialog box, and click Submit.
When the project is created successfully, you can see the newly created project in the project list. Click to view and copy the App ID.
Token is a dynamic key generated by App ID, App Certificate, user ID, channel name, token expiration timestamp, and other information. For scenarios requiring high security, such as the production environment, Agora recommends using a token for authentication.
The following table lists Agora products which support token-based authentication:
Product | Version | Method to determine version |
---|---|---|
Voice or Video SDK (Native) | v2.1.0 or later | getSdkVersion |
Voice or Video SDK (Web) | v2.4.0 or later | AgoraRTC.VERSION |
Third-party Framework SDK | All versions | / |
On-premise Recording SDK | v2.1.0 or later | / |
Cloud Recording | All versions | / |
Agora RTM SDK | All versions | / |
See Get an App ID.
An App Certificate is a string generated from Agora Console that enables token authentication. For different security requirements, Agora provides two types of app certificates:
If you enable an app certificate for the first time, you need to enable a primary certificate first.
To enable a primary certificate, do the following:
If you choose the APP ID + APP Certificate + Token authentication mechanism when creating a project, Agora enables the primary certificate for you by default. On the Edit Project page, you can click to view and copy the primary certificate.
If you choose the APP ID authentication mechanism when creating a project, you need to enable the primary certificate manually. On the Edit Project page, find Primary certificate and click Enable. Once the primary certificate is enabled, you can click to view and copy the primary certificate.
Note: A temporary token does not apply to the Agora RTM SDK.
Tokens are generated on your app server. Agora provides an open-source AgoraDynamicKey repository in GitHub, which enables you to generate tokens on your server with programming languages such as C++, Java, Python, PHP, Ruby, Node.js, and Go. For detailed descriptions on the repository and how to generate a token with the sample code, see Generate a Token.
This section takes the C++ API as an example to describe the parameters for generating a token:
static std::string buildTokenWithUid(
const std::string& appId,
const std::string& appCertificate,
const std::string& channelName,
uint32_t uid,
UserRole role,
uint32_t privilegeExpiredTs = 0);
Parameter | Description |
---|---|
appId |
The App ID of your Agora project. |
appCertificate |
The App Certificate of your Agora project. |
channelName |
The unique channel name for the Agora RTC session in the string format. The string length must be less than 64 bytes. Supported character scopes are:
|
uid |
The user ID. A 32-bit unsigned integer with a value range from 1 to (232 - 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 or log onto the service system. |
role |
The privilege of the user:
|
privilegeExpiredTs |
The Unix timestamp (s) when the token expires, represented by the sum of the current timestamp and the valid time of the token. For example, if you set privilegeExpiredTs as the current timestamp plus 600 seconds, the token expires in 10 minutes. A token is valid for 24 hours at most. If you set this parameter as 0 or a period longer than 24 hours, the token is valid for 24 hours. |
After generating a token, see the following steps to use the token:
To meet the requirements for higher security, Agora is phasing out authenticating with an App ID. For projects that currently use App ID to authenticate users, Agora recommends the following steps to upgrade all your projects to token-based authentication:
Enable the App Certificate in Console. Once the App Certificate is enabled, the project supports authenticating users with either an App ID or a token. This enables the app to ungrade to token-based authentication without any impact on current users.
Refer to Generate a Token to deploy a token server.
Modify the authentication logic of the app client. Ensure that you fill the token
parameter in joinChannel
or login
with the token generated from your server.
You app client is now ready for a gray release.
When all app clients have upgraded to token-based authentication, delete No Certificate in Agora Console. This prevents users from joining a channel with only an App ID.
onTokenPrivilegeWillExpire
and onRequestToken
callbacks to notify the app when the token will expire or has expired. You need to generate a new token on the server, and then call renewToken
to pass the new Token to the SDK or joinChannel
to re-join the channel.The APIs mentioned in this article are in C++. Use this table to look up the API names and their functions in other programming languages:
C++ | Java | Objective-C | JavaScript | |
---|---|---|---|---|
Joins a channel | joinChannel |
joinChannel |
joinChannelByToken |
join |
The token-will-expire callback | onTokenPrivilegeWillExpire |
onTokenPrivilegeWillExpire |
tokenPrivilegWillExpire |
client.on("token-privilege-will-expire") |
The token-has-expired callback | onRequestToken |
onRequestToken |
rtcEngineRequestToken |
client.on("token-privilege-did-expire") |
Renews the token | renewToken |
renewToken |
renewToken |
renewToken |