From 6519644fc1bda5cb4a0b19f05afb9309092b9ada Mon Sep 17 00:00:00 2001 From: Stavros Date: Tue, 15 Jul 2025 00:17:41 +0300 Subject: [PATCH] fix: handle type string for oauth groups --- internal/constants/constants.go | 8 ++++---- internal/handlers/oauth.go | 2 +- internal/utils/utils.go | 12 +++++++++++ internal/utils/utils_test.go | 35 +++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 5 deletions(-) diff --git a/internal/constants/constants.go b/internal/constants/constants.go index c1d464c..d6f64fa 100644 --- a/internal/constants/constants.go +++ b/internal/constants/constants.go @@ -2,10 +2,10 @@ package constants // Claims are the OIDC supported claims (prefered username is included for convinience) type Claims struct { - Name string `json:"name"` - Email string `json:"email"` - PreferredUsername string `json:"preferred_username"` - Groups []string `json:"groups"` + Name string `json:"name"` + Email string `json:"email"` + PreferredUsername string `json:"preferred_username"` + Groups any `json:"groups"` } // Version information diff --git a/internal/handlers/oauth.go b/internal/handlers/oauth.go index 6e1528f..827a497 100644 --- a/internal/handlers/oauth.go +++ b/internal/handlers/oauth.go @@ -189,7 +189,7 @@ func (h *Handlers) OAuthCallbackHandler(c *gin.Context) { Name: name, Email: user.Email, Provider: providerName.Provider, - OAuthGroups: strings.Join(user.Groups, ","), + OAuthGroups: utils.CoalesceToString(user.Groups), }) // Check if we have a redirect URI diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 0c499b1..ac5c9ca 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -327,3 +327,15 @@ func DeriveKey(secret string, info string) (string, error) { encodedKey := base64.StdEncoding.EncodeToString(key) return encodedKey, nil } + +func CoalesceToString(value any) string { + switch v := value.(type) { + case []string: + return strings.Join(v, ",") + case string: + return v + default: + log.Warn().Interface("value", value).Msg("Unsupported type, returning empty string") + return "" + } +} diff --git a/internal/utils/utils_test.go b/internal/utils/utils_test.go index d36b434..957bfae 100644 --- a/internal/utils/utils_test.go +++ b/internal/utils/utils_test.go @@ -511,3 +511,38 @@ func TestDeriveKey(t *testing.T) { t.Fatalf("Expected %v, got %v", expected, result) } } + +func TestCoalesceToString(t *testing.T) { + t.Log("Testing coalesce to string with a string") + + value := "test" + expected := "test" + + result := utils.CoalesceToString(value) + + if result != expected { + t.Fatalf("Expected %v, got %v", expected, result) + } + + t.Log("Testing coalesce to string with a slice of strings") + + valueSlice := []string{"test1", "test2"} + expected = "test1,test2" + + result = utils.CoalesceToString(valueSlice) + + if result != expected { + t.Fatalf("Expected %v, got %v", expected, result) + } + + t.Log("Testing coalesce to string with an unsupported type") + + valueUnsupported := 12345 + expected = "" + + result = utils.CoalesceToString(valueUnsupported) + + if result != expected { + t.Fatalf("Expected %v, got %v", expected, result) + } +}