Secure authentication with tokens

Updated

Introduces how to retrieve tokens from your app server to authenticate your users.

Agora uses digital tokens to authenticate users and their privileges before they access Agora Chat. For test purposes, you can generate temporary tokens in Agora Console. See Manage users and generate tokens for details.

In a development environment, you deploy your own app server to generate authentication tokens. This page introduces how to set up a token server, retrieve tokens from your server, and authenticate your users.

Understand the tech

Agora Chat employs two types of tokens for authentication:

  • Use tokens with app privileges to call Agora RESTful APIs.

  • Use tokens with user privileges for user authentication and interaction through the Agora Chat SDK.

Use-caseToken typeInformation required to generateMaximum validity
RESTful API callsToken with app privilegesApp ID of your Agora Chat projectApp certificate of your Agora Chat projectToken validity period for your Agora Chat project24 hours
SDK API callsToken with user privilegesApp ID of your Agora Chat projectApp certificate of your Agora Chat projectToken validity period for your Agora Chat projectThe Agora Chat user ID24 hours

Prerequisites

In order to follow this procedure, you must have the following:

If you have a firewall implemented in your network environment, Agora provides a firewall whitelist solution for you to access Agora Chat in environments with restricted network access. If you want to use the firewall whitelist solution, submit a ticket and our technical support will provide the target domain and corresponding IP.

Implement the authentication flow

This section shows you how to supply and consume a token used to authenticate a user with Agora Chat.

Deploy an app server to generate tokens

Tokens used for connecting with Agora Chat are generated by your app server. When a client sends a request, the app server generates an appropriate token. The following figure shows how to manage tokens with app and user privileges to facilitate secure user authentication and interaction with Agora Chat services.

Token generation API call sequence

This section guides you through setting up a token server. Choose your preferred language and follow the step-by-step procedure.

This sample server code provided in this guide is for demonstration purposes only. Do not use it directly in a production environment.

Chat SDK token authentication

The following code fetches and replaces an expired token for the Agora Chat SDK. It performs the following tasks:

  1. Fetches a fresh token on app startup* At the start of the application, it fetches a valid token for the user from a token server.

  2. Listens for onTokenWillExpire event: The app sets up a listener to monitor when the token is about to expire. Upon receiving this event, it fetches a new token from the token server and renews the token with the Agora Chat SDK. Since SDK v1.4.0, the SDK triggers this event when 80% of the token's validity period has elapsed (previously 50%).

Reference

This section introduces token generator libraries, version requirements, and related documents about tokens.

Token generator libraries

For more examples and source code in other development languages, explore the AgoraDynamicKey open-source repository on GitHub.

Tokens for Agora RTC products

If you use Agora Chat together with the Agora RTC SDK, Agora recommends upgrading to AccessToken 2.