Hi Sosy,
Error message is clear - missing dependency (library).
Anyway, I’ve simplified your code by generating RSA key dynamically (its non-sense but I just wanted to test it), copied required .jar file to WEB-INF/lib folder and it works for me. I tried both calling JSP directly as well from workflow.
Required JAR files that must be copied to WEB-INF/lib folder in Composer’s project:
accessors-smart-1.2.jar
asm-5.0.4.jar
jackson-core-2.9.9.jar
jcip-annotations-1.0-1.jar
json-smart-2.3.jar
nimbus-jose-jwt-7.7.jar
Below is code of “my” JSP page.
Input parameter: userCF
Output parameters: userCF, jwt, keyPrivate, keyPublic
<%@page language="java" contentType="application/json;charset=UTF-8" pageEncoding="UTF-8"%>
<%!
// Implement this method to execute some server-side logic.
public JSONObject performLogic(JSONObject state, Map<String, String> additionalParams) throws Exception {
// The state variable contains all variables from the application.
// If the application is a callflow, this consists of the variables in the Entry block
// of the callflow. If the application is a workflow, the project variables will
// be passed.
// For callflows, it's possible for the state variable to be null, if the
// passState option of the Backend block is false.
// Example of how to access data from the state object.
// Note that "Input1" is the name of the Input block and
// "Var1" is the name of the user-defined variable.
// String userInput = state.getString("Input1");
// String userVariable = state.getString("Var1");
// The additionalParams map contains any additional input parameters passed
// in the backend logic block.
// Example:
// Note that "Param1" is the parameter name of the input parameter.
// String param = additionalParams.get("Param1");
//String CF = "XXXXXXXXXXX";
String CF = additionalParams.get("userCF");
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(2048);
KeyPair kp = kpg.generateKeyPair();
RSAPublicKey publickeysig = (RSAPublicKey) kp.getPublic();
RSAPrivateKey privatekeysig = (RSAPrivateKey) kp.getPrivate();
Map<String, String> payload = new HashMap<String, String>();
payload.put("userCF", CF);
String jwtRequest = new String();
jwtRequest = buildBody(payload, privatekeysig, publickeysig);
JSONObject result = new JSONObject();
// Finally, this method must return a result object. Any values stored in
// this result object will be reassigned to output parameters defined in the voice application.
// For example, if the voice application declares an output parameter called "OutParam",
// the output parameter will get the value "Value".
// result.put("OutParam", "Value");
result.put("userCF",CF);
result.put("jwt",jwtRequest);
result.put("keyPublic",publickeysig.toString());
result.put("keyPrivate",privatekeysig.toString());
return result;
};
public static String buildBody(Object payload, RSAPrivateKey privatekeysig, RSAPublicKey publickeysig)
throws JsonProcessingException, JOSEException, NoSuchAlgorithmException, InvalidKeySpecException {
SignedJWT signedJWT = createJWS(payload, privatekeysig, publickeysig);
return signedJWT.serialize();
}
public static SignedJWT createJWS(Object payload, RSAPrivateKey privatekeysig, RSAPublicKey publickeysig)
throws JOSEException, NoSuchAlgorithmException, InvalidKeySpecException {
RSAKey sigKey;
sigKey = new RSAKey
// .Builder(privatekeysig.toRSAPublicKey())
.Builder(publickeysig)
// .privateKey(privatekeysig.toRSAPrivateKey())
.privateKey(privatekeysig).keyUse(KeyUse.SIGNATURE)
// .keyID(privSigKey.getKid())
.keyID("123").build();
JWSAlgorithm sigAlgorithm = JWSAlgorithm.RS256;
SignedJWT signedJWT = new SignedJWT(
new JWSHeader.Builder(sigAlgorithm).keyID(sigKey.getKeyID())
// .type(new JOSEObjectType("JWT"))
.build(),
new JWTClaimsSet.Builder().issueTime(new Date()).jwtID(UUID.randomUUID().toString())
.issuer("https://genesys-collaudo.it").claim("command-input", payload).build());
signedJWT.sign(new RSASSASigner(sigKey));
return signedJWT;
}
%>
<%@page import="java.security.KeyFactory"%>
<%@page import="java.security.KeyPair"%>
<%@page import="java.security.KeyPairGenerator"%>
<%@page import="java.security.NoSuchAlgorithmException"%>
<%@page import="java.security.interfaces.RSAPrivateKey"%>
<%@page import="java.security.interfaces.RSAPublicKey"%>
<%@page import="java.security.spec.InvalidKeySpecException"%>
<%@page import="java.security.spec.RSAPrivateCrtKeySpec"%>
<%@page import="java.security.spec.RSAPublicKeySpec"%>
<%@page import="java.util.Date"%>
<%@page import="java.util.UUID"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="com.fasterxml.jackson.core.JsonProcessingException"%>
<%@page import="com.nimbusds.jose.JOSEException"%>
<%@page import="com.nimbusds.jose.JWSAlgorithm"%>
<%@page import="com.nimbusds.jose.JWSHeader"%>
<%@page import="com.nimbusds.jose.crypto.RSASSASigner"%>
<%@page import="com.nimbusds.jose.jwk.KeyUse"%>
<%@page import="com.nimbusds.jose.jwk.RSAKey"%>
<%@page import="com.nimbusds.jwt.JWTClaimsSet"%>
<%@page import="com.nimbusds.jwt.SignedJWT"%>
<%@page import="com.nimbusds.jose.util.Base64URL"%>
<%-- GENERATED: DO NOT REMOVE --%>
<%@page import="org.json.JSONObject"%>
<%@page import="org.json.JSONException"%>
<%@page import="java.util.Map"%>
<%@include file="../include/backend.jspf" %>
R.