mirror of
https://github.com/lukaszraczylo/traefikoidc.git
synced 2026-06-05 22:44:17 +00:00
c3f23cb99b
* Resolve issue with opaque tokens not being parsed correctly * Increase test coverage * Further improvements to test coverage and code quality * Add new providers. * fixup! Add new providers. * Cleanup. * fixup! Cleanup. * fixup! fixup! Cleanup. * fixup! fixup! fixup! Cleanup. * fixup! fixup! fixup! fixup! Cleanup. * Memory management optimisation 24 bytes per Put < 256-4096 bytes per buffer allocation avoided (10-170x difference) * Pooling cleanup.
126 lines
3.0 KiB
Go
126 lines
3.0 KiB
Go
// Package utils provides common utility functions used across the OIDC middleware
|
|
package utils
|
|
|
|
import (
|
|
"os"
|
|
"runtime"
|
|
"strings"
|
|
)
|
|
|
|
// CreateStringMap creates a map with string keys for efficient lookups
|
|
func CreateStringMap(items []string) map[string]struct{} {
|
|
result := make(map[string]struct{})
|
|
for _, item := range items {
|
|
result[item] = struct{}{}
|
|
}
|
|
return result
|
|
}
|
|
|
|
// CreateCaseInsensitiveStringMap creates a map with lowercase keys for case-insensitive matching
|
|
func CreateCaseInsensitiveStringMap(items []string) map[string]struct{} {
|
|
result := make(map[string]struct{})
|
|
for _, item := range items {
|
|
result[strings.ToLower(item)] = struct{}{}
|
|
}
|
|
return result
|
|
}
|
|
|
|
// DeduplicateScopes removes duplicate scopes from a slice
|
|
func DeduplicateScopes(scopes []string) []string {
|
|
seen := make(map[string]bool)
|
|
result := []string{}
|
|
for _, scope := range scopes {
|
|
if !seen[scope] {
|
|
seen[scope] = true
|
|
result = append(result, scope)
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
// MergeScopes combines default scopes with user-provided scopes, removing duplicates
|
|
func MergeScopes(defaultScopes, userScopes []string) []string {
|
|
if len(userScopes) == 0 {
|
|
return append([]string(nil), defaultScopes...)
|
|
}
|
|
|
|
seen := make(map[string]bool)
|
|
var result []string
|
|
|
|
for _, scope := range defaultScopes {
|
|
if !seen[scope] {
|
|
seen[scope] = true
|
|
result = append(result, scope)
|
|
}
|
|
}
|
|
|
|
for _, scope := range userScopes {
|
|
if !seen[scope] {
|
|
seen[scope] = true
|
|
result = append(result, scope)
|
|
}
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
// IsTestMode detects if the code is running in a test environment
|
|
func IsTestMode() bool {
|
|
if os.Getenv("SUPPRESS_DIAGNOSTIC_LOGS") == "1" {
|
|
return true
|
|
}
|
|
|
|
if strings.Contains(os.Args[0], ".test") ||
|
|
strings.Contains(os.Args[0], "go_build_") ||
|
|
os.Getenv("GO_TEST") == "1" ||
|
|
runtime.Compiler == "yaegi" {
|
|
return true
|
|
}
|
|
|
|
for _, arg := range os.Args {
|
|
if strings.Contains(arg, "-test") {
|
|
return true
|
|
}
|
|
}
|
|
|
|
if runtime.Compiler == "gc" {
|
|
progName := os.Args[0]
|
|
if strings.Contains(progName, "test") ||
|
|
strings.HasSuffix(progName, ".test") ||
|
|
strings.Contains(progName, "__debug_bin") {
|
|
return true
|
|
}
|
|
}
|
|
|
|
// Only use runtime stack check as fallback when no explicit test conditions are being controlled
|
|
if os.Getenv("DISABLE_RUNTIME_STACK_CHECK") != "1" &&
|
|
os.Getenv("SUPPRESS_DIAGNOSTIC_LOGS") == "" &&
|
|
os.Getenv("GO_TEST") == "" {
|
|
// Check runtime stack for test functions only as last resort
|
|
buf := make([]byte, 2048)
|
|
n := runtime.Stack(buf, false)
|
|
stack := string(buf[:n])
|
|
if strings.Contains(stack, "testing.tRunner") ||
|
|
strings.Contains(stack, "testing.(*T)") ||
|
|
strings.Contains(stack, ".test.") {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// KeysFromMap extracts string keys from a map for logging purposes
|
|
func KeysFromMap(m map[string]struct{}) []string {
|
|
keys := make([]string, 0, len(m))
|
|
for k := range m {
|
|
keys = append(keys, k)
|
|
}
|
|
return keys
|
|
}
|
|
|
|
// BuildFullURL constructs a URL from scheme, host, and path components
|
|
func BuildFullURL(scheme, host, path string) string {
|
|
return scheme + "://" + host + path
|
|
}
|