How to get logged in user in SAP Cloud Platform (C...
Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
In this session, we will look at how to get the logged in user in SAP Cloud Platform (Cloud Foundry environment)
Synopsis:
SAP Cloud Platform can be set up with any Identity Provider that supports SAML
When building SAPUI5 applications, we can require that users be authenticated against this Identity Provider
There may be many instances where you might want to display user specific messages on the home page (for example, some form of welcome message) – and for this you need to know the logged in user
In Cloud Foundry, the user authentication is handled by the approuter which performs all the complex OAuth flows
Steps:
Unlike the Neo environment, there is no ready made userapi service provided by SAP Cloud Platform Cloud Foundry that provides the logged in user information
However, the Cloud Foundry provides us with the approuter that performs all the complex OAuth flows
We can add custom middleware functions that can query the request object and extract the user information inside of it
The user information itself is stored in a JWT Token that can be retrieved and decoded using a npm package (in our case, we are using jwt-decode)
Please note that we are not validating the JWT Token, but merely decoding it and getting the user information in the payload of the JWT Token
Check out https://jwt.io/ for more information on JWT Tokens