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.