mirror of
https://github.com/Pacerino/CaddyProxyManager.git
synced 2026-08-04 11:15:08 -04:00
Support CPM_AUTH_MODE=local|oidc. OIDC uses go-oidc with JIT user provisioning and an optional email-domain allowlist. Adds auth config, login and callback endpoints, a JWT signing-key bootstrap and Provider/Subject fields on the user model. Includes OIDC allowlist tests.
55 lines
1.6 KiB
Go
55 lines
1.6 KiB
Go
package auth
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/Pacerino/CaddyProxyManager/internal/config"
|
|
)
|
|
|
|
func TestIsAllowedDomain(t *testing.T) {
|
|
original := config.Configuration.Auth.OIDC.AllowedDomains
|
|
t.Cleanup(func() { config.Configuration.Auth.OIDC.AllowedDomains = original })
|
|
|
|
tests := []struct {
|
|
name string
|
|
allowed []string
|
|
email string
|
|
want bool
|
|
}{
|
|
{"empty allowlist permits all", nil, "user@anything.com", true},
|
|
{"exact match", []string{"example.com"}, "user@example.com", true},
|
|
{"case insensitive", []string{"Example.COM"}, "user@example.com", true},
|
|
{"whitespace trimmed", []string{" example.com "}, "user@example.com", true},
|
|
{"not allowed", []string{"example.com"}, "user@evil.com", false},
|
|
{"no at sign", []string{"example.com"}, "notanemail", false},
|
|
{"one of many", []string{"a.com", "b.com"}, "user@b.com", true},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
config.Configuration.Auth.OIDC.AllowedDomains = tt.allowed
|
|
if got := isAllowedDomain(tt.email); got != tt.want {
|
|
t.Errorf("isAllowedDomain(%q) with %v = %v, want %v", tt.email, tt.allowed, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestIsOIDCEnabled(t *testing.T) {
|
|
original := config.Configuration.Auth.Mode
|
|
t.Cleanup(func() { config.Configuration.Auth.Mode = original })
|
|
|
|
config.Configuration.Auth.Mode = "oidc"
|
|
if !IsOIDCEnabled() {
|
|
t.Error("expected oidc enabled")
|
|
}
|
|
config.Configuration.Auth.Mode = "OIDC"
|
|
if !IsOIDCEnabled() {
|
|
t.Error("expected oidc enabled (case-insensitive)")
|
|
}
|
|
config.Configuration.Auth.Mode = "local"
|
|
if IsOIDCEnabled() {
|
|
t.Error("expected oidc disabled")
|
|
}
|
|
}
|