Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to build JWT from JSON string #820

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading