Skip to main content
gortsplib does not enforce authentication automatically. Instead, you call ctx.Conn.VerifyCredentials(req, user, pass) at the start of the appropriate handler callbacks and return StatusUnauthorized when it returns false.

VerifyCredentials

VerifyCredentials checks the Authorization header of an incoming RTSP request against the expected username and password. On the first call per connection it generates a digest nonce; subsequent calls reuse it. It supports both Basic and Digest MD5 authentication by default (see Server.AuthMethods). Returns true if the credentials are valid, false if they are absent or incorrect.

When to call VerifyCredentials

The correct callbacks to verify credentials depend on whether the client is a publisher or a reader.
RTSP does not always send credentials on the first request. Some clients only include the Authorization header after receiving a 401 Unauthorized response. VerifyCredentials handles this challenge/response cycle automatically.

Readers

Readers go through two requests before they can receive packets: DESCRIBE and SETUP. You must verify credentials in both callbacks to prevent unauthenticated access.

Publishers

Publishers are authenticated during ANNOUNCE, before any media is accepted.

ErrServerAuth sentinel

When returning an unauthorized response, always include liberrors.ErrServerAuth{} as the error value. This tells the server to emit a proper WWW-Authenticate challenge header in the response, which prompts the client to retry with credentials.
Returning a nil error alongside StatusUnauthorized will not send the WWW-Authenticate header, breaking the authentication handshake for clients that use Digest authentication.

Full example

server-auth/main.go

Notes

Use separate credentials for publishers and readers (as shown above). This lets you grant write access to trusted sources while allowing broader read access with different — or no — credentials.
The default AuthMethods accepts Basic and Digest MD5. Digest SHA-256 is excluded by default because it prevents FFmpeg from authenticating. To enable it, set Server.AuthMethods explicitly.