From 52c1514978e9deeaaed38e9a75b7bfd17b7d7371 Mon Sep 17 00:00:00 2001 From: Joachim Bauch Date: Wed, 19 May 2021 16:22:15 +0200 Subject: [PATCH] Always use auth userid in ping requests. Fixes issue introduced in 3a2adffab67153bf7ae662ec0df4aa974cb9bcd2 where some ping requests were not evaluated correctly if a userid was given in the room session data. --- clientsession.go | 6 +++++- hub_test.go | 27 +++++++++++++++++++++++++++ room.go | 15 +++++++++++---- room_test.go | 11 +++++++++++ 4 files changed, 54 insertions(+), 5 deletions(-) diff --git a/clientsession.go b/clientsession.go index a8f9fc2..6451aff 100644 --- a/clientsession.go +++ b/clientsession.go @@ -239,6 +239,10 @@ func (s *ClientSession) ParsedBackendUrl() *url.URL { return s.parsedBackendUrl } +func (s *ClientSession) AuthUserId() string { + return s.userId +} + func (s *ClientSession) UserId() string { userId := s.userId if userId == "" { @@ -449,7 +453,7 @@ func (s *ClientSession) doUnsubscribeRoomNats(notify bool) { // Notify go func(sid string) { ctx := context.Background() - request := NewBackendClientRoomRequest(room.Id(), s.UserId(), sid) + request := NewBackendClientRoomRequest(room.Id(), s.userId, sid) request.Room.Action = "leave" var response map[string]interface{} if err := s.hub.backend.PerformJSONRequest(ctx, s.ParsedBackendUrl(), request, &response); err != nil { diff --git a/hub_test.go b/hub_test.go index ca3afa0..9b241e7 100644 --- a/hub_test.go +++ b/hub_test.go @@ -299,6 +299,31 @@ func processSessionRequest(t *testing.T, w http.ResponseWriter, r *http.Request, return response } +func processPingRequest(t *testing.T, w http.ResponseWriter, r *http.Request, request *BackendClientRequest) *BackendClientResponse { + if request.Type != "ping" || request.Ping == nil { + t.Fatalf("Expected an ping backend request, got %+v", request) + } + + if request.Ping.RoomId == "test-room-with-sessiondata" { + if entries := request.Ping.Entries; len(entries) != 1 { + t.Errorf("Expected one entry, got %+v", entries) + } else { + if entries[0].UserId != "" { + t.Errorf("Expected empty userid, got %+v", entries[0]) + } + } + } + + response := &BackendClientResponse{ + Type: "ping", + Ping: &BackendClientRingResponse{ + Version: BackendVersion, + RoomId: request.Ping.RoomId, + }, + } + return response +} + func registerBackendHandler(t *testing.T, router *mux.Router) { registerBackendHandlerUrl(t, router, "/") } @@ -312,6 +337,8 @@ func registerBackendHandlerUrl(t *testing.T, router *mux.Router, url string) { return processRoomRequest(t, w, r, request) case "session": return processSessionRequest(t, w, r, request) + case "ping": + return processPingRequest(t, w, r, request) default: t.Fatalf("Unsupported request received: %+v", request) return nil diff --git a/room.go b/room.go index feb0e0c..e750e68 100644 --- a/room.go +++ b/room.go @@ -638,7 +638,7 @@ func (r *Room) publishSessionFlagsChanged(session *VirtualSession) { } } -func (r *Room) publishActiveSessions() { +func (r *Room) publishActiveSessions() (int, *sync.WaitGroup) { r.mu.RLock() defer r.mu.RUnlock() @@ -651,13 +651,16 @@ func (r *Room) publishActiveSessions() { } var sid string + var uid string switch sess := session.(type) { case *ClientSession: - // Use Nextcloud session id + // Use Nextcloud session id and user id sid = sess.RoomSessionId() + uid = sess.AuthUserId() case *VirtualSession: // Use our internal generated session id (will be added to Nextcloud). sid = sess.PublicId() + uid = sess.UserId() default: continue } @@ -676,14 +679,17 @@ func (r *Room) publishActiveSessions() { entries[u] = append(e, BackendPingEntry{ SessionId: sid, - UserId: session.UserId(), + UserId: uid, }) } + var wg sync.WaitGroup if len(urls) == 0 { - return + return 0, &wg } for u, e := range entries { + wg.Add(1) go func(url *url.URL, entries []BackendPingEntry) { + defer wg.Done() ctx, cancel := context.WithTimeout(context.Background(), r.hub.backendTimeout) defer cancel() @@ -694,6 +700,7 @@ func (r *Room) publishActiveSessions() { } }(urls[u], e) } + return len(entries), &wg } func (r *Room) publishRoomMessage(message *BackendRoomMessageRequest) { diff --git a/room_test.go b/room_test.go index 62fbb5e..7644491 100644 --- a/room_test.go +++ b/room_test.go @@ -407,4 +407,15 @@ func TestRoom_RoomSessionData(t *testing.T) { if userid := session.UserId(); userid != expected { t.Errorf("Expected userid %s, got %s", expected, userid) } + + room := hub.getRoom(roomId) + if room == nil { + t.Fatalf("Room not found") + } + + entries, wg := room.publishActiveSessions() + if entries != 1 { + t.Errorf("expected 1 entries, got %d", entries) + } + wg.Wait() }