Created
August 15, 2020 11:59
-
-
Save Videl/222eff2f16383e2f318ecec2228d1008 to your computer and use it in GitHub Desktop.
JS for script mapper in Keycloak
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://stackoverflow.com/questions/52518298/how-to-create-a-script-mapper-in-keycloak | |
// https://stackoverflow.com/questions/48199539/best-way-to-test-debug-javascript-mappers-policies-in-keycloak | |
// https://docs.oracle.com/javase/10/nashorn/nashorn-java-api.htm#JSNUG119 | |
var client = keycloakSession.getContext().getClient(); | |
var forEach = Array.prototype.forEach; | |
// print(user.getRealmRoleMappings()); | |
var isAdmin = false; | |
forEach.call(user.getRealmRoleMappings().toArray(), function (roleModel) { | |
// print(roleModel.getName()); | |
if (roleModel.getName().contains("admin")) { | |
isAdmin = true; | |
} | |
}); | |
if (isAdmin) { | |
token.getOtherClaims().put("claimName", "admin"); | |
} else { | |
token.getOtherClaims().put("claimName", "normal_user"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment