From f9ab9a64061ad4b36e7a80a713d905f1da464960 Mon Sep 17 00:00:00 2001 From: Stavros Date: Sat, 15 Feb 2025 17:23:24 +0200 Subject: [PATCH] fix: filter oauth whitelist to remove empty strings --- cmd/root.go | 4 +++- internal/utils/utils.go | 10 ++++++++++ internal/utils/utils_test.go | 19 +++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/cmd/root.go b/cmd/root.go index 1d9ec3a..4b8f6d8 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -62,7 +62,9 @@ var rootCmd = &cobra.Command{ } // Create oauth whitelist - oauthWhitelist := strings.Split(config.OAuthWhitelist, ",") + oauthWhitelist := utils.Filter(strings.Split(config.OAuthWhitelist, ","), func(val string) bool { + return val != "" + }) log.Debug().Msg("Parsed OAuth whitelist") // Create OAuth config diff --git a/internal/utils/utils.go b/internal/utils/utils.go index fd88240..4d0ebab 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -207,3 +207,13 @@ func GetTinyauthLabels(labels map[string]string) types.TinyauthLabels { func OAuthConfigured(config types.Config) bool { return (config.GithubClientId != "" && config.GithubClientSecret != "") || (config.GoogleClientId != "" && config.GoogleClientSecret != "") || (config.GenericClientId != "" && config.GenericClientSecret != "") || (config.TailscaleClientId != "" && config.TailscaleClientSecret != "") } + +// Filter helper function +func Filter[T any](slice []T, test func(T) bool) (res []T) { + for _, value := range slice { + if test(value) { + res = append(res, value) + } + } + return res +} diff --git a/internal/utils/utils_test.go b/internal/utils/utils_test.go index a2dcabc..d2e475b 100644 --- a/internal/utils/utils_test.go +++ b/internal/utils/utils_test.go @@ -313,3 +313,22 @@ func TestGetTinyauthLabels(t *testing.T) { t.Fatalf("Expected %v, got %v", expected, result) } } + +// Test the filter function +func TestFilter(t *testing.T) { + t.Log("Testing filter helper") + + // Create variables + data := []string{"", "val1", "", "val2", "", "val3", ""} + expected := []string{"val1", "val2", "val3"} + + // Test the filter function + result := utils.Filter(data, func(val string) bool { + return val != "" + }) + + // Check if the result is equal to the expected + if !reflect.DeepEqual(expected, result) { + t.Fatalf("Expected %v, got %v", expected, result) + } +}