1818
1919import com .auth0 .jwt .JWT ;
2020import com .auth0 .jwt .JWTCreator ;
21+ import com .auth0 .jwt .JWTEncryptor ;
2122import com .auth0 .jwt .JWTVerifier ;
2223import com .auth0 .jwt .algorithms .Algorithm ;
24+ import com .auth0 .jwt .algorithms .CipherParams ;
2325import com .auth0 .jwt .exceptions .JWTDecodeException ;
2426import com .auth0 .jwt .exceptions .JWTVerificationException ;
2527import com .auth0 .jwt .exceptions .oicmsg_exceptions .JWKException ;
@@ -341,23 +343,43 @@ public void fromJwt(String jwt, KeyJar keyJar, String keyOwner,
341343 /**
342344 * Serialize the content of this instance (the claims map) into a jwt string.
343345 *
344- * @param key
346+ * @param signingKey
345347 * signing key
346348 * @param alg
347- * signing algorithm
349+ * signing algorithm name
348350 * @return message as jwt string.
351+ * @throws SerializationException
349352 */
350- public String toJwt (Key key , String alg )
353+ public String toJwt (Key signingKey , String alg ) throws SerializationException {
354+ return toJwt (signingKey , alg , null , null , null );
355+ }
356+ /**
357+ * Serialize the content of this instance (the claims map) into a jwt string.
358+ *
359+ * @param signingKey
360+ * signing key
361+ * @param alg
362+ * signing algorithm name
363+ * @param transportKey
364+ * key transport key, if null encryption is not done.
365+ * @param encAlg
366+ * key transport algorithm name. Must not be null if transportKey is set.
367+ * @param encEnc
368+ * content encryption algorithm name. Must not be null if transportKey is set.
369+ * @return message as jwt string.
370+ */
371+
372+ public String toJwt (Key signingKey , String alg , Key transportKey , String encAlg , String encEnc )
351373 throws SerializationException {
352374 header = new HashMap <String , Object >();
353375 header .put ("alg" , alg );
354376 header .put ("typ" , "JWT" );
355- if (key != null && key .getKid () != null ) {
356- header .put ("kid" , key .getKid ());
377+ if (signingKey != null && signingKey .getKid () != null ) {
378+ header .put ("kid" , signingKey .getKid ());
357379 }
358380 Algorithm algorithm = null ;
359381 try {
360- algorithm = AlgorithmResolver .resolveSigningAlgorithm (key , alg );
382+ algorithm = AlgorithmResolver .resolveSigningAlgorithm (signingKey , alg );
361383 } catch (IllegalArgumentException | ValueError | UnsupportedEncodingException
362384 | SerializationNotPossible e ) {
363385 throw new SerializationException (String
@@ -383,8 +405,24 @@ public String toJwt(Key key, String alg)
383405 }
384406
385407 }
386- return newBuilder .sign (algorithm );
387-
408+
409+ String signedJwt = newBuilder .sign (algorithm );
410+ if (transportKey == null ) {
411+ return signedJwt ;
412+ }
413+ if (encAlg == null || encEnc == null ) {
414+ throw new SerializationException (
415+ "encAlg and encEnc are mandatory parameters if transport key is set" );
416+ }
417+ try {
418+ return JWTEncryptor .init ().withPayload (signedJwt .getBytes ("UTF-8" )).encrypt (
419+ AlgorithmResolver .resolveKeyTransportAlgorithm (transportKey , encAlg ),
420+ Algorithm .getContentEncryptionAlg (encEnc , CipherParams .getInstance (encEnc )));
421+ } catch (UnsupportedEncodingException | ValueError | SerializationNotPossible e ) {
422+ throw new SerializationException (
423+ String .format ("Not able to initialize key transport algorithm '%s' to encrypt JWS, '%s'" ,
424+ encAlg , e .getMessage ()));
425+ }
388426 }
389427
390428 /**
0 commit comments