Skip to content
ngTurn

Time-limited TURN credentials

Never ship a static TURN password inside a web or mobile app: anyone can copy it and use your bandwidth. Generate short-lived credentials on your server instead.

ngTurn uses the widely supported shared-secret scheme from the TURN REST API proposal, the same one coturn implements with use-auth-secret:

  1. The username is an expiry time in Unix seconds, optionally followed by a colon and your own user ID: 1767225600:user-42.
  2. The credential is the Base64-encoded HMAC-SHA1 of that username, keyed with your shared secret.
  3. The relay recomputes the HMAC and rejects the credential after the expiry time.

Keep lifetimes short, typically one hour for calls. Clients only need a credential that is valid when the session starts.

turn-credentials.js
import { createHmac } from 'node:crypto';
export function turnCredentials(userId, secret, ttlSeconds = 3600) {
const username = `${Math.floor(Date.now() / 1000) + ttlSeconds}:${userId}`;
const credential = createHmac('sha1', secret).update(username).digest('base64');
return { username, credential, ttl: ttlSeconds };
}

Expose an authenticated endpoint in your backend that returns { username, credential } for the signed-in user, and call it right before creating the peer connection.