mirror of
https://github.com/netbirdio/netbird.git
synced 2026-10-23 22:09:09 +02:00
[management] Handle single-string JWT group claim from IdPs (#6014)
This commit is contained in:
@@ -146,7 +146,11 @@ func (c *ClaimsExtractor) ToGroups(token *jwt.Token, claimName string) []string
|
|||||||
userJWTGroups := make([]string, 0)
|
userJWTGroups := make([]string, 0)
|
||||||
|
|
||||||
if claim, ok := claims[claimName]; ok {
|
if claim, ok := claims[claimName]; ok {
|
||||||
if claimGroups, ok := claim.([]interface{}); ok {
|
switch claimGroups := claim.(type) {
|
||||||
|
case string:
|
||||||
|
// Some IdPs emit a single group claim as a string instead of an array.
|
||||||
|
userJWTGroups = append(userJWTGroups, claimGroups)
|
||||||
|
case []any:
|
||||||
for _, g := range claimGroups {
|
for _, g := range claimGroups {
|
||||||
if group, ok := g.(string); ok {
|
if group, ok := g.(string); ok {
|
||||||
userJWTGroups = append(userJWTGroups, group)
|
userJWTGroups = append(userJWTGroups, group)
|
||||||
@@ -154,9 +158,11 @@ func (c *ClaimsExtractor) ToGroups(token *jwt.Token, claimName string) []string
|
|||||||
log.Debugf("JWT claim %q contains a non-string group (type: %T): %v", claimName, g, g)
|
log.Debugf("JWT claim %q contains a non-string group (type: %T): %v", claimName, g, g)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
|
log.Debugf("JWT claim %q is not a string or string array (type: %T): %v", claimName, claim, claim)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Debugf("JWT claim %q is not a string array", claimName)
|
log.Debugf("JWT claim %q is missing", claimName)
|
||||||
}
|
}
|
||||||
|
|
||||||
return userJWTGroups
|
return userJWTGroups
|
||||||
|
|||||||
@@ -249,6 +249,15 @@ func TestClaimsExtractor_ToGroups(t *testing.T) {
|
|||||||
groupClaimName: "groups",
|
groupClaimName: "groups",
|
||||||
expectedGroups: []string{},
|
expectedGroups: []string{},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "extracts single group string from claim",
|
||||||
|
claims: jwt.MapClaims{
|
||||||
|
"sub": "user-123",
|
||||||
|
"groups": "admin",
|
||||||
|
},
|
||||||
|
groupClaimName: "groups",
|
||||||
|
expectedGroups: []string{"admin"},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "handles custom claim name",
|
name: "handles custom claim name",
|
||||||
claims: jwt.MapClaims{
|
claims: jwt.MapClaims{
|
||||||
|
|||||||
Reference in New Issue
Block a user