Show current user details in Cloud Foundry apps with IAS

It’s always nice to show the name of the user in your app. Doing this in SCP applications requires a different implementation on CF compared to NEO. SAP provides a service for this in NEO but not in CF. Luckily, the following blog post helps a lot with this:

I tried this post and it works perfectly when using the default IDP from SAP but once you’re using your own IAS, it only shows the userid:

Although it can be solved easiIy it took me some time to find a solution. Therefore I’m sharing how to solve this. I came up with several solutions to fulfill all possible needs.

Solution 1: Changing the userid to mail, login name or something else.

In the first solution, I’m just going to change the userid to the mail of the user. I’m using mail as an example but their are other possibilities too, like login name, display name , … .

Changing this will be done in the IAS for the related application by following the next steps:

Go to your IAS -> open “Applications & Resources” -> “Applications”

Open the right application configuration -> Trust -> Click on “Subject Name Identifier”

Here you can change the attribute to the one that you want to display in your app. By default the IAS is using the User ID. This can be changed E-Mail, Display Name or anything you like in the provided list:

After changing to E-Mail you will get the mail address in the “getuserinfo”. (Like mentioned earlier, I’m using this blog for the implementation of “getuserinfo”: )

Display name will show the following:

You can also use “Advanced Configuration”. This will allow you to add a prefix and a suffix to the attribute but you can only use one attribute here.

You can find a list of possible attributes in SAP help:

Solution 2: Use JWT token to get more information of the user

The previous solution is an easy solution to switch the userid to the mail address or even the login name. In case you want to show more information of the user this is still not the ideal solution that you’re looking for. This solution will provide you a bit more information of the user next to the “Subject Name Identifier” from the IAS.

This solution is a small and simple extension on top of the already provided solution. By just decoding the JWT token you already receive more information of the user like the first and last name. Credits go to Dries Van Vaerenbergh for this one!

For decoding the JWT token I’m using the following npm package: “jwt-decode”

By including the package in your script and just one line you get more information of the user out of the JWT token: (full code project: )

const jwtDecode = require('jwt-decode'); var decodedJWTToken = jwtDecode(req.user.token.accessToken);

This will give us the following information about the user:

Solution 3: Get all user details from IAS

Last but not least,  if you really need all the information of the current user you can get it from IAS. This can be done by using the API from the IAS: 

Before using this API, you need enable the API authentication of your application in the IAS. You can also find this in the SAP help:

Go to your IAS, select the correct Application:

In the first tab you scroll down to API Authentication and click on “Client ID and Secrets”:

If not yet done, create a new secret. In case Client ID was empty, adding the first secret will generate one:

Here an example of getting the user information by using the email address in postman. For authentication we use the client id and secret:

If you want to use this API in our app you need to be aware that it’s not possible to do this directly in the UI because of security reasons. Even with a destination the API will force a redirect which will be blocked by CORS policy. For that reason, it needs to be implemented on the server side like for example in a nodejs or Java middleware. An easy way could be to implement it in the same function of the approuter that we’re using already. 

Besides enabling the API Authentication, you also need to create a destination in the SCP platform. Go to you subaccount → Destinations and create a new destination to the IAS

Use client id and secret in destination:

Next add the following code in the function “beforeRequestHandler” of the approuter:

Full code: 

rp({ uri: uaa_service.credentials.url + '/oauth/token', method: 'POST', headers: { 'Authorization': 'Basic ' + Buffer.from(sUaaCredentials).toString('base64'), 'Content-type': 'application/x-www-form-urlencoded' }, form: { 'client_id': dest_service.credentials.clientid, 'grant_type': 'client_credentials' } }).then(function (data) { const token = JSON.parse(data).access_token; return rp({ uri: dest_service.credentials.uri + '/destination-configuration/v1/destinations/' + sDestinationName, headers: { 'Authorization': 'Bearer ' + token } }); }).then(function (data) { const oDestination = JSON.parse(data); const token = oDestination.authTokens[0]; return rp({ method: 'GET', uri: oDestination.destinationConfiguration.URL + sEndpoint +, headers: { 'Authorization': `${token.type} ${token.value}` } }); }).then(function (xmldata) { parseString(xmldata, function (err, IASResult) { res.end(JSON.stringify({, user: req.user, decodedJWTToken: decodedJWTToken, iasData: IASResult })); }); }).catch(function (error) { res.end("error: " + JSON.stringify(error)); });

This code is based on the example of Marius Obert in his blog about the Destination Service in Cloud Foundry:

When running this service after deploying, you will get not only the first and lastname but also address information and everything else that’s filled in on IAS:

User information on IAS:

Full project can be found on GitHub:

Hope this will help you!

Greetings, Wouter!