From 61489fc4a74e722a2d50d09d693165d17777e350 Mon Sep 17 00:00:00 2001
From: Bolke de Bruin <bolke@xs4all.nl>
Date: Fri, 26 Aug 2022 12:15:43 +0200
Subject: [PATCH] Fix username selection

---
 cmd/rdpgw/security/basic.go | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/cmd/rdpgw/security/basic.go b/cmd/rdpgw/security/basic.go
index 595fe81..e2596b7 100644
--- a/cmd/rdpgw/security/basic.go
+++ b/cmd/rdpgw/security/basic.go
@@ -23,18 +23,18 @@ func CheckHost(ctx context.Context, host string) (bool, error) {
 	case "roundrobin", "unsigned":
 		var username string
 
-		log.Printf("Checking host")
 		s := getSessionInfo(ctx)
-		if s == nil {
+		if s == nil || s.UserName == "" {
 			var ok bool
 			username, ok = ctx.Value("preferred_username").(string)
 			if !ok {
 				return false, errors.New("no valid session info or username found in context")
 			}
 		}
+		log.Printf("Checking host for user %s", username)
 		for _, h := range Hosts {
 			if username != "" {
-				h = strings.Replace(h, "{{ preferred_username }}", s.UserName, 1)
+				h = strings.Replace(h, "{{ preferred_username }}", username, 1)
 			}
 			if h == host {
 				return true, nil
-- 
GitLab