Secure authentication 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, for example, join a classroom.
In order to provide a better authentication experience and security guarantee, on August 18, 2022, Agora launched a new version of the token, AccessToken2. We recommend using AccessToken2, which is compatible with AccessToken.
This page explains how to use AccessToken2 to deploy a token generator on the server and use token authentication on the client, as well as providing reference information.
Understand the tech
The following figure shows the steps in the authentication flow:
An Agora token is a dynamic key generated on your app server that is valid for 24 hours or less. When your users log in to Flexible Classroom from your app client, the 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 user ID
-
The Unix timestamp for when the token expires
Prerequisites
In order to follow this example 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.
NoteIf you are using Go 1.16+, GO111MODULE is on by default. See this blog post for details. -
npm and a supported browser.
Implement the authentication flow
This section shows you how to use a token generator to generate tokens and authenticate users.
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 for AccessToken2
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.
-
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:
-
Start the server by running the following command:
Use AccessToken2 for authentication
This section uses the Web client as an example to show how to use a token for client-side user authentication. The following reference code comes from the Flexible Classroom web source code.
-
Initiate a token request to the server integrated with the Agora token generator:
-
Use the token obtained in this request to create a
launchOption
object: -
Call
launch
and use the token to join the classroom:
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
-
App Global
The scope is global App operations, such as creating classrooms, setting and querying rooms, etc.
Language Algorithm Core method Sample code C++ HMAC-SHA256 BuildAppToken N/A Go HMAC-SHA256 BuildAppToken sample.go Java HMAC-SHA256 buildAppToken EducationTokenBuilder2Sample.java Node.js HMAC-SHA256 buildAppToken EducationTokenSample.js PHP HMAC-SHA256 buildAppToken EducationTokenBuilderSample.php Python 2 HMAC-SHA256 build_app_token education_token_builder_sample.py Python 3 HMAC-SHA256 build_app_token education_token_builder_sample.py -
Specify room and user (used by client)
The scope is the specified user in the specified room. This token packages two services,
ServiceEducation
andServiceRtm
, which are passed in when the client SDK is started. It can help users open the token for Flexible Classroom and Signaling user login.Language Algorithm Core method Sample code C++ HMAC-SHA256 BuildRoomUserToken N/A Go HMAC-SHA256 BuildRoomUserToken sample.go Java HMAC-SHA256 buildRoomUserToken EducationTokenBuilder2Sample.java Node.js HMAC-SHA256 buildRoomUserToken EducationTokenSample.js PHP HMAC-SHA256 buildRoomUserToken EducationTokenBuilderSample.php Python 2 HMAC-SHA256 build_room_user_token education_token_builder_sample.py Python 3 HMAC-SHA256 build_room_user_token education_token_builder_sample.py
AccessToken
-
If you are using AccessToken, you can refer to the following sample code:
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
Considerations
Parameter consistency
The parameters used to generate the token need to be consistent with the ones you used to launch Flexible Classroom.
App certificate and token
To use the token for authentication, you need to enable the app certificate for your project on Agora Console. Once a project has enabled the app certificate, you must use tokens to authenticate its users.