🐛 fix(auth): Use Django user ID for LiveKit token identity #747
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem:
When using certain OpenID Connect (OIDC) providers (e.g., LemonLDAP) where the
sub
claim (subject identifier) is not formatted as a UUID, users encountered an "Invalid LiveKit token" error when attempting to activate features requiring LiveKit token authentication (e.g., real-time subtitles).The error message indicated that the value provided was "not a valid UUID", specifically during the
UserModel.objects.get(id=user_id)
lookup in theLiveKitTokenAuthentication
process.Root Cause:
The
LiveKitTokenAuthentication
class insrc/backend/core/authentication/livekit.py
attempts to retrieve a DjangoUser
object usingUserModel.objects.get(id=user_id)
, whereuser_id
is derived from theidentity
field of the LiveKit token. Theidentity
for authenticated users was previously set tostr(user.sub)
in thegenerate_token
utility function.While LiveKit itself expects an identity string, the Django
User
model'sid
field is a UUID. If the OIDCsub
claim (which populatesuser.sub
) is not a UUID, a mismatch occurs when the application tries to fetch the user by theirid
using this non-UUIDsub
value. This leads to aFieldError
orValidationError
during the database lookup, causing the LiveKit token validation to fail.Solution:
To ensure robust compatibility with all OIDC providers, the
generate_token
function insrc/backend/core/utils.py
has been modified. Instead of usingstr(user.sub)
as theidentity
for the LiveKit token, it now consistently usesstr(user.id)
.The
user.id
field is the internal Django User ID, which is guaranteed to be a valid UUID. This change ensures that the LiveKit token'sidentity
always matches the format expected by theUserModel.objects.get(id=...)
lookup, regardless of the format of the OIDCsub
claim.Impact:
This fix resolves the "Invalid LiveKit token" error for users authenticated via OIDC providers that do not provide UUIDs in their
sub
claims. It improves the application's compatibility and reliability across diverse authentication environments.