Groovy Examples

Go to list of examples

Reading Credentials

Reading Credentials

You need to import SecureStoreService at the start of the script.

Then you can call getUserCredential with the credential name, which can be different from the username. Also beware that you need to convert returned password to string.

Externalize

You probably want to externalize credential name to be able to change it without editing the flow.

Exception Handling

You can check if secureStoreService or userCredential is null. But if you don't have any solution in the script, you may want to leave exception handling to flow itself.

Groovy IDE notice
You can't use SecureStoreService with GroovyIDE at the moment.

Script

Try it on Groovy IDE
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;

import com.sap.it.api.securestore.SecureStoreService;
import com.sap.it.api.securestore.UserCredential;
import com.sap.it.api.securestore.exception.SecureStoreException;
import com.sap.it.api.ITApiFactory;

def Message processData(Message message) {
  
  // Read from properties to make it more dynamic
  def mapProperties = message.getProperties();
  def credentialName = mapProperties.get("credential_name_property_key");

  // Credential specific code
  SecureStoreService secureStoreService = ITApiFactory.getService(SecureStoreService.class, null);
  UserCredential userCredential = secureStoreService.getUserCredential(credentialName);

  def user = userCredential.getUsername().toString()
  def pass = userCredential.getPassword().toString()

  message.setProperty("user", user);
  message.setProperty("pass", pass);
  return message;
}

Input Properties

Key Value
credential_name_property_key Partner1_SFTP

Expected Properties

Key Value
user My Username
pass My Password