Securing Service Bus REST Services Using OAuth2
Securing Service Bus REST Services Using OAuth2
Maarten Smeets
AMIS
CONTENTS
This manual described how you can secure Service Bus REST services with OAuth2 and configure role based
authorization without having to install/license additional products (such as Oracle API Platform, Oracle
Entitlements Server, Oracle Access Manager). Of course this solution does not provide all the features of the
mentioned products but if you do not need them, this might be a viable alternative.
This manual describes the installation, usage and test process. It does not explain the choices made and code
written. For this check out my blog posts at: https://technology.amis.nl/author/maarten-smeets/
This is a JDeveloper 12.2.1.3 application. Just do recursive search/replace in files (Notepad++ or JDev can do
this) of 12.2.1.3 with a previous version if you require a jpr/jws for a 12.2.1 version older than 12.2.1.3. For
ease of use, also jars, wars, zips have been committed which can be used as-is (no build process required if the
default configuration is sufficient).
Create a group tokenusers and put a user in this group. This user is allowed to obtain tokens.
This keystore needs to be policy based since OWSM, and thus OWSM policies, do not support password based
KSS keystores.
Open the keystore and click Manage. Next click Generate keypair. In a production situation you would want to
import your own keypair signed with your own certificate authority here.
Make sure the managed server (or managed server cluster) is the only target! Default Next, Next, Next, Next,
Finish.
The WAR file contains a web.xml file and does not enforce TLS. Since sending basic authentication credentials
and tokens over HTTP is dangerous (can easily be sniffed), you might want to update the web.xml file to
enforce TLS if you can host it (<transport-guarantee>CONFIDENTIAL</transport-guarantee>). If you do TLS
offloading before traffic reaches the WebLogic Server of course this is not needed.
GRANT THE TOKEN SERVICE ACCESS TO THE KEYSTORE.
Login to the EM Fusion Middleware Control and open op the system policies page.
Create a new policy
This is the location of the token service deployment. The parameters will be replaced by WebLogic.
When something goes wrong, no token is returned. You will then get a response like:
{
"access_token": "",
"scope": "read write",
"token_type": "Bearer",
"expires_in": 0
}
CURL
You can test the token service with curl:
POSTMAN TIPS/TRICKS
You can also test the token service with Postman (can be downloaded from: https://www.getpostman.com/)
Postman has a build-in console which allows you to analyse requests and responses in detail
When using Postman to access TLS secured services, you might not see responses in the console. Postman
checks if (public) certificates are valid. This check can be disabled:
Postman has built-in support for OAuth2. You can also ‘manually’ do a request
An HTTP POST request on the token service URL with Basic Authentication. The username and password used
should be the same as for the user which was created and put in the group tokenusers. In this example
tokenuser.
Above you can see a valid response. The access token is a JWT token consisting of 3 parts. A header, a body
and a signature. The header and body are Base64 encoded and the signature is encrypted with the previously
created keypair. The parts are separated with a ‘.’. Header and body can be Base64 decoded with for example
https://www.base64decode.org/.
{"kid":"oauth2keypair","alg":"RS256"}
kid is a hint to the party receiving the token on which key alias they can use to decode the token. Alg is the
algorithm which was used to create the signature.
{"sub":"tokenuser","iss":"www.oracle.com","exp":1540547266,"iat":1540546666
}
‘sub’ indicated the subject for who the token was created. You should check that this is the same as the user
used in the Basic Authentication request. ‘iss’ indicates the token issuer. ‘www.oracle.com’ is trusted by
default by the predefined policies. ‘iat’ indicates the issue date (epoch) of the token. You can transform this to
a date with for example www.epochconverter.com
‘exp’ indicates expiry time. When the current time is past the expiry time, the token can no longer be used.
Indicate Grant Type Client Credentials, specify the Access Token URL and use the previously created username
and password as Client ID and Client Secret respectively. The token service does not do anything with the
scope yet. The Client Authentication should be ‘Send as Basic Auth header’.
When requesting a token, a valid response would be something like is shown below. When you scroll down
you have a ‘Use Token’ button to add the token to the request of a service call (HTTP header Authorization
content: Bearer token (in which token is replaced with the actual token))
DEPLOY THE CUSTOM OWSM POLICY FOR ROLE BASED ACCESS
Patch 24669800: Unable to configure Custom OWSM policy for OSB REST Services
This file needs to be put in the WLS_DOMAIN/lib folder. In my case with the Integrated WebLogic server this
was: C:\Users\maart\AppData\Roaming\JDeveloper\system12.2.1.3.42.170820.0914\DefaultDomain\lib
After having done this, restart the domain to make the policy available.
These steps are required to secure services and provide role based access. The service in this example is an
Oracle Service Bus REST service.
Create a session
When using TLS for incoming messages on the Service Bus attach
oracle/http_jwt_token_over_ssl_service_policy else attach oracle/http_jwt_token_service_policy
Open Postman
Go to the Authorization tab. Type OAuth 2, ‘Add Authorization data to Request Headers’
Use Token
Send a request
- Try to request a token for a user not in the tokenusers group. The tokenservice should not generate a
token for such a user.
- Try to use a token of a user which is not in a role specified as configuration parameter of the custom
policy. The reply should indicate Unauthorized. HTTP status code 401
- Try specifying several principles and a user having for example only the last one. The user should be
able to access the service with the token.
- Try to manipulate the token. E.g. decode the body, replace the subject, encode the body. The JWT
token validation should fail.
GOOD TO KNOW
ADDITIONAL LOGGING
The OAuth2 token service and custom OWSM policy both use the ADF logger. Log messages will appear in the
servers diagnostics log files. You can increase the log level using the normal EM log configuration settings:
WHICH SETTING IS LOCATED WHERE
THE STRIPE, KEYSTORE NAME, KEY ALIAS, EXPIRATION TIME (TOKEN VALIDITY)
These can be configured in
TokenService\src\nl\amis\services\oauth2\tokenservice\TokenApplication.properties
Stripe owsm with keystore keystore are used by default by the predefined OWSM policies
oracle/http_jwt_token_over_ssl_service_policy and oracle/http_jwt_token_service_policy. Should you wish to
change them, you need to override the policy configuration and define credential store framework entries.
Mind that the meaning of the parameters and credential store framework entries differ for KSS and JKS
keystores. OWSM does not support KSS keystores with passwords. Detail on how to do the is out of scope for
this manual. See for more information: https://technology.amis.nl/2017/09/24/oracle-soa-suite-and-weblogic-
overview-of-key-and-keystore-configuration/.
The key alias is also the kid in the header of the JWT token and will be used by the party validating the token to
obtain the public key in order to use it to verify the signature.
After you change the properties file and do a redeploy, sometimes the new properties are not picked up.
Restart the
web.xml
- The security role tokenusers maps to the WebLogic principle tokenusers. This is the name of a group
defined in the WebLogic security realm.
The token will be generated for the user who did the Basic Authentication request. This user is visible as sub
(Subject) in the body of the token.
A JAR file
- This JAR contains the Java code of the policy. The Java code uses the parameters defined in the file
below.
- a policy-config.xml file. This file indicates which class is implementing the policy. Important part of this
file is the reference to restUserAssertion. This maps to an entry in the file below
The policy description file contains an element which maps to the entry in the policy-config.xml file. Also the
ZIP file has a structure which is in line with the name and Id of the policy. It is like;
Thus the name of the policy is CUSTOM/rest_user_assertion_policy
This name is also part of the contents of the rest_user_assertion_policy file. You can also see there is again a
reference to the implementation class and the restUserAssertion element which is in the policy-config.xml file
is also there. The capabilities of the policy are mentioned in the restUserAssertion attributes.
Suppose you want to expand the capabilities of the policy, you can use the WSM Policies screen to create a
policy like one of the standard policies which has the capability you want.
Next you can export that newly created policy. It is not possible to Export the predefined policies as you can
see in the above screenshot. This only works for policies in the Security category. This will give you the policy
description file which contains the policies capabilities. You can then copy these (attributes) into your own
custom policy.