Skip to content

Commit

Permalink
Allow to build JWT from JSON string (#820)
Browse files Browse the repository at this point in the history
  • Loading branch information
sberyozkin authored Aug 20, 2024
1 parent 0798f11 commit 5cde65d
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ public static JwtClaimsBuilder claims(String jsonLocation) {
return JwtProvider.provider().claims(jsonLocation);
}

/**
* Creates a new instance of {@link JwtClaimsBuilder} from a JSON string.
*
* @param json JSON string
* @return {@link JwtClaimsBuilder}
*/
public static JwtClaimsBuilder claimsJson(String json) {
return JwtProvider.provider().claimsJson(json);
}

/**
* Creates a new instance of {@link JwtClaimsBuilder} from {@link JsonWebToken}.
*
Expand Down Expand Up @@ -309,6 +319,23 @@ public static String sign(JsonObject jsonObject) {
return claims(jsonObject).sign();
}

/**
* Sign the claims from a JSON string using 'RS256' algorithm with a private RSA key
* loaded from the location set with the "smallrye.jwt.sign.key-location".
* Private RSA key of size 2048 bits or larger MUST be used.
*
* The 'iat' (issued at time), 'exp' (expiration time) and 'jit' (unique token identifier) claims
* will be set and the `iss` issuer claim may be set by the implementation unless they have already been set.
* See {@link JwtClaimsBuilder} description for more information.
*
* @param json JSON string
* @return signed JWT token
* @throws JwtSignatureException the exception if the signing operation has failed
*/
public static String signJson(String json) {
return claimsJson(json).sign();
}

/**
* Encrypt the claims loaded from a JSON resource using 'RSA-OAEP-256' algorithm with a public RSA key
* loaded from the location set with the "smallrye.jwt.encrypt.key-location".
Expand Down Expand Up @@ -362,6 +389,23 @@ public static String encrypt(JsonObject jsonObject) {
return claims(jsonObject).jwe().encrypt();
}

/**
* Encrypt the claims from a JSON string using 'RSA-OAEP-256' algorithm with a public RSA key
* loaded from the location set with the "smallrye.jwt.encrypt.key-location".
* Public RSA key of size 2048 bits or larger MUST be used.
*
* The 'iat' (issued at time), 'exp' (expiration time) and 'jit' (unique token identifier) claims
* will be set and the `iss` issuer claim may be set by the implementation unless they have already been set.
* See {@link JwtClaimsBuilder} description for more information.
*
* @param json JSON string
* @return encrypted JWT token
* @throws JwtEncryptionException the exception if the encryption operation has failed
*/
public static String encryptJson(String json) {
return claimsJson(json).jwe().encrypt();
}

/**
* Sign the claims loaded from a JSON resource using 'RS256' algorithm with a private RSA key
* loaded from the location set with the "smallrye.jwt.sign.key-location" and encrypt the inner JWT using
Expand Down Expand Up @@ -417,4 +461,22 @@ public static String innerSignAndEncrypt(Map<String, Object> claims) {
public static String innerSignAndEncrypt(JsonObject jsonObject) {
return claims(jsonObject).innerSign().encrypt();
}

/**
* Sign the claims from a JSON string using 'RS256' algorithm with a private RSA key
* loaded from the location set with the "smallrye.jwt.sign.key-location" and encrypt the inner JWT using
* 'RSA-OAEP-256' algorithm with a public RSA key loaded from the location set with the "smallrye.jwt.encrypt.key-location".
* Public RSA key of size 2048 bits or larger MUST be used.
*
* The 'iat' (issued at time), 'exp' (expiration time) and 'jit' (unique token identifier) claims
* will be set and the `iss` issuer claim may be set by the implementation unless they have already been set.
* See {@link JwtClaimsBuilder} description for more information.
*
* @param json JSON string
* @return encrypted JWT token
* @throws JwtEncryptionException the exception if the encryption operation has failed
*/
public static String innerSignAndEncryptJson(String json) {
return claimsJson(json).innerSign().encrypt();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ static JwtClaims parseJwtClaims(String jwtLocation) {
}
}

static JwtClaims parseJwtContent(String jwtContent) {
try {
return JwtClaims.parse(jwtContent);
} catch (Exception ex) {
throw ImplMessages.msg.failureToParseJWTClaims(ex.getMessage(), ex);
}
}

static PrivateKey readPrivateKeyFromKeystore(String keyStorePath) {
Optional<String> keyStorePassword = getOptionalConfigProperty(KEYSTORE_PASSWORD, String.class);
if (keyStorePassword.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ class JwtClaimsBuilderImpl extends JwtSignatureImpl implements JwtClaimsBuilder,
super(fromMapToJwtClaims(claimsMap));
}

JwtClaimsBuilderImpl(JwtClaims claims) {
super(claims);
}

private static JwtClaims fromMapToJwtClaims(Map<String, Object> claimsMap) {
JwtClaims claims = new JwtClaims();
@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ public JwtClaimsBuilder claims(String jsonLocation) {
return new JwtClaimsBuilderImpl(jsonLocation);
}

/**
* {@inheritDoc}
*/
@Override
public JwtClaimsBuilder claimsJson(String json) {
return new JwtClaimsBuilderImpl(JwtBuildUtils.parseJwtContent(json));
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ private static JwtProvider getProvider() {
*/
public abstract JwtClaimsBuilder claims(String jsonLocation);

/**
* Creates a new instance of {@link JwtClaimsBuilder} from a JSON string.
*
* @param json JSON string
* @return {@link JwtClaimsBuilder}
*/
public abstract JwtClaimsBuilder claimsJson(String json);

/**
* Creates a new instance of {@link JwtClaimsBuilder} from {@link JsonWebToken}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,21 @@ void encryptMapOfClaimsShortcut() throws Exception {
doTestEncryptedClaims(jweCompact);
}

@Test
void encryptJsonString() throws Exception {
String jweCompact = Jwt.claimsJson("{\"customClaim\":\"custom-value\"}")
.jwe().encrypt();

doTestEncryptedClaims(jweCompact);
}

@Test
void encryptJsonStringShortcut() throws Exception {
String jweCompact = Jwt.encryptJson("{\"customClaim\":\"custom-value\"}");

doTestEncryptedClaims(jweCompact);
}

@Test
void encryptJsonObject() throws Exception {
JsonObject json = Json.createObjectBuilder().add("customClaim", "custom-value").build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,20 @@ void innerSignAndEncryptMapOfClaimsShortcut() throws Exception {
checkRsaInnerSignedEncryptedClaims(jweCompact);
}

@Test
void innerSignAndEncryptJsonString() throws Exception {
String jweCompact = Jwt.claimsJson("{\"customClaim\":\"custom-value\"}")
.innerSign().encrypt();
checkRsaInnerSignedEncryptedClaims(jweCompact);
}

@Test
void innerSignAndEncryptJsonStringShortcut() throws Exception {
String jweCompact = Jwt.innerSignAndEncryptJson("{\"customClaim\":\"custom-value\"}");

checkRsaInnerSignedEncryptedClaims(jweCompact);
}

@Test
void innerSignAndEncryptJsonObject() throws Exception {
JsonObject json = Json.createObjectBuilder().add("customClaim", "custom-value").build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,33 @@ void signMapOfClaimsShortcut() throws Exception {
assertEquals("custom-value", claims.getClaimValue("customClaim"));
}

@Test
void signJsonString() throws Exception {
String jwt = Jwt.claimsJson("{\"customClaim\":\"custom-value\"}")
.sign(getPrivateKey());

JsonWebSignature jws = getVerifiedJws(jwt);
JwtClaims claims = JwtClaims.parse(jws.getPayload());

assertEquals(4, claims.getClaimsMap().size());
checkDefaultClaimsAndHeaders(getJwsHeaders(jwt, 2), claims);

assertEquals("custom-value", claims.getClaimValue("customClaim"));
}

@Test
void signJsonStringShortcut() throws Exception {
String jwt = Jwt.signJson("{\"customClaim\":\"custom-value\"}");

JsonWebSignature jws = getVerifiedJws(jwt);
JwtClaims claims = JwtClaims.parse(jws.getPayload());

assertEquals(4, claims.getClaimsMap().size());
checkDefaultClaimsAndHeaders(getJwsHeaders(jwt, 2), claims);

assertEquals("custom-value", claims.getClaimValue("customClaim"));
}

@Test
void signMapOfClaimsWithKeyLocation() throws Exception {
String jwt = Jwt.claims(Collections.singletonMap("customClaim", "custom-value"))
Expand Down

0 comments on commit 5cde65d

Please sign in to comment.