Author Topic: POST request using JWT in Genesys  (Read 4660 times)

Offline Sosy

  • Newbie
  • *
  • Posts: 44
  • Karma: 0
POST request using JWT in Genesys
« on: August 07, 2019, 03:28:37 PM »
Hi guys,
we've beek asked to send POST requests to a server using the JSON Web Token signed with a Private Key (corrisponding Public Key is shared with the server).

I was wondering if any of you has some experience with signed JWT and if there's a way to generate them in Genesys and sign them with the given Private Key (Composer/IRD).

We're currently able to generate the signed JWT using a JAVA Project in Eclipse on our local Windows machines but we cant figure out how to integrate this JAVA project in Genesys.

We're trying to use the Backend Block to call a .jsp page to execute the code but this project needs some external libraries: we put the .jar files (that contain the libraries) in the WEB-INF/lib directory of the project and then used the <%@ import page="java.security..."%> to use them but
as soon as we invoke a class that is contained within the imported external libraries the test application crashes.

Any ideas?

Thanks in advance!


Offline René

  • Administrator
  • Hero Member
  • *****
  • Posts: 1832
  • Karma: 62
Re: POST request using JWT in Genesys
« Reply #1 on: August 07, 2019, 05:09:21 PM »
Hi,

I used custom JSP pages several times without any issues. What kind of error do you get?

R.

Offline cavagnaro

  • Administrator
  • Hero Member
  • *****
  • Posts: 7641
  • Karma: 56330
Re: POST request using JWT in Genesys
« Reply #2 on: August 07, 2019, 05:16:10 PM »
What does Tomcat logs say? Be sure to use the correct java (32 or 64) and that libraries are compatible with Java that Tomcat is using.

Offline Sosy

  • Newbie
  • *
  • Posts: 44
  • Karma: 0
Re: POST request using JWT in Genesys
« Reply #3 on: August 08, 2019, 01:32:41 PM »
Hi everyone,

first, as always, thanks for your replies.

For this application, I need to define a variable having the RSAPrivateKey Type that is defined within the java.security.interfaces.RSAPrivateKey library.
So I added the rt.jar file (that contains the library) in the project WEB-INF/lib folder.

Then I imported the library in the jsp page using <%@ import page="java.security.interfaces.RSAPrivateKey"@>.

As soon as I declare my variable in the code within the jsp page and try to run my application it crashes exiting from the Entry Block exception.

[quote]RSAPrivateKey privatekeysig = chiavePrivata(N, E, D, P, Q, Dp, Dq, Qi); [/quote]

Here's MCP logs for that:
[quote]
2019-08-08T14:41:13.756  Interaction  T-CTI-POM-WSV08  MCP_TO_2_TEST  GCTI-145-50043  007A01D4-1000A58F 7340 fetch_end Fail (HTTP error response 500):http://10.207.173.205:8080/117039_ProvaLibrerie/src/PROVA.jsp
2019-08-08T14:41:13.756  Standard  T-CTI-POM-WSV08  MCP_TO_2_TEST  GCTI-145-40027  EROR 007A01D4-1000A58F 7340 0C0003F9 Fetch error on url http://10.207.173.205:8080/117039_ProvaLibrerie/src/PROVA.jsp, reason HTTP error response 500, callid 007A01D4-1000A58[/quote]

About Tomcat, I noticed that the sdtout log is not "working properly" as it only says:

[quote]2019-08-08 12:31:26 Commons Daemon procrun stdout initialized[/quote]

What can it be? I remember once we modified the Java Option in Tomcat and we ended up with the same situation but we restored the older ones and the logs worked again...

Here's the Java Options:

[quote]-Dcatalina.home=C:\Tomcat 7.0
-Dcatalina.base=C:\Tomcat 7.0
-Djava.endorsed.dirs=C:\Tomcat 7.0\endorsed
-Djava.io.tmpdir=C:\Tomcat 7.0\temp
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
-Djava.util.logging.config.file=C:\Tomcat 7.0\conf\logging.properties
-Djavax.net.debug=all
-XX:MaxPermSize=128m[/quote]


Thanks



Offline René

  • Administrator
  • Hero Member
  • *****
  • Posts: 1832
  • Karma: 62
Re: POST request using JWT in Genesys
« Reply #4 on: August 08, 2019, 02:46:02 PM »
Hi Sosy,

Error is related to your Java code (JSP page) and has nothing to do with Composer project. You would need to fix the code prior integrating it with your Composer project.

BTW Feel free to post your Java code here and we can have a look.

R.

Offline Sosy

  • Newbie
  • *
  • Posts: 44
  • Karma: 0
Re: POST request using JWT in Genesys
« Reply #5 on: August 08, 2019, 03:08:09 PM »
Hi Renč, here's the code:

[quote]import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPrivateCrtKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.util.Date;
import java.util.UUID;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.nimbusds.jose.JOSEException;
import com.nimbusds.jose.JWSAlgorithm;
import com.nimbusds.jose.JWSHeader;
import com.nimbusds.jose.crypto.RSASSASigner;
import com.nimbusds.jose.jwk.KeyUse;
import com.nimbusds.jose.jwk.RSAKey;
import com.nimbusds.jwt.JWTClaimsSet;
import com.nimbusds.jwt.SignedJWT;
import com.nimbusds.jose.util.Base64URL;

public class testJSP {
public static void main(String args[]) throws Throwable {

String CF = "XXXXXXXXXXX";
//String[] setX5c = {"xxxx"};
String N = "xxx";
String E = "AQAB";
String D = "xxx";
String P = "xxx";
String Q = "xxx";
String Dp = "xxx";
String Dq = "xxx";
String Qi = "xxx";

RSAPrivateKey privatekeysig = chiavePrivata(N, E, D, P, Q, Dp, Dq, Qi);
RSAPublicKey publickeysig = chiavePubblica(N, E);
           
        Map<String,String> payload = new HashMap<String,String>();
        payload.put("userCF", CF);
       
        String jwtRequest = new String();
       
        jwtRequest = buildBody(payload, privatekeysig, publickeysig);
       
        System.out.println(jwtRequest);
       
}

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("e47a600c-fdf2-469b-befb-xxxxxxxx")
                .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;                 
             
}

public static RSAPrivateKey chiavePrivata(String N, String E, String D, String P, String Q, String Dp, String Dq, String Qi)throws NoSuchAlgorithmException, InvalidKeySpecException{
        RSAPrivateCrtKeySpec spec=new RSAPrivateCrtKeySpec(
                      new Base64URL(N).decodeToBigInteger(), // arg0 - the modulus n
                      new Base64URL(E).decodeToBigInteger(), // arg1 - the public exponent e
                      new Base64URL(D).decodeToBigInteger(), // arg2 - the private exponent d
                      new Base64URL(P).decodeToBigInteger(), // arg3 - the prime factor p of n
                      new Base64URL(Q).decodeToBigInteger(), // arg4 - the prime factor q of n
                      new Base64URL(Dp).decodeToBigInteger(),// arg5 - this is d mod (p-1)
                      new Base64URL(Dq).decodeToBigInteger(),// arg6 - this is d mod (q-1)
                      new Base64URL(Qi).decodeToBigInteger() // arg7 - the Chinese Remainder Theorem coefficient q-1 mod p
                      );
        return (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(spec);
    }

public static RSAPublicKey chiavePubblica(String N, String E) throws NoSuchAlgorithmException, InvalidKeySpecException{
        RSAPublicKeySpec spec=new RSAPublicKeySpec(
                                new Base64URL(N).decodeToBigInteger(), // arg0 - the modulus n
                                new Base64URL(E).decodeToBigInteger()  // arg1 - the public exponent e
                              );
        return (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(spec);
    }             
}   

[/quote]

The program return a JWT that is syntactically correct as I copy and paste it in Postman, sending a POST request, I get the expected response...

ps. I'm not trying to  integrate the main method as I'm using the method provided in the .jsp page.
« Last Edit: August 08, 2019, 03:15:47 PM by Sosy »

Offline René

  • Administrator
  • Hero Member
  • *****
  • Posts: 1832
  • Karma: 62
Re: POST request using JWT in Genesys
« Reply #6 on: August 08, 2019, 03:31:55 PM »
Is this code of your JSP page or just a sample? JSP page called using backend block requires specific format...

Offline Sosy

  • Newbie
  • *
  • Posts: 44
  • Karma: 0
Re: POST request using JWT in Genesys
« Reply #7 on: August 08, 2019, 03:48:18 PM »
This is the Java Code we use in Eclipse to generate the JWT (in order to test the POST request).

Here's the .jsp code We're trying to implement (we're dong it little by little):

[quote]<%@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
{
JSONObject result = new JSONObject();
String CF = "XXXXXXXXXXX";

String N = "xxx";
String E = "AQAB";
String D = "xxx";
String P = "xxx";
String Q = "xxx";
String Dp = "xxx";
String Dq = "xxx";
String Qi = "xxx";


RSAPrivateKey privatekeysig = chiavePrivata(N, E, D, P, Q, Dp, Dq, Qi);
String x = "Success";

result.put("OUT_out", x);

return result;
};

public static RSAPrivateKey chiavePrivata(String N, String E, String D, String P, String Q, String Dp, String Dq, String Qi)throws NoSuchAlgorithmException, InvalidKeySpecException{
      RSAPrivateCrtKeySpec spec=new RSAPrivateCrtKeySpec(
                    new Base64URL(N).decodeToBigInteger(), // arg0 - the modulus n
                    new Base64URL(E).decodeToBigInteger(), // arg1 - the public exponent e
                    new Base64URL(D).decodeToBigInteger(), // arg2 - the private exponent d
                    new Base64URL(P).decodeToBigInteger(), // arg3 - the prime factor p of n
                    new Base64URL(Q).decodeToBigInteger(), // arg4 - the prime factor q of n
                    new Base64URL(Dp).decodeToBigInteger(),// arg5 - this is d mod (p-1)
                    new Base64URL(Dq).decodeToBigInteger(),// arg6 - this is d mod (q-1)
                    new Base64URL(Qi).decodeToBigInteger() // arg7 - the Chinese Remainder Theorem coefficient q-1 mod p
                    );
      return (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(spec); 
};
%>
<%-- GENERATED: DO NOT REMOVE --%>
<%@page import="java.io.BufferedWriter"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.File"%>
<%@page import="java.io.FileWriter"%>
<%@page import="java.io.FileReader"%>
<%@page import="java.io.IOException"%>
<%@page import="java.io.Writer"%>
<%@page import="java.text.DateFormat"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Calendar"%>
<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>

<%@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.jose.util.Base64URL"%>
<%@page import="com.nimbusds.jwt.JWTClaimsSet"%>
<%@page import="com.nimbusds.jwt.SignedJWT"%> 

<%@page import="java.security.KeyFactory"%>
<%@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="com.fasterxml.jackson.core.JsonProcessingException"%>
<%@include file="../include/backend.jspf" %>[/quote]

As I said, if I declare the variable and call the method [i]RSAPrivateKey privatekeysig = chiavePrivata(N, E, D, P, Q, Dp, Dq, Qi);[/i] I got the error. If I comment that line no error is thrown.

Sorry if it is bad coding, we never wrote customized jsp backend before... Apart from coding errors, do you think it will ever be possibile to use that Java code in Composer through JSP?
« Last Edit: August 08, 2019, 03:49:56 PM by Sosy »

Offline René

  • Administrator
  • Hero Member
  • *****
  • Posts: 1832
  • Karma: 62
Re: POST request using JWT in Genesys
« Reply #8 on: August 08, 2019, 04:32:01 PM »
It would take me a while to make your code working in my lab.

What error do you get when you call JSP directly?

Offline Sosy

  • Newbie
  • *
  • Posts: 44
  • Karma: 0
Re: POST request using JWT in Genesys
« Reply #9 on: August 08, 2019, 06:04:59 PM »
Hi Renč,

this is what I got calling JSP directly from browser:

[quote]HTTP Status 500 - An exception occurred processing JSP page /src/PROVA_bck.jsp at line 43


type Exception report

message An exception occurred processing JSP page /src/PROVA_bck.jsp at line 43

description The server encountered an internal error that prevented it from fulfilling this request.

exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /src/PROVA_bck.jsp at line 43

40:       
41: public static RSAPrivateKey chiavePrivata(String N, String E, String D, String P, String Q, String Dp, String Dq, String Qi)throws NoSuchAlgorithmException, InvalidKeySpecException{
42: RSAPrivateCrtKeySpec spec = new RSAPrivateCrtKeySpec(
43: new Base64URL(N).decodeToBigInteger(), // arg0 - the modulus n
44: new Base64URL(E).decodeToBigInteger(), // arg1 - the public exponent e
45: new Base64URL(D).decodeToBigInteger(), // arg2 - the private exponent d
46: new Base64URL(P).decodeToBigInteger(), // arg3 - the prime factor p of n


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:575)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:462)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)



root cause
javax.servlet.ServletException: java.lang.NoClassDefFoundError: net/minidev/json/JSONAware
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:916)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:845)
org.apache.jsp.src.PROVA_005fbck_jsp._jspService(PROVA_005fbck_jsp.java:276)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)



root cause
java.lang.NoClassDefFoundError: net/minidev/json/JSONAware
java.lang.ClassLoader.defineClass1(Native Method)
java.lang.ClassLoader.defineClass(Unknown Source)
java.security.SecureClassLoader.defineClass(Unknown Source)
org.apache.catalina.loader.WebappClassLoaderBase.findClassInternal(WebappClassLoaderBase.java:3114)
org.apache.catalina.loader.WebappClassLoaderBase.findClass(WebappClassLoaderBase.java:1342)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1823)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1703)
java.lang.ClassLoader.defineClass1(Native Method)
java.lang.ClassLoader.defineClass(Unknown Source)
java.security.SecureClassLoader.defineClass(Unknown Source)
org.apache.catalina.loader.WebappClassLoaderBase.findClassInternal(WebappClassLoaderBase.java:3114)
org.apache.catalina.loader.WebappClassLoaderBase.findClass(WebappClassLoaderBase.java:1342)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1823)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1703)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:126)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:63)
org.apache.jsp.src.PROVA_005fbck_jsp.chiavePrivata(PROVA_005fbck_jsp.java:93)
org.apache.jsp.src.PROVA_005fbck_jsp.performLogic(PROVA_005fbck_jsp.java:69)
org.apache.jsp.src.PROVA_005fbck_jsp._jspService(PROVA_005fbck_jsp.java:242)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)



root cause
java.lang.ClassNotFoundException: net.minidev.json.JSONAware
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1854)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1703)
java.lang.ClassLoader.defineClass1(Native Method)
java.lang.ClassLoader.defineClass(Unknown Source)
java.security.SecureClassLoader.defineClass(Unknown Source)
org.apache.catalina.loader.WebappClassLoaderBase.findClassInternal(WebappClassLoaderBase.java:3114)
org.apache.catalina.loader.WebappClassLoaderBase.findClass(WebappClassLoaderBase.java:1342)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1823)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1703)
java.lang.ClassLoader.defineClass1(Native Method)
java.lang.ClassLoader.defineClass(Unknown Source)
java.security.SecureClassLoader.defineClass(Unknown Source)
org.apache.catalina.loader.WebappClassLoaderBase.findClassInternal(WebappClassLoaderBase.java:3114)
org.apache.catalina.loader.WebappClassLoaderBase.findClass(WebappClassLoaderBase.java:1342)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1823)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1703)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:126)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:63)
org.apache.jsp.src.PROVA_005fbck_jsp.chiavePrivata(PROVA_005fbck_jsp.java:93)
org.apache.jsp.src.PROVA_005fbck_jsp.performLogic(PROVA_005fbck_jsp.java:69)
org.apache.jsp.src.PROVA_005fbck_jsp._jspService(PROVA_005fbck_jsp.java:242)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)



note The full stack trace of the root cause is available in the Apache Tomcat/7.0.65 logs.
[/quote]

Offline René

  • Administrator
  • Hero Member
  • *****
  • Posts: 1832
  • Karma: 62
Re: POST request using JWT in Genesys
« Reply #10 on: August 08, 2019, 06:31:55 PM »
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:
[code]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
[/code]

Below is code of "my" JSP page.
Input parameter: userCF
Output parameters: userCF, jwt, keyPrivate, keyPublic

[code]<%@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" %>[/code]

R.

Offline Sosy

  • Newbie
  • *
  • Posts: 44
  • Karma: 0
Re: POST request using JWT in Genesys
« Reply #11 on: August 09, 2019, 11:11:12 AM »
Hi Renč!

THANK YOU! We finally did it!

The problem here was that we didn't understand how the code worked so we didn't put in the WEB-INF/lib folder ALL the .jar file needed.

Your answer rang a bell in my brain so I looked deeper in my code and took all the libraries that I was missing.

Offline René

  • Administrator
  • Hero Member
  • *****
  • Posts: 1832
  • Karma: 62
Re: POST request using JWT in Genesys
« Reply #12 on: August 09, 2019, 12:34:46 PM »
Hi Sosy,

Good to hear it works for you!

R.