Authenticate Your Users with Tokens
Authentication is the act of validating the identity of each user before they access your system. Agora uses digital tokens to authenticate users and their privileges before they access an Agora service, such as joining an Agora call, or logging into the real-time messaging system.
To enhance its authentication and security services, Agora provides a new version of token called AccessToken2 as of August 18, 2022. For how to upgrade from AccessToken to AccessToken2, see Upgrade from AccessToken to AccessToken2.
This page shows you how to create a token server and a client app for AccessToken2. The client app retrieves a token from the token server. This token authenticates the current user when the user accesses the Agora service.
Understand the tech
The following figure shows the steps in the authentication flow:
A token is a dynamic key generated on your app server that is valid for a maximum of 24 hours. When your users connect to an Agora channel from your app client, Agora Platform validates the token and reads the user and project information stored in the token. A token contains the following information:
- The App ID of your Agora project
- The channel name
- The user ID of the user to be authenticated
- The privilege of the user, either as a publisher or a subscriber
- The time after which the token expires
Prerequisites
In order to follow this procedure you must have the following:
- A valid Agora account.
- An Agora project with the App Certificate enabled.
- Golang 1.14+ with GO111MODULE set to on.
If you are using Go 1.16+, GO111MODULE is on by default. See this blog for details.
- npm and a supported browser.
Implement the authentication flow
This section shows you how to supply and consume a token that gives rights to specific functionality to authenticated users using the source code provided by Agora.
Get the App ID and App Certificate
This section shows you how to get the security information needed to generate a token, including the App ID and App Certificate of your project.
1. Get the App ID
Agora automatically assigns each project an App ID as a unique identifier.
To copy this App ID, find your project on the Project Management page in Agora Console, and click the copy icon in the App ID column.
2. Get the App Certificate
To get an App Certificate, do the following:
-
On the Project Management page, click Config for the project you want to use.
-
Click the copy icon under Primary Certificate.
Deploy a token server
Token generators create the tokens requested by your client app to enable secure access to Agora security infrastructure. To serve these tokens you deploy a generator in your security infrastructure.
In order to show the authentication workflow, this section shows how to build and run a token server written in Golang on your local machine.
This sample server uses BuildTokenWithUid
[1/2].
- Create a file,
server.go
, with the following content. Then replace<Your App ID>
and<Your App Certificate>
with your App ID and App Certificate.
-
A
go.mod
file defines this module’s import path and dependency requirements. To create thego.mod
for your token server, run the following command: -
Get dependencies by running the following command. You can use a Go mirror origin such as https://goproxy.cn/ to speed up the process.
-
Start the server by running the following command:
Use AccessToken2 for client-side user authentication
This section uses the Web client as an example to show how to use a token for client-side user authentication.
-
Create the project structure of the Web client with a folder including the following files.
index.html
: User interfaceclient.js
: App logic with Agora RTC Web SDK v4.x (Must be v4.8.0 or higher)
-
In
index.html
, add the following code to include the app logic in the UI: -
Create the app logic by editing
client.js
with the following content:
- Replace
<Your App ID>
with your App ID. The App ID must match the one in the server. - Replace
<Your Host URL and port>
with the host URL and port of the local Golang server you have just deployed, such as10.53.3.234:8082
.
In the code example, you can see that token is related to the following code logic in the client:
- Call
join
to join the channel with token, user ID, and channel name. The user ID and channel name must be the same as the ones used to generate the token. - The
token-privilege-will-expire
callback occurs 30 seconds before the privilege expires. When thetoken-privilege-will-expire
callback is triggered, the client must fetch the token from the server and callrenewToken
to pass the new token to the SDK. - The
token-privilege-did-expire
callback occurs when the privilege expires. When thetoken-privilege-did-expire
callback is triggered, the client must fetch the token from the server and calljoin
to use the new token to join the channel.
- Open
index.html
with a supported browser to perform the following actions:- Successfully joining a channel.
- Renewing a token every 10 seconds.
Reference
This section introduces token generator libraries, version requirements, and related documents about AccessToken2.
SDK compatibility for AccessToken2
AccessToken2 supports the following versions of the Agora RTC SDK (excluding the client-side Media Push feature):
SDK | SDK Version to Support AccessToken2 |
---|---|
RTC Native SDK | >= 3.6.0 |
RTC Electron SDK | >= 3.6.0 |
RTC Unity SDK | >= 3.6.0 |
RTC React Native SDK | >= 3.6.0 |
RTC Flutter SDK | >= 5.10 |
RTC Web SDK | >= 4.8.0 |
RTC SDKs that use AccessToken2 can interoperate with RTC SDKs that use AccessToken. RTC SDKs that support AccessToken2 also support AccessToken.
AccessToken2 generator libraries
Agora provides an open-source AgoraDynamicKey repository on GitHub, which enables you to generate tokens on your server with programming languages such as C++, Java, and Go.
Language | Algorithm | Core method | Sample code |
---|---|---|---|
C++ | HMAC-SHA256 | buildTokenWithUid | RtcTokenBuilderSample.cpp |
Go | HMAC-SHA256 | buildTokenWithUid | sample.go |
Java | HMAC-SHA256 | buildTokenWithUid | RtcTokenBuilderSample.java |
Node.js | HMAC-SHA256 | buildTokenWithUid | RtcTokenBuilderSample.js |
PHP | HMAC-SHA256 | buildTokenWithUid | RtcTokenBuilderSample.php |
Python | HMAC-SHA256 | buildTokenWithUid | RtcTokenBuilderSample.py |
Python3 | HMAC-SHA256 | buildTokenWithUid | RtcTokenBuilderSample.py |
BuildTokenWithUid API Reference
This section introduces the core method for generating AccessToken2: BuildTokenWithUid
. The AccessToken2 generator libraries provide two BuildTokenWithUid
methods:
BuildTokenWithUid
[1/2]: Generates an AccessToken2, and sets the expiration for AccessToken2 and the expiration for all privileges.BuildTokenWithUid
[2/2]: Generates an AccessToken2, and sets the expiration for the following:
- AccessToken2
- The privilege of publishing audio streams in a channel
- The privilege of publishing video streams in a channel
- The privilege of publishing data streams in a channel
BuildTokenWithUid [1/2]
This method uses a token_expire
parameter to set the expiration for AccessToken2 and a privilege_expire
parameter to set the expiration for all privileges.
Parameter | Description |
---|---|
appId | The App ID of your Agora project. |
appCertificate | The App Certificate of your Agora project. |
channelName | The channel name. The string length must be less than 64 bytes. The following character sets are supported:
|
uid | The user ID of the user to be authenticated. A 32-bit unsigned integer with a value range from 1 to (2³² - 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. |
role | The privilege of the user, either as a publisher or a subscriber. This parameter determines whether a user can publish streams in the channel.
|
token_expire | The duration (in seconds) from the generation of an AccessToken2 to the expiration of that AccessToken2. For example, if you set it as 600, the AccessToken2 expires 10 minutes after generation. The maximum duration of an AccessToken2 is 24 hours. If you set it to a duration longer than 24 hours, the AccessToken2 still expires after 24 hours. If you set it to 0, the AccessToken2 expires immediately. |
privilege_expire | The duration (in seconds) from the generation of an AccessToken2 to the expiration of all privileges. For example, if you set it to 600, the privilege expires 10 minutes after generation. If you set it to 0 (default), the privilege never expires. |
BuildTokenWithUid [2/2]
To facilitate privilege-level configuration in a channel, Agora provides an overloaded method, BuildTokenWithUid
[2/2], to support configuring the expiration of the AccessToken2 and related privileges:
- Join a channel
- Publish audio streamsin a channel
- Publish video streams in a channel
- Publish data streams in a channel
This method generates an RTC AccessToken2 and supports configuring the expiration time of the token and the following privileges:
- Joining an RTC channel
- Publishing audio streams in an RTC channel
- Publishing video streams in an RTC channel
- Publishing data streams in an RTC channel
The privileges of publishing audio streams in an RTC channel, publishing video streams in an RTC channel, and publishing data streams in an RTC channel only take effect after enabling co-host authentication.
You can assign multiple privileges to a user. When a privilege is about to expire or has expired, the RTC SDK triggers the onTokenPriviegeWillExpire
callback or the onRequestToken
callback. You need to take the following actions in your own app logic:
- Tag the type of privilege that is about to expire or has expired in your app logic.
- The app fetches a new AccessToken2 from the token server.
- The SDK calls renewToken to renew the AccessToken2.
You need to set an appropriate expiration timestamp. For example, if the expiration time of joining a channel is earlier than that of publishing audio in the channel, when the privilege of joining a channel expires, the user is kicked out of the RTC channel. Even if the privilege of publishing audio is still valid, user cannot exercise that privilege.
Parameter | Description |
---|---|
token_expire | The duration (in seconds) from the generation of an AccessToken2 to the expiration of that AccessToken2. For example, if you set it as 600, the AccessToken2 expires 10 minutes after generation. The maximum duration of an AccessToken2 is 24 hours. If you set it to a duration longer than 24 hours, the AccessToken2 still expires after 24 hours. If you set it to 0, the AccessToken2 expires immediately. |
join_channel_privilege_expire | The duration (in seconds) from the generation of an AccessToken2 to the expiration of the privilege of joining a channel. For example, if you set it to 600, the privilege expires 10 minutes after generation. If you set it to 0 (default), the privilege never expires. |
pub_audio_privilege_expire | The duration (in seconds) from the generation of an AccessToken2 to the expiration of the privilege of publishing audio streams in a channel. For example, if you set it to 600, the privilege expires 10 minutes after generation. If you set it to 0 (default), the privilege never expires. |
pub_video_privilege_expire | The duration (in seconds) from the generation of an AccessToken2 to the expiration of the privilege of publishing video streams in a channel. For example, if you set it to 600, the privilege expires 10 minutes after generation. If you set it to 0 (default), the privilege never expires. |
pub_data_stream_privilege_expire | The duration (in seconds) from the generation of an AccessToken2 to the expiration of the privilege of publishing data streams in a channel. For example, if you set it to 600, the privilege expires 10 minutes after generation. If you set it to 0 (default), the privilege never expires. |
Enable co-host authentication
Refer to the following steps to enable this function in Agora Console:
- Log in to Agora Console. Under Projects, choose a project for which you want to enable co-host authentication, click the Config icon, and enter the Edit Project page.
- In the Features area, click Enable authentication.
- Follow the on-screen instructions to learn more about this function, check the box, and click Enable.
Co-host authentication takes effect in 5 minutes.
Once you have enabled co-host authentication, a user using your app must meet both of the following requirements to publish streams in a channel:
- The user role in
setClientRole
is set ashost
. - The user joins the channel with a token that has the privilege of a publisher (by setting the role parameter in the
buildToken
method askRolePublisher
).
Upgrade from AccessToken to AccessToken2
For how to use AccessToken to authenticate your users and how to upgrade to AccessToken2, see Upgrade from AccessToken to AccessToken2.