Dev.to Security πŸ” Cybersecurity πŸ‘ 0 πŸ“– 4 min read

Token-authenticated video playback: how short-lived playback tokens work

Disclosure: I work at TPStreams. If you put paid videos behind a login, it's tempting to think the login protects the video. It doesn't. The login protects the page. The video is fetched by the player from its own URL,

Disclosure: I work at TPStreams.

If you put paid videos behind a login, it's tempting to think the login protects the video. It doesn't. The login protects the page. The video is fetched by the player from its own URL, and if that URL works without the login, anyone who copies it from the network tab can share it.

Token-authenticated playback closes that gap. This post walks through what a playback token is, how the flow works, what to put in a token, and what tokens don't solve.

The problem with a static URL

A typical setup looks like this:

  1. Student logs in to your site.
  2. Your page embeds https://cdn.example.com/videos/lesson-4/master.m3u8.
  3. The player fetches the playlist and segments from the CDN.

Step 3 has nothing to do with step 1. The CDN doesn't know who is logged in. The URL is the same for every student and it never expires, so it ends up in a group chat.

The idea: sign the URL per viewer, per session

Instead of a permanent URL, your backend issues a short-lived token every time an authorised user presses play. The video service (or CDN) checks the token before it serves anything.

Browser            Your backend               Video service / CDN
   |  press play        |                            |
   |------------------->|  check: logged in? paid?  |
   |                    |  create token (expires 5m) |
   |<-------------------|                            |
   |  playlist URL + token  ------------------------>|  verify signature + expiry
   |<------------------------------------------------|  serve playlist & segments

The important property: the token is created on your server, with a secret the browser never sees. The browser can use the token, but it can't create or extend one.

What goes into a token

Most implementations are either an HMAC over a few fields or a signed JWT. The fields that matter:

  • Video ID: the token only works for this asset.
  • Expiry: a few minutes is common. Long enough to start playback, short enough that a copied link dies quickly.
  • Viewer ID: useful for analytics, session limits and watermarking.
  • Optional restrictions: IP address, allowed domain/referrer, or country.

A minimal HMAC example in Node:

import crypto from "node:crypto";

function signPlayback(videoId, userId, ttlSeconds = 300) {
  const expires = Math.floor(Date.now() / 1000) + ttlSeconds;
  const payload = `${videoId}:${userId}:${expires}`;
  const sig = crypto
    .createHmac("sha256", process.env.PLAYBACK_SECRET)
    .update(payload)
    .digest("base64url");
  return { expires, sig };
}

// in your route, after checking the user has access:
const { expires, sig } = signPlayback("lesson-4", req.user.id);
const url = `https://cdn.example.com/videos/lesson-4/master.m3u8` +
            `?uid=${req.user.id}&exp=${expires}&sig=${sig}`;

The verifier recomputes the HMAC from the same fields and rejects the request if the signature doesn't match or exp is in the past. Use a constant-time comparison (crypto.timingSafeEqual) when you compare signatures.

HLS adds a wrinkle

An HLS stream isn't one file. The player fetches a master playlist, then rendition playlists, then hundreds of segments. If only the master playlist is protected, someone can still pull the segment URLs directly.

Common ways to handle this:

  • Token in a cookie or path prefix that the CDN checks on every request under that path (CloudFront signed cookies work this way).
  • Rewriting playlists so every segment URL carries its own short-lived signature.
  • Protecting the decryption key instead of the segments: segments are encrypted, and the key (or DRM licence) is only issued to a request with a valid token.

The third approach pairs naturally with DRM. With Widevine and FairPlay, the licence request is where you check the token, and the key never reaches a browser that wasn't authorised.

How providers implement it

Most video platforms and CDNs ship some version of this, so you rarely need to build the verifier yourself:

  • AWS CloudFront: signed URLs and signed cookies, using a key pair you register.
  • Mux: playback IDs with a "signed" policy, played with a JWT you sign on your server.
  • Cloudflare Stream: signed URLs/tokens, which you can create via their API or sign yourself with a key.
  • Bunny Stream: token authentication on the CDN, with an expiry and a hash.
  • TPStreams (where I work): your backend calls the API to generate an expiring playback token per viewer, combined with Widevine/FairPlay DRM, domain restrictions and dynamic watermarking.

The details differ (JWT vs HMAC, cookie vs query string), but the model is the same: your server decides who can watch, and the edge enforces it.

Practical tips

  • Keep TTLs short and issue a new token on each play. Don't cache tokens in local storage.
  • Never sign on the client. If the secret is in your JavaScript bundle, it isn't a secret.
  • Check entitlement before signing, not after. The token is only as good as the access check behind it.
  • Bind the token to the viewer, so you can enforce concurrent-session limits and show a per-viewer watermark.
  • Log token creation. If a video leaks, knowing which account requested which tokens helps you find the source.

What tokens don't solve

Tokens stop link sharing and hotlinking. They don't stop an authorised viewer from recording the screen. That's where DRM output protection (on supported devices) and a visible, viewer-specific watermark come in: the first makes capture harder, the second makes a leak traceable.

Think of it as layers: tokens control who can start playback, DRM controls how the content is decrypted, and watermarking tells you who leaked it if it gets out anyway.

If you want to see how the token flow fits with uploads, DRM and SDKs in one API, the TPStreams developer page covers it: https://tpstreams.com/developer-api/

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.