Skip to content

Commit fa22603

Browse files
fkwptoger5
andauthored
Update room alias handling in token generation for LegacySfuRequest (#147)
* Update room alias handling in token generation for LegacySfuRequest * update test for room alias handling in token generation for LegacySfuRequest * Update main.go Co-authored-by: Timo <16718859+toger5@users.noreply.github.com> * doc update * rephrase doc --------- Co-authored-by: Timo <16718859+toger5@users.noreply.github.com>
1 parent 99e80c9 commit fa22603

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

main.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,21 @@ func (h *Handler) processLegacySFURequest(r *http.Request, req *LegacySFURequest
226226

227227
// TODO: is DeviceID required? If so then we should have validated at the start
228228
lkIdentity := userInfo.Sub + ":" + req.DeviceID
229-
token, err := getJoinToken(h.key, h.secret, req.Room, lkIdentity)
229+
230+
// We can hard-code the slotId since for the m.call application only the m.call#ROOM slot is defined.
231+
// This ensures that the same LiveKit room alias being derived for the same Matrix room for both the
232+
// LegacySFURequest (/sfu/get endpoint) and the SFURequest (/get_token endpoint).
233+
//
234+
// Note a mismatch between the legacy livekit_alias (which is the Matrix roomId) field in the MatrixRTC
235+
// membership state event and the actual lkRoomAlias (as derived below and used on the SFU) which is
236+
// part of the LiveKit JWT Token does in general NOT confuse clients as the JWT token is passed as is
237+
// to the livekit-client SDK.
238+
//
239+
// This change ensures compatibility with clients using pseudonymous livekit_aliases.
240+
slotId := "m.call#ROOM"
241+
lkRoomAliasHash := sha256.Sum256([]byte(req.Room + "|" + slotId))
242+
lkRoomAlias := unpaddedBase64.EncodeToString(lkRoomAliasHash[:])
243+
token, err := getJoinToken(h.key, h.secret, lkRoomAlias, lkIdentity)
230244
if err != nil {
231245
return nil, &MatrixErrorResponse{
232246
Status: http.StatusInternalServerError,
@@ -236,7 +250,7 @@ func (h *Handler) processLegacySFURequest(r *http.Request, req *LegacySFURequest
236250
}
237251

238252
if isFullAccessUser {
239-
if err := createLiveKitRoom(r.Context(), h, req.Room, userInfo.Sub, lkIdentity); err != nil {
253+
if err := createLiveKitRoom(r.Context(), h, lkRoomAlias, userInfo.Sub, lkIdentity); err != nil {
240254
return nil, &MatrixErrorResponse{
241255
Status: http.StatusInternalServerError,
242256
ErrCode: "M_UNKNOWN",

main_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,10 @@ func TestLegacyHandlePost(t *testing.T) {
250250
u, _ := url.Parse(testServer.URL)
251251

252252
matrixServerName = u.Host
253+
matrixRoom := "testRoom"
253254

254255
testCase := map[string]interface{}{
255-
"room": "testRoom",
256+
"room": matrixRoom,
256257
"openid_token": map[string]interface{}{
257258
"access_token": "testAccessToken",
258259
"token_type": "testTokenType",
@@ -313,9 +314,13 @@ func TestLegacyHandlePost(t *testing.T) {
313314
t.Errorf("unexpected sub: got %v want %v", claims["sub"], "@user:"+matrixServerName+":testDevice")
314315
}
315316

317+
slotId := "m.call#ROOM"
318+
lkRoomAliasHash := sha256.Sum256([]byte(matrixRoom + "|" + slotId))
319+
lkRoomAlias := unpaddedBase64.EncodeToString(lkRoomAliasHash[:])
320+
316321
// should have permission for the room
317-
if claims["video"].(map[string]interface{})["room"] != "testRoom" {
318-
t.Errorf("unexpected room: got %v want %v", claims["room"], "testRoom")
322+
if claims["video"].(map[string]interface{})["room"] != lkRoomAlias {
323+
t.Errorf("unexpected room: got %v want %v", claims["room"], lkRoomAlias)
319324
}
320325
}
321326

0 commit comments

Comments
 (0)