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.
cancel
Showing results for 
Search instead for 
Did you mean: 
miltonc
Product and Topic Expert
Product and Topic Expert
8,653
Github repository:

https://github.com/miltonchandradas/loggedinuserCF (Branch: part2)

 

Introduction:

  • 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


 

YouTube link:

https://youtu.be/4ol2zhGP2WQ

 

Conclusion:

  • This video should give you a clear idea on how to get the logged in user in SAP Cloud Platform Cloud Foundry environment

  • You should also have a good handle on how middleware functions work and how the JWT Tokens are used in the OAuth flows

  • If you have any questions or comments, please list them below and I will be happy to answer


 

Previous post:

https://blogs.sap.com/2020/08/23/how-to-consume-destinations-in-sap-cloud-platform-cloud-foundry/

 

Next post:

 

Related post:

https://blogs.sap.com/2020/08/23/how-to-get-logged-in-user-in-sap-cloud-platform-neo-environment/

 
5 Comments
kamal_kaur30
Discoverer
Hi Milton Chandradas,

Can you provide any reference how do we fetch logged in user details in case of managed approuter.

Thanks,

Kamalpreet Kaur
0 Kudos
Hi Milton,

Great blog. Any idea on how to achieve this in a CAP application with managed approuter.

 

Regards,

Parthi
shrijan_shrivastav
Associate
Associate
0 Kudos
Hi Milton Chandradas,

 

Thanks for such a nice blog. This blog really helped me in fetching User details from XSUAA service in CF.

Do we have also any way to fetch I/D number  from this service? Current service only return email and other user name attributes.
miltonc
Product and Topic Expert
Product and Topic Expert
0 Kudos
There is now a much easier way of getting logged in user details.  SAP has built a new API (much like the API in Neo environment).  So we don't have to do all this workaround to get logged in user details.

Will send the link shortly...
ragavendrabc
Explorer
0 Kudos
Hello Milton,

Please provide the link for getting the logged-in user details like userID, username etc.,

 

Thanks,

Ragavendra