import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Date;
import java.security.GeneralSecurityException;
import java.security.KeyFactory;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.security.spec.PKCS8EncodedKeySpec;
import org.apache.commons.codec.binary.Base64;
import com.auth0.jwt.JWT;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.algorithms.Algorithm;
def Message processData(Message message) {
map = message.getProperties();
exp = map.get("exp");
aud = map.get("aud");
scope = map.get("scope");
iss = map.get("iss");
subject = map.get("subject");
accountNo = map.get("accountNo");
contentType = map.get("contentType");
accept = map.get("accept");
privateKey = map.get("privateKey");
publicKey = map.get("publicKey");
java.security.Security.addProvider(
new org.bouncycastle.jce.provider.BouncyCastleProvider()
);
// Read private key from property
RSAPrivateKey privKey = getPrivateKey(privateKey);
// Read public key from property
RSAPublicKey pubKey = getPublicKey(publicKey);
// Create RSA algorithm from keys
Algorithm algorithm = Algorithm.RSA256(pubKey, privKey);
// Get epoch time
long currentTimeMs = System.currentTimeMillis();
Date issuedAt = new Date(currentTimeMs);
// Get expiration time with validity of 1 hour
long expiryTimeMs = currentTimeMs + Integer.parseInt(exp) * 3600000;
Date expiresAt = new Date(expiryTimeMs);
// Create JWT for impersonation
String token = JWT.create()
.withIssuer(iss)
.withSubject(subject)
.withIssuedAt(issuedAt)
.withExpiresAt(expiresAt)
.withAudience(aud)
.withClaim("scope", scope)
.sign(algorithm);
message.setBody(token)
return message;
}
public static RSAPrivateKey getPrivateKey(String privKey) throws IOException, GeneralSecurityException {
return getPrivateKeyFromProperty(privKey);
}
public static RSAPrivateKey getPrivateKeyFromProperty(String key) throws IOException, GeneralSecurityException {
String privateKey = key;
privateKey = privateKey.replace("-----BEGIN RSA PRIVATE KEY-----", "");
privateKey = privateKey.replace("-----END RSA PRIVATE KEY-----", "");
byte[] encoded = Base64.decodeBase64(privateKey);
KeyFactory kf = KeyFactory.getInstance("RSA");
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded);
RSAPrivateKey privKey = (RSAPrivateKey) kf.generatePrivate(keySpec);
return privKey;
}
public static RSAPublicKey getPublicKey(String pubKey) throws IOException, GeneralSecurityException {
return getPublicKeyFromProperty(pubKey);
}
public static RSAPublicKey getPublicKeyFromProperty(String key) throws IOException, GeneralSecurityException {
String publicKey = key;
publicKey = publicKey.replace("-----BEGIN PUBLIC KEY-----", "");
publicKey = publicKey.replace("-----END PUBLIC KEY-----", "");
byte[] encoded = Base64.decodeBase64(publicKey);
KeyFactory kf = KeyFactory.getInstance("RSA");
RSAPublicKey pubKey = (RSAPublicKey) kf.generatePublic(new X509EncodedKeySpec(encoded));
return pubKey;
}
The following external jars has to be added in the resources section of the integration flow.
The integration flow has been configured and deployed successfully and now it is time to test our implementation from postman.
As seen above, the OAuth Bearer token has been generated with expiry duration of an hour successfully for authenticating Docusign APIs. Copy the generated JWT token from the monitoring.
Now lets analyze the JWT token generated by the script step with the help of jwt.io website.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
13 | |
10 | |
7 | |
7 | |
7 | |
6 | |
6 | |
5 | |
5 | |
5 |