9.5.2. Obtaining Entitlements
The library provides an keycloak-authz.jsentitlement function that you can use to obtain an RPT from the server by providing the resources and scopes your client wants to access.
Example about how to obtain an RPT with permissions for all resources and scopes the user can access
authorization.entitlement('my-resource-server-id').then(function (rpt) {
// onGrant callback function.
// If authorization was successful you'll receive an RPT
// with the necessary permissions to access the resource server
});
Example about how to obtain an RPT with permissions for specific resources and scopes
authorization.entitlement('my-resource-server', {
"permissions": [
{
"id" : "Some Resource"
}
]
}).then(function (rpt) {
// onGrant
});
When using the entitlement function, you must provide the client_id of the resource server you want to access.
The entitlement function is completely asynchronous and supports a few callback functions to receive notifications from the server:
-
onGrant: The first argument of the function. If authorization was successful and the server returned an RPT with the requested permissions, the callback receives the RPT. -
onDeny: The second argument of the function. Only called if the server has denied the authorization request. -
onError: The third argument of the function. Only called if the server responds unexpectedly.