Blog Post

How to fetch User Credentials using Groovy Script in SAP CI?

August 22, 2023

Importance of User Credentials in SAP Cloud Integration

Understanding why user credentials are important in SAP CPI is necessary before diving into the technical details. User credentials are the authentication details people need to login to the system, and they are essential for limiting access to sensitive information and features.

User credentials minimise the risk of data breaches and unauthorised access in a cloud integration environment by guaranteeing that only authorised workers can connect with particular resources. Businesses may uphold a strong security posture and follow compliance regulations by securely retrieving and managing user credentials.

Steps to fetch the User Credentials

There are the following steps to fetch the user credentials which are:

Step 1: Create User Credentials

  1. Open the Cloud Integration in Monitor screen. In Security Material add the User Credentials.
  2. Provide the Username and Password and click on Deploy.

Step 2: Create the Integration Flow

  1. Open the Cloud Integration design mode and create the integration flow in the Edit mode.
  2. Add the Groovy Script and Content Modifier in the integration flow.

Step 3: Write the Groovy Script

Write the following Groovy Script:

"

// Import required classes from SAP CPI API and other Java packages

import com.sap.it.api.ITApi

import com.sap.it.api.ITApiFactory

import com.sap.it.api.securestore.*

import com.sap.gateway.ip.core.customdev.util.Message

import java.util.HashMap

// Define the custom data processing logic for the integration flow

def Message processData(Message message) {

   // Get the body of the incoming message

   def body = message.getBody();

   // Variables to store the retrieved username and password

   String password;

   String username;

   // Access the Secure Store service through the ITApiFactory

   def service = ITApiFactory.getApi(SecureStoreService.class, null);

   // Retrieve the user credential using the alias "ABT_CI_056" from the Security Material

   def credential = service.getUserCredential("ABT_CI_056");

   // Check if the credential was found in the Secure Store

   if (credential == null) {

       // If the credential is not found, throw an IllegalStateException with an error message

       throw new IllegalStateException("No credential found for alias 'ABT_CI_056'");

   } else {

       // If the credential is found, extract the username and password from it

       password = new String(credential.getPassword());

       username = new String(credential.getUsername());

   }

   // Set the retrieved username and password as message properties for further processing

   message.setProperty("PASSWORD", password);

   message.setProperty("USERNAME", username);

   // Return the modified message to continue with the integration flow

   return message;

}

"

In the Entire Script we have fetch the credentials from the Security material and stored them in the Exchange Property under the name of USERNAME and PASSWORD. Now to check the Username and Password in the Trace mode let use add the Content Modifier and read the data in the body.

Step 4: Content Modifier

  1. Add the Content Modifier in the Integration Flow and in the Message body add the Expression to see the property value as shown in the following image.

Step 5: Save and Deploy

  1. Save and Deploy the Integration Flow.
  2. Open the Monitor Message Processing in the Trace mode. You can see the Username and Password.

Summary

A strong and effective way to control access and security in SAP CI is to fetch user credentials using Groovy scripts. The procedures provided in this article can help organisations create a strong authentication system and protect their important data. In order to guarantee a seamless and secure user experience, keep in mind to prioritise security measures and stay current with industry best practises.

It's crucial to stay on top of security and performance issues in the quickly changing world of cloud integration. Businesses have the tools they need to maintain control and safeguard their most sensitive data when they utilise Groovy scripts for obtaining user credentials. Take advantage of Groovy scripting in SAP CI to advance your security procedures.

LinkedIn 
Forbes Technology Council, Official Member (2022)
LinkedIn
Forbes Technology Council, Official Member (2022)

About the Author

Jaspreet is an Executive Consultant with expertise in SAP, SaaS/Cloud Integrations, Cyber Security and Data Science. Jaspreet is hands-On Architect who does Pre-Sales, Solution Architecture, Development, Lead Delivery of Complex Integration programs, Manage disperse teams and Ensure successful Project Go-Live/Goals. He has made a lasting impact on global businesses IT projects including Aflac, Advanced Energy, Donnelley Financial Solutions(DFIN), Dell EMC and many more.

Do you want to
learn more about integration?

We are dedicated to make our knowledge accessible. You can either figure it out by yourself or you can let us give you a hand.

Let us take care of your integration.

We are SAP Certified and we can make your project happen. Explore our services and contact us. We will be happy to take on your project.

View Our Services