mirror of
https://github.com/ClusterCockpit/cc-backend
synced 2026-08-31 00:47:15 +02:00
feat(auth): map OIDC token roles to CC roles via configurable mapping
OIDC roles were translated by a hardcoded switch that only recognised the literal names user/admin/manager/support (dropping api) and required the IdP to emit exactly those names. Add an optional auth.oidc.role-mapping (OIDC role/group claim value -> CC role) so operators can map their own realm/client role names, including api. The mapping is the sole source of roles: only mapped names are honored, unmapped token roles are ignored, and users without any mapped role receive the base "user" role. Mapping targets are validated once at startup. Role assignment stays authoritative on every login as before. Note: deployments relying on the IdP emitting literal CC role names must now map them explicitly (e.g. "admin": "admin"). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Entire-Checkpoint: 66451678484b
This commit is contained in:
+49
-23
@@ -12,6 +12,8 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/ClusterCockpit/cc-backend/internal/repository"
|
||||
@@ -34,6 +36,12 @@ type OpenIDConfig struct {
|
||||
// OAuth2 client secret for the OIDC provider.
|
||||
// Overridden by the OID_CLIENT_SECRET environment variable when set.
|
||||
ClientSecret string `json:"client-secret"`
|
||||
|
||||
// Maps an OIDC role/group claim value to a CC role (admin/support/api/manager/user).
|
||||
// This is the sole source of roles: a token role grants a CC role only if it is
|
||||
// listed here. Unmapped token roles are ignored (no identity fallback), so literal
|
||||
// CC role names must be mapped explicitly. Users without any mapped role get "user".
|
||||
RoleMapping map[string]string `json:"role-mapping"`
|
||||
}
|
||||
|
||||
type OIDC struct {
|
||||
@@ -41,6 +49,9 @@ type OIDC struct {
|
||||
provider *oidc.Provider
|
||||
authentication *Authentication
|
||||
clientID string
|
||||
// roleMapping is the validated subset of OpenIDConfig.RoleMapping
|
||||
// (IdP role/group name -> CC role).
|
||||
roleMapping map[string]string
|
||||
}
|
||||
|
||||
func randString(nByte int) (string, error) {
|
||||
@@ -89,11 +100,47 @@ func NewOIDC(a *Authentication) *OIDC {
|
||||
Scopes: []string{oidc.ScopeOpenID, "profile", "roles"},
|
||||
}
|
||||
|
||||
oa := &OIDC{provider: provider, client: client, clientID: clientID, authentication: a}
|
||||
// Validate the optional role mapping once at startup. Invalid targets are
|
||||
// dropped with a warning rather than failing startup. IdP names (keys) are
|
||||
// kept verbatim so they match the raw token claim values.
|
||||
roleMapping := make(map[string]string)
|
||||
for name, ccRole := range Keys.OpenIDConfig.RoleMapping {
|
||||
role := strings.ToLower(ccRole)
|
||||
if !schema.IsValidRole(role) || role == schema.GetRoleString(schema.RoleAnonymous) {
|
||||
cclog.Warnf("OIDC: ignoring role-mapping '%s' -> '%s': invalid or non-assignable target role", name, ccRole)
|
||||
continue
|
||||
}
|
||||
roleMapping[name] = role
|
||||
}
|
||||
|
||||
oa := &OIDC{provider: provider, client: client, clientID: clientID, authentication: a, roleMapping: roleMapping}
|
||||
|
||||
return oa
|
||||
}
|
||||
|
||||
// mapOIDCRoles translates raw OIDC role/group names into the CC role set using
|
||||
// only the configured mapping (IdP name -> CC role). Unmapped names are ignored.
|
||||
// Always returns at least [user]. The result is deduplicated and sorted.
|
||||
func mapOIDCRoles(oidcRoles []string, mapping map[string]string) []string {
|
||||
roleSet := make(map[string]bool)
|
||||
for _, r := range oidcRoles {
|
||||
if cc, ok := mapping[r]; ok {
|
||||
roleSet[cc] = true
|
||||
}
|
||||
}
|
||||
|
||||
if len(roleSet) == 0 {
|
||||
return []string{schema.GetRoleString(schema.RoleUser)}
|
||||
}
|
||||
|
||||
roles := make([]string, 0, len(roleSet))
|
||||
for role := range roleSet {
|
||||
roles = append(roles, role)
|
||||
}
|
||||
sort.Strings(roles)
|
||||
return roles
|
||||
}
|
||||
|
||||
func (oa *OIDC) RegisterEndpoints(r chi.Router) {
|
||||
r.HandleFunc("/oidc-login", oa.OAuth2Login)
|
||||
r.HandleFunc("/oidc-callback", oa.OAuth2Callback)
|
||||
@@ -240,28 +287,7 @@ func (oa *OIDC) OAuth2Callback(rw http.ResponseWriter, r *http.Request) {
|
||||
oidcRoles = append(oidcRoles, access.Roles...)
|
||||
}
|
||||
|
||||
roleSet := make(map[string]bool)
|
||||
for _, r := range oidcRoles {
|
||||
switch r {
|
||||
case "user":
|
||||
roleSet[schema.GetRoleString(schema.RoleUser)] = true
|
||||
case "admin":
|
||||
roleSet[schema.GetRoleString(schema.RoleAdmin)] = true
|
||||
case "manager":
|
||||
roleSet[schema.GetRoleString(schema.RoleManager)] = true
|
||||
case "support":
|
||||
roleSet[schema.GetRoleString(schema.RoleSupport)] = true
|
||||
}
|
||||
}
|
||||
|
||||
var roles []string
|
||||
for role := range roleSet {
|
||||
roles = append(roles, role)
|
||||
}
|
||||
|
||||
if len(roles) == 0 {
|
||||
roles = append(roles, schema.GetRoleString(schema.RoleUser))
|
||||
}
|
||||
roles := mapOIDCRoles(oidcRoles, oa.roleMapping)
|
||||
|
||||
user := &schema.User{
|
||||
Username: username,
|
||||
|
||||
Reference in New Issue
Block a user