Entitlement Mediator with WSO2 Enterprise Service Bus (ESB)

The WSO2 Identity Server (IS) can function as a eXtensible Access Control Markup Language (XACML) Policy Decision Point (PDP) and the WSO2 Enterprise Service Bus (ESB) can function as a XACML Policy Enforcement Point (PEP). In this post I will explain the configurations available with WSO2 ESB to enforce XACML. The XAMCL standard refrains from specifying what method should be used to communicate from the PEP to the PDP. Many vendors have implemented their proprietary way of doing this. There is a standard around this called “Web Services Profile of XACML (WS-XACML) Version 1.0”. However there hasn’t been any development on this standard for quite a few years now and not much traction is being shown towards it due to its bias to SOAP and its  performance implications due to XML signatures. However the benefit of adopting a standard is the elimination of vendor locking. That is one day you may need move to a PDP from another vendor and still have the current PEP work with it; and if the communication was done based on standards and the new PDP also supports this standard you will easily be able to switch PDPs. Otherwise you may need to modify your existing PEP to adopt to the new PDP. The WSO2 Identity Server has its proprietary SOAP API, Thrift API and basic support of WS-XACML.

Adding Entitlement Mediator to a Sequence

Adding Entitlement Mediator to a Sequence

The PEP in WSO2 ESB is called ‘Entitlement Mediator’. You can engage this mediator within a sequence.

Entitlement Mediator Design View

Entitlement Mediator Design View

As you can see in the above picture, the Entitlement mediator has 4 child sequence references branching from it. ‘OnAccept’ is the sequence which is invoked when the Entitlement mediator returns a result of ‘Permit’. For any other results such as ‘Deny’, ‘Not Applicable’ or ‘Indeterminate’ from the PDP the mediator would invoke the ‘OnReject’ sequence. It is up to the administrator who configures the mediator to decide what to do with the two outcomes. E.g. If the result was ‘Permit’ the request could be directed to the back end service that was requested. If it was ‘Deny’ the request can be directed to a different endpoint or the WSO2 ESB may respond to the client with a message saying “Unauthorized!”. Following images show such a configuration.

OnAccept/OnReject Design View

OnAccept/OnReject Design View

OnAccept/OnReject Source View

OnAccept/OnReject Source View

The ‘Obligations’ and ‘Advice’ sequences are used to handle obligation and advice statements in the XACML response. If there are any obligation or advice statements in the XACML response, the current message context would be cloned and a new message context created and the obligation or advice statements respectively will be set to the SOAP body and the corresponding sequence will be invoked on it. These two sequences have been added as extension points where the user should design it as he want.The advice sequence will be invoked asynchronously and the Entitlement mediator will not wait for its response. The obligations sequence will be invoked synchronously and the Entitlement mediator will wait for its response. If the obligations sequence returns true the Entitlement mediator will proceed to the OnAccept sequence, but if the obligations sequence returns false the Entitlement mediator will proceed to the OnReject sequence. These sequences can be given in-line or by referring to sequences stored in the registry.

Entitlement Mediator Configuration

Entitlement Mediator Configuration

The above image shows the configuration section of the Entitlement mediator. The ‘Entitlement Service Client Type’ is to say which method of communication should be used between the PEP and the PDP. With SOAP there are two options that can be used depending on the method of authentication. You could either authenticate using the Authentication Admin service or using Basic Authentication headers. The Basic Authentication support is only available with WSO2 IS 4.0.0 onwards. With previous Identity Servers you could only use SOAP with Authentication AdminThrift uses its own authentication service over TCP. Thrift is available from IS 3.2.3 onwards. WS-XACML also uses Basic Authentication and is available from IS 4.0.0. ‘Entitlement Sever’ specifies the server URL of the WSO2 IS. This should take the value in the form of

https://<ip>:<port>/services/

if the Entitlement Service Client Type is set to SOAP or WS-XACML. If it is set to thrift then the Entitlement Server should have the value in the form of

https://<ip>:<port>/

The ‘User Name’ and ‘Password’ fields should take the credentials of a user who has privileges to invoke the Entitlement Service in WSO2 IS. ‘Thrift Host’ and ‘Thrift Port’ should be configured if the Entitlement Service Client Type is thrift. This is the host-port pair used to establish a thrift connection to the entitlement service. The default port on which the thrift service starts is 10500. The Entitlement mediator defines a set of hooks that can be optionally overridden which defines the subject-id, resource-id and action-id attribute IDs respectively of the Subject, Resource and Action categories of the XACML 3.0 request. This configuration is what is known as the ‘Entitlement Callback Handler’. In addition to the above 3 attribute IDs users can add any number of attribute IDs for any number of attribute categories as well. There are four out-of-the-box handlers which can be switched depending on the setup. These handlers exhibit certain behavior for some of the out-of-the-box scenarios that is available with WSO2 ESB. They are:

1. UT – Used if the proxy service is secured with Username Token security policy.
2. X509 – Used if the proxy service is secured with WS-Security policy which requires client certificate or secured with client authentication.
3. SAML – Used if the proxy service is secured with WS-Trust.
4. Kerberos Used if the proxy service is secured with Kerberos.

In addition to these, users can implement their own Entitlement Callback Handlers and plug them to the WSO2 ESB through the synapse configuration. This blog explains how to achieve this.

The properties available in the entitlement mediator can be found here. These properties define the default behavior of the Entitlement Mediator. Specific handlers override this behavior. The default UT handler, overrides the method how the subject-id attribute value is picked. It looks for the property by the name ‘username‘ in the Axis2 message context and sets its value as the XACML request subject-id value. This is because if UsernameToken security is enabled in ESB for a proxy service, once a user authenticates to this proxy service the username would be set to the Axis2 message context. Likewise the other handlers also look at various properties for values for the attributes and construct the XACML request. The following attribute IDs are used by the default handlers.

1. urn:oasis:names:tc:xacml:1.0:subject:subject-id of category urn:oasis:names:tc:xacml:1.0:subject-category:access-subject
2. urn:oasis:names:tc:xacml:1.0:action:action-id of category urn:oasis:names:tc:xacml:3.0:attribute-category:action
3. urn:oasis:names:tc:xacml:1.0:resource:resource-id of category urn:oasis:names:tc:xacml:3.0:attribute-category:resource
4. IssuerDN of category urn:oasis:names:tc:xacml:3.0:attribute-category:environment (used only by X509 handler)
5. SignatureAlgorithm of category urn:oasis:names:tc:xacml:3.0:attribute-category:environment (used only by X509 handler)

OAuth 2.0 with WSO2 Identity Server

The OAuth2.0 authorization delegation protocol has gained wide attraction among the API communities for its ease of implementation compared to OAuth1.0a which involves signatures. It provides specific authorization flows (grant types in OAuth2.0 terms) for web applications, mobile and native applications and browser based clients.

The WSO2 Identity Server has the capability to function as an OAuth2.0 Authorization server. It also supports OAuth1.0a but in this post I will only be talking about OAuth2.0 support.

One of the popular products from WSO2 is the WSO2 API Manager. This product uses the same OAuth2.0 feature from the WSO2 Identity Server (enabled by the componentized architecture of Carbon platform powered by OSGi).

The OAuth2.0 core specification defines 4 authorization grant types for 4 different use cases.

1. Authorization Code grant type

The OAuth1.0a core specification talks about only one type of authorization flow mainly, which is the 3-legged flow. Authorization Code grant type is pretty much the evolution of OAuth1.0a which eliminates the need for signatures and relies solely on transport layer security such as SSL. This is a 3-legged flow and is the recommended and dominantly used flow for web applications. This flow involves two separate requests. First to the authorization endpoint and secondly to the token endpoint. In the authorization endpoint the resource owner authenticates himself and authorizes the client for a specific scope for a specific time period. The authorization server would respond with an authorization code to a callback URL specified by the client. In the second step the client would authenticate himself to the token endpoint and exchange the authorization code to an access token.

2. Implicit grant type

This  grant type has only one request. That is the request to the authorization endpoint. The resource owner as in the previous grant type would authenticate himself at the authorization endpoint. However the second step in the previous request does not take place here, which is the authentication of the client. Therefore the clients that use this grant type are known as public or anonymous clients because they don’t authenticate themselves. This grant type is defined in case of clients that live on the browser such as JavaScript clients. Here the clients credentials are kept in the browser, therefore can be seen by the resource owner or anyone who has got access to the browser session. Hence the the clients credentials are not confidential anymore and authentication does not make sense. However the client_id must be sent which can be used for throttling and monitoring (SLA) purposes. This grant type should be used with care, at the discretion of the Authorization server.

3. Resource Owner Password Credentials grant type

This grant type again has only one request. But this one talks to the token endpoint. Here the resource owner would need to provide his credentials to the client and the client would send those credentials and his own credentials to the token endpoint and authenticate both parties with their respective credentials and get back the access token. This type of grant is useful in scenarios where there is no browsers involved such as mobile apps or Desktop apps. Again this type of grant should be allowed at the discretion of the authorization server, suitably for reasons such as if the client is not able to take advantage of the browser and the clients are generally trusted parties and are certified to be trusted by the resource owners.

4. Client Credentials grant type

This grant is the most simplest grant type available. In this case the client directly sends an access token request to the token endpoint with his credentials. There is no resource owner credentials involved here. I.e. the resource owners do not authenticate themselves or authorize clients. This grant type is generally used in cases where the resources are public or else if the client is accessing resources owned by himself. So why do we need an authentication scheme at all if the resources are public? Or why do we need OAuth2.0 instead of HTTP Basic Auth if the client is accessing his own resources? Well, the answer for the first question is to impose SLAs and do monitoring, metering and billing. The answer for the second question is that the OAuth2.0 model has some inherent advantages not found with HTTP Basic Auth. Mainly the client credentials are exchanged for an access token which is used thereafter and the client’s credentials can be safely discarded until the access token expires. Therefore the frequency at which the client’s credential is required is reduced. Also the scope and lifetime of access given to the access token can be controlled unlike giving away the credentials which means if the credentials are compromised the masqueraders who obtain the credentials have complete authority over the user’s account until such time the legitimate user comes to know this and changes his credentials at the authorization server. But even that might be too late if the masquerader has already changed the password of that account. In such a case the consequences can be devastating.

Once an access token is obtained regardless of the grant type used resource access is the same from the client’s perspective. The client would send a resource request with the access token to the resource server. The request path would ideally be intercepted by an entity which will validate the access token with the authorization server and decide whether to let the request to continue or not. How the access token validation happens between the authorization server and resource server is implementation specific of the authorization server which OAuth2.0 does not talk about. The WSO2 Identity Server currently exposes a SOAP endpoint for this purpose. The WSO2 API Manager provides a SOAP endpoint as well as a Thrift endpoint for this purpose.

The OAuth2.0 specification has provisions for extensions at many places. The authorization grants and access tokens types are two main examples. One of the earliest grant type additions to the OAuth2.0 specification is the “SAML 2.0 Profile for OAuth 2.0 Client Authentication and Authorization Grants”. Although this is still at a draft stage we can see all the major players in the industry adopting this standard mostly with some level of customization. The WSO2 Identity Server also has support for this since its 4.1.0 release. I have talked about this support here. Another such extension grant type which is also at a draft stage is the “JSON Web Token (JWT) Profile for OAuth 2.0 Client Authentication and Authorization Grants”. There are two major access token types that have been widely adopted in the industry; the “Bearer Token” (an IETF standard) and the “OAuth 2.0 Message Authentication Code (MAC) Tokens” (an IETF draft). The WSO2 Identity Server currently supports only the Bearer Token profile. For someone who wants an alternative for accessing resources with signatures, there is a customization available here.

Another complement to the OAuth2.0 specification is the “OAuth 2.0 Token Revocation” draft. The WSO2 Identity Server supports this draft as well from its 4.1.0 release, and you can read about it here.

The OAuth Bible is a comprehensive compilation of OAuth1.0a and 2.0 terminology and all their major flows.

OAuth Token Revocation with WSO2 Identity Server

The OAuth Token Revocation functionality is available with WSO2 Identity Server 4.1.0. The OAuth Token Revocation implementation follows the specification here. There are two endpoints exposed from the token revocation feature.

1. REST endpoint at /oauth2endpoints/revoke

2. SOAP endpoint at /services/OAuthAdminService with operation revokeAuthzForAppsByResourceOwner

The REST endpoint is for OAuth2.0 clients who want to revoke any access granted to them by a resource owner. This could be at the discretion of the resource owner or otherwise. In other words this endpoint is meant for OAuth2.0 clients only, to authenticate themselves using client_id and client_secret and revoke the authorization granted to them. They may use the access token or refresh token for this purpose. Whichever token the client uses the result is the same; the client cannot access the user’s resource again until such time the user explicitly provides his grant by authorizing the client at the OAuth2.0 authorization server.

Following is an example of the request that needs to be sent to the revocation REST endpoint by OAuth2.0 client to revoke a token:

curl -X POST --basic -u "4xTplVAiQEwrBF6wYSW3cpyqYDoa:GREoG5f80kmg7uHNed2YwfJSxlQa" -H "Content-Type: application/x-www-form-urlencoded;charset=UTF-8" -k -d "token=d23e96c9bf2818fe5b4db0f8dbe829bb&token_type_hint=access_token" https://localhost:9443/oauth2endpoints/revoke

The token parameter used here can be an access token or refresh token. The token_type_hint parameter is optional. This parameter can take values access_token or refresh_token. The Identity Server will use this parameter to speed up the process of looking up the token by searching first in the set of tokens the client specifies (access_token or refresh_token). If the token is not to be found in the set the client claims it to be in, then the server will look for the token in the other set (refresh_token or access_token).

The SOAP endpoint on the other hand is for the resource owners to directly interact with the Authorization server and revoke authorization grants for applications they previously granted access to, without the OAuth2.0 application/client being an intermediary in the process. The use of this SOAP endpoint is demonstrated by the WSO2 Identity Server’s management console at Configure’ -> My Authorized Apps‘ for resource owners to login and revoke application authorization.

Following is a screen shot of the ‘My Authorized Apps’ page at an instance when the user ‘ResourceOwner’ has granted authorization to the application ‘Playground2.0’ created by user ‘AppDev’.

Application 'Playground2.0' created by user 'AppDev' granted authorization by user 'ResourceOwner'

Application ‘Playground2.0’ created by user ‘AppDev’ granted authorization by user ‘ResourceOwner’

The token revocation end-point also supports CORS (Cross-Origin Resource Sharing) specification and also JSONP (Remote JSON – JSONP).

CORS is supported through CORS-Filter which can be found here. The CORS Filter is designed to be plugged to a webapp using its deployment descriptor (web.xml). Since the OAuth2.0 endpoints in WSO2 Identity Server have been written as JAX-RS endpoints you can add the required CORS configurations to its deployment descriptor. You can find this webapp at <WSO2_IS_HOME>/repository/deployment/server/webapps/oauth2endpoints.war. Rather than editing the web.xml directly in the deployed directory, its easier to copy the oauth2endpoints.war file into another location, edit the web.xml and copy it back into the webapps folder and it will get hot deployed.

Example of a JSONP revocation request:

curl -X POST --basic -u "4xTplVAiQEwrBF6wYSW3cpyqYDoa:GREoG5f80kmg7uHNed2YwfJSxlQa" -H "Content-Type: application/x-www-form-urlencoded;charset=UTF-8" -k -d "token=d23e96c9bf2818fe5b4db0f8dbe829bb&token_type_hint=access_token&callback=package.myCallback" https://localhost:9443/oauth2endpoints/revoke

The callback parameter is optional.