mirror of
https://github.com/casdoor/casdoor.git
synced 2025-09-06 18:10:29 +08:00
feat(jwt): Enable using User Properties as custom claims (#3571)
This commit is contained in:
@@ -342,10 +342,31 @@ func getClaimsCustom(claims Claims, tokenField []string) jwt.MapClaims {
|
|||||||
res["provider"] = claims.Provider
|
res["provider"] = claims.Provider
|
||||||
|
|
||||||
for _, field := range tokenField {
|
for _, field := range tokenField {
|
||||||
userField := userValue.FieldByName(field)
|
if strings.HasPrefix(field, "Properties") {
|
||||||
if userField.IsValid() {
|
/*
|
||||||
newfield := util.SnakeToCamel(util.CamelToSnakeCase(field))
|
Use selected properties fields as custom claims.
|
||||||
res[newfield] = userField.Interface()
|
Converts `Properties.my_field` to custom claim with name `my_field`.
|
||||||
|
*/
|
||||||
|
parts := strings.Split(field, ".")
|
||||||
|
if len(parts) != 2 || parts[0] != "Properties" { // Either too many segments, or not properly scoped to `Properties`, so skip.
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
base, fieldName := parts[0], parts[1]
|
||||||
|
mField := userValue.FieldByName(base)
|
||||||
|
if !mField.IsValid() { // Can't find `Properties` field, so skip.
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
finalField := mField.MapIndex(reflect.ValueOf(fieldName))
|
||||||
|
if finalField.IsValid() { // // Provided field within `Properties` exists, add claim.
|
||||||
|
res[fieldName] = finalField.Interface()
|
||||||
|
}
|
||||||
|
|
||||||
|
} else { // Use selected user field as claims.
|
||||||
|
userField := userValue.FieldByName(field)
|
||||||
|
if userField.IsValid() {
|
||||||
|
newfield := util.SnakeToCamel(util.CamelToSnakeCase(field))
|
||||||
|
res[newfield] = userField.Interface()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user