How do I use co-host token authentication?
Co-host authentication enables you to authenticate whether a user has the privilege to publish streams in a live streaming channel. This feature helps ensure that only authorized users publish streams and prevents illegal users from exploiting business vulnerabilities or stealing tokens to bomb a live broadcast room.
Understand the tech
You deploy a token server and generate tokens with the required privileges; the Agora server verifies the tokens you generate.
-
Using co-host authentication requires app logic changes. Ensure that you read this article before enabling this function.
-
Co-host authentication applies to scenarios where the channel profile is set to Live Broadcasting.
Prerequisites
Before proceeding, ensure that your app meets the following requirements:
- Uses the Agora Video SDK v2.1.0 or later.
- Uses only token-based authentication on all app clients to authenticate users. For details, see Upgrade authentication mechanism.
Implementation
Set the role
parameter
This section shows you how to set the role
parameter when generating a token using AccessToken2
. The following code uses C++ as an example but the principles and steps remain the same if you use another programming language to build your token server.
Parameter | Description |
---|---|
role | The publishing privilege of the user:
|
Modify the app logic
Refer to the following steps to authenticate whether a user has the publishing privilege in scenarios where an audience member wants to become a host:
- Before joining a channel, the app client applies for a token with the privilege of a subscriber. The app server generates a token and passes it to the app client.
- The app client calls
joinChannel
and passes the token generated withkRoleSubscriber
privilege to the SDK. - Before changing the user role from audience to host, the app client applies for a token with the privilege of
kRolePublisher
. The app server generates a second token, and passes it to the app client. - The app client calls
renewToken
and passes the new token to the SDK. - The app client calls
setClientRole
to change the user role from an audience member to a host. The Agora server authenticates the token when the app client callssetClientRole
. If the token is generated with the privilege ofkRolePublisher
, the app client can publish streams.
-
If the user wants to switch from a host to an audience member, repeat steps 3 to 5. Apply for a token with the privilege of
kRoleSubscriber
, callrenewToken
on the app client, and then callsetClientRole
. -
When the token expires, you generate a new token on the app server and call
renewToken
to pass the fresh token to the SDK. The new token also has a service validity period.
Enable co-host authentication
Refer to the following steps to enable co-host authentication in Agora Console:
- Log in to Agora Console. Under Projects, choose a project for which you want to enable co-host authentication, click the Edit icon, and enter the Edit Project page.
- In the All Features area, click Co-Host authentication.
- Follow the on-screen instructions to know more about this function, and click Enable Co-host authentication. Co-host authentication takes effect in 5 minutes.
FAQs
-
Suppose a user takes the role of broadcaster. After I enable co-host authentication, will the user be able to publish streams?
Answer: Yes. After the token expires, you need to generate a new token with the privilege of
kRolePublisher
, and callrenewToken
to pass the new token to the SDK. -
Suppose a user takes the role of audience. After I enable co-host authentication, what should I do if this user wants to switch to broadcaster and publish streams?
Answer: Once co-host authentication is enabled, a user needs to meet both of the following requirements to publish streams:
- The user role in
setClientRole
is set as Broadcaster. - The user joins the channel with a token generated by setting the
role
parameter in thebuildToken
method tokRolePublisher
.
To summarize, for an audience member to become a host and publish streams, you need to follow steps in Modify the app logic, generate a token with the privilege of a publisher, call
renewToken
to pass the new token to the SDK, and then callsetClientRole
to change the user role to broadcaster. - The user role in