How to set key_ops in generating JWK using java script?

68 Views Asked by At

I want to generate JWK with parameters using JavaScript. Following is the code I used to generate JWK

var JA = java.util;
var JWKgen = Packages.com.nimbusds.jose.jwk.gen;
var Keys = Packages.com.nimbusds.jose.jwk.KeyUse;
var ops = Packages.com.nimbusds.jose.jwk.KeyOperation;
var alg = Packages.com.nimbusds.jose.Algorithm;

var RSAKeyjwk = JWKgen.RSAKeyGenerator(3072)
.keyUse(Keys.SIGNATURE) 
.keyID(JA.UUID.randomUUID().toString())
.algorithm(alg("RS384"))
.issueTime(new Date()) 
.generate();

I have added the keyUse, KeyId, algorithm as per the following link https://javadoc.io/static/com.nimbusds/nimbus-jose-jwt/6.7/com/nimbusds/jose/jwk/gen/JWKGenerator.html#keyOperations-java.util.Set-

I want to add "key_ops":["verify"],"ext": true in the JWK. I tried following syntax

keyOperations(Set <KeyOperation) - Method in class com.nimbusds.jose.jwk.gen.JWKGenerator Sets the operations (key_ops) of the JWK.

.keyOperations(Set(ops['VERIFY'])). 
.keyOperations(Set(ops.VERIFY))
.keyOperations(Set(ops("VERIFY")))

I am getting the following error

TypeError: Set objects may not be constructed using "new". TypeError: Set objects may not be constructed using "new".

Java constructor for "com.nimbusds.jose.jwk.KeyOperation" with arguments "string" not found.

Please help me on how to add the key_ops parameter.

Thanks

1

There are 1 best solutions below

0
HTech On

I got it solved with the help of my colleague. Here it is

var JA = java.util;

const verify = JA.Set(ops.parse(['verify']));


var RSAKeyjwk = JWKgen.RSAKeyGenerator(3072)
.keyUse(Keys.SIGNATURE)
.keyID(JA.UUID.randomUUID().toString())
.algorithm(alg("RS384"))
.keyOperations(verify)
.issueTime(new Date())
.generate();

logger.info(RSAKeyjwk);

still I couldnt add "ext": true no methods available in JWK gen.