Authentication
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 Signaling system.
This document shows you how to create a Signaling token server and a Signaling client app. The Signaling client app retrieves a Signaling token from the Signaling token server. This token authenticates the current user when the user accesses Signaling.
Understand the tech
The following figure shows the steps in the authentication flow:
a Signaling token is a dynamic key generated on your app server that is valid for 24 hours. When your users log in to Signaling from your app client, Signaling validates the token and reads the user and project information stored in the token. a Signaling token contains the following information:
-
The App ID of your Agora project
-
The App Certificate of your Agora project
-
The Signaling user ID of the user to be authenticated
-
The Unix timestamp when 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 browsers.
-
For AccessToken2, Signaling SDK v1.5.0 or greater.
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 plus 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 Platform. 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 is for demonstration purposes only. Do not use it in a production environment.
-
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.Implement either:
-
AccessToken2
-
AccessToken
-
-
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:
-
Start the server by running the following command:
Use tokens for user authentication
This section uses the Web client as an example to show how to use a token for client-side user authentication.
In order to show the authentication workflow, this section shows how to build and run a Web client on your local machine.
This sample client is for demonstration purposes only. Do not use it in a production environment.
-
Create the project structure of the Web client with a folder including the following files.
-
index.html
: User interface -
client.js
: App logic with Agora Signaling SDK
-
-
Download Agora Signaling SDK for Web. Save the JS file in
libs
to your project directory. -
In
index.html
, add the following code to include the app logic in the UI, then replace<path to the JS file>
with the path of the JS file you saved in step 2. -
Create the app logic by editing
client.js
with the following content. Then replace<Your App ID>
with your App ID. The App ID must match the one in the server. You also need to 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
. -
Open
index.html
with a supported browser to perform the following actions:
-
Successfully logging in to Signaling.
-
Renewing a token every 30 seconds.
Reference
This section introduces token generator libraries, version requirements, and related documents about tokens.
Token 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.
-
AccessToken2
Language Algorithm Core method Sample code C++ HMAC-SHA256 buildToken RtmTokenBuilder2Sample.cpp Go HMAC-SHA256 buildToken sample.go Java HMAC-SHA256 buildToken RtmTokenBuilder2Sample.java PHP HMAC-SHA256 buildToken RtmTokenBuilder2Sample.php Python 2 HMAC-SHA256 buildToken RtmTokenBuilder2Sample.py Python 3 HMAC-SHA256 buildToken RtmTokenBuilder2Sample.py -
AccessToken
Language Algorithm Core method Sample code C++ HMAC-SHA256 buildToken RtmTokenBuilderSample.cpp Go HMAC-SHA256 buildToken sample.go Java HMAC-SHA256 buildToken RtmTokenBuilderSample.java Node.js HMAC-SHA256 buildToken RtmTokenBuilderSample.js PHP HMAC-SHA256 buildToken RtmTokenBuilderSample.php Python 2 HMAC-SHA256 buildToken RtmTokenBuilderSample.py Python 3 HMAC-SHA256 buildToken RtmTokenBuilderSample.py
API Reference
This section introduces the method to generate a Signaling token. Take C++ as an example:
-
AccessToken2
Parameter Description appId
The App ID of your Agora project. appCertificate
The App Certificate of your Agora project. userId
The user ID of the RTM system. You need specify the user ID yourself. See the userId parameter of the login method for supported character sets. expire
The duration (in seconds) from the generation of AccessToken2 to the expiration of AccessToken2. For example, if you set it as 600, the AccessToken2 expires 10 minutes after generation. An AccessToken2 is valid for a maximum of 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. -
AccessToken
Parameter Description appId The App ID of your Agora project. appCertificate The App Certificate of your Agora project. userAccount The user ID of Signaling. You need specify the user ID yourself. See the userId parameter of the login method for supported character sets. role The user role. Agora supports only one user role. Set the value as the default value Rtm_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. This parameter is currently invalid. You can ignore this parameter. a Signaling token is valid for 24 hours.
Upgrade AccessToken2
This section introduces how to upgrade from AccessToken to AccessToken2 by example.
Prerequisites
- You have deployed a token server and a web client for AccessToken in a previous version.
- You have integrated an SDK version that supports AccessToken2.
Update the token server
- Replace the
rtmtokenbuilder
import statement:
- Update the
BuildToken
function:
Test the AccessToken2 server
The client does not require any updates; however, the expiration logic changes accordingly.
To use AccessToken2 in server-side, refer to RESTful API Authentication and authenticate with new request headers.
Considerations
User ID
The user ID that you use to generate the Signaling token must be the same as the one you use to log in to Signaling.
App Certificate and Signaling token
To use the Signaling token for authentication, you need to enable the App Certificate for your project on Console. Once a project has enabled the App Certificate, you must use Signaling tokens to authenticate its users.
Signaling Token expiration
-
AccessToken2
AccessToken2 allows you to specify the validity period of an RTM token in seconds based on your business requirements. The validity period can be a maximum of 24 hours.
When a token is due to expire in 30 seconds, the RTM SDK triggers the
onTokenPrivilegeWillExpire
callback. Upon receiving this callback, you can generate a new RTM token on your app server and callrenewToken
to pass the new RTM token to the SDK.When an RTM token expires, the subsequent logic varies depending on the connection state of the SDK:
- If the RTM SDK is in the
CONNECTION_STATE_CONNECTED
state, users receive theonTokenExpired
callback and theonConnectionStateChanged
callback caused byCONNECTION_CHANGE_REASON_TOKEN_EXPIRED (9)
, notifying them that the connection state of the SDK switches toCONNECTION_STATE_ABORTED
. In this case, users need to log in again via thelogin
method. - If the RTM SDK is in the
CONNECTION_STATE_RECONNECTING
state, users receive theonTokenExpired
callback when the network reconnects. In this case, users need to renew the token via therenewToken
method.
Although you can use the
onTokenPrivilegeWillExpire
andonTokenExpired
callbacks to handle token expiration conditions, Agora recommends that you regularly renew the token (such as every hour) to keep the token valid.The names of methods, callbacks, and enums mentioned in the above section only apply to C++. Refer to the API documentation for names in other platforms.
- If the RTM SDK is in the
-
AccessToken
a Signaling token is valid for 24 hours.
When the Signaling SDK is in the
CONNECTION_STATE_CONNECTED
state, the user remains online even if the Signaling token expires. If a user logs in with an expired Signaling token, the Signaling SDK returns theLOGIN_ERR_TOKEN_EXPIRED
error.The Signaling SDK triggers the
onTokenExpired
callback only when a Signaling token expires and the Signaling SDK is in theCONNECTION_STATE_RECONNECTING
state. The callback is triggered only once. Upon receiving this callback, you can generate a new Signaling token on your app server, and callrenewToken
to pass the new Signaling token to the SDK.