API Manager runs Identity Server application management under the hood to look up user roles and permissions. In org.wso2.carbon.identity.application.mgt.ApplicationMgtUtil
you can see the isUserAuthorized
method that is presumably fired whenever the application needs to check the user's permissions.
/**
* @param applicationName
* @param username
* @return
* @throws IdentityApplicationManagementException
*/
public static boolean isUserAuthorized(String applicationName, String username)
throws IdentityApplicationManagementException {
String applicationRoleName = getAppRoleName(applicationName);
try {
if (log.isDebugEnabled()) {
log.debug("Checking whether user has role : " + applicationRoleName + " by retrieving role list of " +
"user : " + username);
}
String[] userRoles = CarbonContext.getThreadLocalCarbonContext().getUserRealm()
.getUserStoreManager().getRoleListOfUser(username);
for (String userRole : userRoles) {
if (applicationRoleName.equals(userRole)) {
return true;
}
}
} catch (UserStoreException e) {
throw new IdentityApplicationManagementException("Error while checking authorization for user: " +
username + " for application: " + applicationName, e);
}
return false;
}
You should be able to replace
String[] userRoles = CarbonContext.getThreadLocalCarbonContext().getUserRealm()
.getUserStoreManager().getRoleListOfUser(username);
with code that will retrieve roles based on the attributes present in the SAML2 token, though obviously you'd have to build and support a structure capable of storing such a mapping.
https://github.com/wso2/carbon-identity-framework/blob/master/components/application-mgt/org.wso2.carbon.identity.application.mgt/src/main/java/org/wso2/carbon/identity/application/mgt/ApplicationMgtUtil.java