keycloak-scim/authorization_services/topics/enforcer/authorization-context.adoc

71 lines
3.9 KiB
Text
Raw Normal View History

2016-11-29 15:30:53 +00:00
[[_enforcer_authorization_context]]
==== Obtaining the Authorization Context
2016-06-17 21:05:51 +00:00
When policy enforcement is enabled, the permissions obtained from the server are available through `org.keycloak.AuthorizationContext`.
This class provides several methods you can use to obtain permissions and ascertain whether a permission was granted for a particular resource or scope.
2016-06-17 21:05:51 +00:00
Obtaining the Authorization Context in a Servlet Container
```java
HttpServletRequest request = ... // obtain javax.servlet.http.HttpServletRequest
2016-09-09 04:25:17 +00:00
KeycloakSecurityContext keycloakSecurityContext =
(KeycloakSecurityContext) request
.getAttribute(KeycloakSecurityContext.class.getName());
AuthorizationContext authzContext =
keycloakSecurityContext.getAuthorizationContext();
2016-06-17 21:05:51 +00:00
```
[NOTE]
For more details about how you can obtain a `KeycloakSecurityContext` consult the adapter configuration. The example above should be sufficient
2017-08-28 12:50:14 +00:00
to obtain the context when running an application using any of the servlet containers supported by {project_name}.
2016-06-17 21:05:51 +00:00
The authorization context helps give you more control over the decisions made and returned by the server. For example, you can use it
to build a dynamic menu where items are hidden or shown depending on the permissions associated with a resource or scope.
2016-06-17 21:05:51 +00:00
```java
if (authzContext.hasResourcePermission("Project Resource")) {
// user can access the Project Resource
}
if (authzContext.hasResourcePermission("Admin Resource")) {
// user can access administration resources
}
if (authzContext.hasScopePermission("urn:project.com:project:create")) {
// user can create new projects
}
```
2017-08-28 12:50:14 +00:00
The `AuthorizationContext` represents one of the main capabilities of {project_name} Authorization Services. From the examples above, you can see that the protected resource is not directly associated with the policies that govern them.
2016-06-17 21:05:51 +00:00
Consider some similar code using role-based access control (RBAC):
2016-06-17 21:05:51 +00:00
```java
if (User.hasRole('user')) {
// user can access the Project Resource
}
if (User.hasRole('admin')) {
// user can access administration resources
}
if (User.hasRole('project-manager')) {
// user can create new projects
}
```
2017-08-28 12:50:14 +00:00
Although both examples address the same requirements, they do so in different ways. In RBAC, roles only _implicitly_ define access for their resources. With {project_name} you gain the capability to create more manageable code that focuses directly on your resources whether you are using RBAC, attribute-based access control (ABAC), or any other BAC variant. Either you have the permission for a given resource or scope, or you don't.
2016-06-17 21:05:51 +00:00
Now, suppose your security requirements have changed and in addition to project managers, PMOs can also create new projects.
2016-06-17 21:05:51 +00:00
2017-08-28 12:50:14 +00:00
Security requirements change, but with {project_name} there is no need to change your application code to address the new requirements. Once your application is based on the resource and scope identifier, you need only change the configuration of the permissions or policies associated with a particular resource in the authorization server. In this case, the permissions and policies associated with the `Project Resource` and/or the scope `urn:project.com:project:create` would be changed.
2017-04-25 22:52:57 +00:00
==== Using the AuthorizationContext to obtain an Authorization Client Instance
2017-08-28 12:50:14 +00:00
The ```AuthorizationContext``` can also be used to obtain a reference to the <<_service_client_api, Authorization Client API>> configured to your application:
2017-04-25 22:52:57 +00:00
```java
ClientAuthorizationContext clientContext = ClientAuthorizationContext.class.cast(authzContext);
AuthzClient authzClient = clientContext.getClient();
```
In some cases, resource servers protected by the policy enforcer need to access the APIs provided by the authorization server. With an ```AuthzClient``` instance in hands, resource servers can interact with the server in order to create resources or check for specific permissions programmatically.