From 3963777660412596c8796343b2b42d3e4b42c0a3 Mon Sep 17 00:00:00 2001 From: Fabio Huser Date: Fri, 31 Dec 2021 06:33:45 +0100 Subject: [PATCH] Derive chat display-name from upstream HTTP user header (#1633) --- controllers/chat.go | 4 ++++ test/automated/api/chat.test.js | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/controllers/chat.go b/controllers/chat.go index dbe5d9dc2..a768fcccc 100644 --- a/controllers/chat.go +++ b/controllers/chat.go @@ -58,6 +58,10 @@ func RegisterAnonymousChatUser(w http.ResponseWriter, r *http.Request) { // this is fine. register a new user anyway. } + if request.DisplayName == "" { + request.DisplayName = r.Header.Get("X-Forwarded-User") + } + newUser, err := user.CreateAnonymousUser(request.DisplayName) if err != nil { WriteSimpleResponse(w, false, err.Error()) diff --git a/test/automated/api/chat.test.js b/test/automated/api/chat.test.js index a4e1cf22f..3484bed0d 100644 --- a/test/automated/api/chat.test.js +++ b/test/automated/api/chat.test.js @@ -38,3 +38,24 @@ test('can fetch chat messages', async (done) => { done(); }); + +test('can derive display name from user header', async (done) => { + const res = await request + .post('/api/chat/register') + .set('X-Forwarded-User', 'test-user') + .expect(200); + + expect(res.body.displayName).toBe('test-user'); + done(); +}); + +test('can overwrite user header derived display name with body', async (done) => { + const res = await request + .post('/api/chat/register') + .send({displayName: 'TestUserChat'}) + .set('X-Forwarded-User', 'test-user') + .expect(200); + + expect(res.body.displayName).toBe('TestUserChat'); + done(); +});