Skip to content

Lesson 3 of 9 · 23m

Session Identity, Tokens, and Room Capabilities

Bind browser sessions to short-lived room capabilities, secure TURN credentials, and design joins so leaked links do not become permanent access.

Teams often secure login carefully and then hand every authenticated browser a permanent room secret and static TURN password. That is backwards. Real-time media systems need capabilities scoped to a room, a participant, a device session, and a short expiry. Media infrastructure is expensive and sensitive; your tokens should reflect that.

Separate account identity from call capability

Account authentication answers "who is this person?" A join capability answers "what may this browser do right now?" The second should be narrower:

{
  "sub": "usr_ada",
  "device": "dev_web_01J9X7",
  "room": "rm_01J9W8",
  "role": "participant",
  "can_publish": ["audio", "video"],
  "can_subscribe": true,
  "exp": 1764529200,
  "jti": "jt_6f91d2"
}

If the token is a JWT, validate issuer, audience, expiry, and key ID server-side. If it is opaque, store only a hash. Either way, include jti or an equivalent nonce so revocation and single-use join admission are possible.

TURN credentials should be minted, not shared

TURN is a relay service. Static credentials in JavaScript are not configuration; they are public infrastructure credentials. Use time-limited credentials with the standard REST mechanism:

const timestamp = Math.floor(Date.now() / 1000) + 12 * 60 * 60;
const username = `${timestamp}:usr_ada`;
const credential = hmacSha1Base64(turnStaticSecret, username);

The browser receives this shape:

const pc = new RTCPeerConnection({
  iceServers: [
    {urls: ["stun:stun.rtc.internal:3478"]},
    {
      urls: ["turn:turn.rtc.internal:3478?transport=udp",
             "turn:turn.rtc.internal:3478?transport=tcp"],
      username,
      credential,
    },
  ],
  iceTransportPolicy: "auto",
});

Twelve hours is often too generous for a meeting. Match credential lifetime to maximum session duration plus reconnect grace, then mint fresh credentials when needed.

Every signaling message needs a room-scoped check

The server should enforce authorization where messages are routed, not only at join:

join room      token room_id must match URL room
publish track  role must include publish capability
offer/answer   sender must be admitted participant
ICE relay      sender and target must share active session
admin mute     role must include moderation capability

The URL room ID is input, not authority. A signed token for room A must not admit room B because a websocket route parameter says B.

Design invitations as revocation-sensitive objects

A meeting link is a bearer capability. If it is forwarded, the recipient can usually join. Mitigate with named participant approval, expiring invitations, one-time accept flows, or organization membership checks. Do not pretend obscurity is access control. Links leak through calendars, screenshots, browser history, analytics, and chat indexing.

Audit events without recording media

You can operate safely without storing call content. Record joins, leaves, publish starts, connection failures, token issuance, moderation actions, and relay usage. Keep them separate from media, encrypt sensitive metadata, and set retention deliberately. The incident you can investigate is often the one where a correlation ID exists across signaling, TURN, and SFU logs.

Field notes

  • The HMAC TURN secret belongs on the signaling or credential service and coturn — never in browser source.
  • Short token lifetimes do not remove the need for server-side reauthorization after role changes or participant removal.
  • iceTransportPolicy: "relay" is useful for privacy-sensitive deployments, but it turns relay bandwidth into a hard cost center.
  • If users can create unlimited rooms, they can consume unbounded signaling, SFU, and TURN resources. Bind room creation to product quota.

Was this lesson helpful?

Sign in to rate