auth: add Keycloak OIDC verifier, role check, and test mock

This commit is contained in:
2026-07-24 21:26:15 +02:00
parent bf41a34f59
commit 59df391291
5 changed files with 276 additions and 4 deletions

View File

@@ -0,0 +1,31 @@
package auth
import (
"encoding/json"
"testing"
)
func TestIDTokenClaims_HasRole(t *testing.T) {
cases := []struct {
name string
json string
role string
want bool
}{
{"role present among others", `{"realm_access":{"roles":["geniusrun-user","other"]}}`, "geniusrun-user", true},
{"role absent", `{"realm_access":{"roles":["other"]}}`, "geniusrun-user", false},
{"realm_access missing entirely", `{}`, "geniusrun-user", false},
{"roles array empty", `{"realm_access":{"roles":[]}}`, "geniusrun-user", false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
var c idTokenClaims
if err := json.Unmarshal([]byte(tc.json), &c); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if got := c.hasRole(tc.role); got != tc.want {
t.Errorf("hasRole(%q) = %v, want %v", tc.role, got, tc.want)
}
})
}
}