Control access using JWTs
Learn how to add access control to a content with Livepeer UI Kit, using JWTs
Using JSON Web Tokens (JWTs) provides a robust way to control access to both your assets and livestreams. The JWTs can be signed and validated to ensure that only authorized users can access the content. Below are examples for both assets and livestreams.
Adding access control to a content only takes a few lines of code.
This guide is written for developers using @livepeer/react
in a React
application.
Create Gated Content
For livestreams
Create your gated stream, with the stream key returned once we create it (styling has been removed for simplicity)
For assets
Create your gated asset, with the jwt playback policy type.
Sign a JWT (Node.JS API Route)
Next, we add an API route - since we are using Next.JS, we add a custom Next.js API route. We add a check in the API route for a special “secret” that must be passed in the POST body for the user to gain access to the stream.
Make sure to create a signing key - those
values will be used as the environment variables ACCESS_CONTROL_PRIVATE_KEY
and NEXT_PUBLIC_ACCESS_CONTROL_PUBLIC_KEY
.
Configure the Player
Lastly, when the content is created, we make a POST request to the
/api/create-signed-jwt
API route we created in the previous step.
The React Player passes the JWT with a header, Livepeer-Jwt
, to the backend,
for WebRTC and HLS playback. For MP4 playback, it uses a query parameter,
jwt
.
Then, we pass the JWT to the Player using the
jwt
prop, which will use that JWT to prove
access to the content!
Using a custom player
If you are not using the player, you will need to pass a header, Livepeer-Jwt
,
when you perform WebRTC SDP negotiation, or when you play back from a m3u8 URL.
For WebRTC SDP negotiation, here is an example of the header being passed:
You can also append the JWT to the WebRTC URL as a query parameter, similar to:
Similarly, for HLS playback, you can pass the JWT in a header:
If you are using HLS.js for your own custom player, you can set the JWT header like this:
Finally, you can append the JWT to the m3u8 URL as a query parameter:
Was this page helpful?