feat(oidc): Added new claim in userinfo response to return all requested audience roles (#9861)

# Which Problems Are Solved

The /userinfo endpoint only returns roles for the current project, even
if the access token includes multiple project aud scopes.

This prevents clients from retrieving all user roles across multiple
projects, making multi-project access control ineffective.

# How the Problems Are Solved

Modified the /userinfo handler logic to resolve roles across all valid
project audience scopes provided in the token, not just the current
project.
Ensured that if **urn:zitadel:iam:org:projects:roles is in the scopes**,
roles from all declared project audiences are collected and included in
the response in **urn:zitadel:iam:org:projects:roles claim**.

# Additional Changes

# Additional Context

This change enables service-to-service authorization workflows and SPA
role resolution across multiple project contexts with a single token.
- Closes #9831

---------

Co-authored-by: Masum Patel <[email protected]>
Co-authored-by: Tim Möhlmann <[email protected]>
This commit is contained in:
masum-msphere
2025-09-22 09:55:21 +00:00
committed by GitHub
co-authored by Masum Patel Tim Möhlmann
parent e5575d6042
commit 295584648d
6 changed files with 141 additions and 9 deletions
+14 -1
View File
@@ -209,7 +209,7 @@ func assertRoles(projectID string, user *query.OIDCUserInfo, roleAudience, reque
}
// prevent returning obtained grants if none where requested
if (projectID != "" && len(requestedRoles) > 0) || len(roleAudience) > 0 {
setUserInfoRoleClaims(info, newProjectRoles(projectID, user.UserGrants, requestedRoles))
setUserInfoRoleClaims(info, newProjectRoles(projectID, user.UserGrants, requestedRoles, roleAudience))
}
}
@@ -278,12 +278,25 @@ func setUserInfoOrgClaims(user *query.OIDCUserInfo, out *oidc.UserInfo) {
func setUserInfoRoleClaims(userInfo *oidc.UserInfo, roles *projectsRoles) {
if roles != nil && len(roles.projects) > 0 {
// Create a map to store accumulated roles for ClaimProjectsRoles
projectsRoles := make(projectRoles)
for requestAudID := range roles.requestAudIDs {
if projectRole, ok := roles.projects[requestAudID]; ok {
maps.Copy(projectsRoles, projectRole)
}
}
if roles, ok := roles.projects[roles.requestProjectID]; ok {
userInfo.AppendClaims(ClaimProjectRoles, roles)
}
for projectID, roles := range roles.projects {
userInfo.AppendClaims(fmt.Sprintf(ClaimProjectRolesFormat, projectID), roles)
}
// Finally, set the accumulated ClaimProjectsRoles
if len(projectsRoles) > 0 {
userInfo.AppendClaims(ClaimProjectsRoles, projectsRoles)
}
}
}