Skip to content

Commit

Permalink
Allow removing claims when bulding JWT (#710)
Browse files Browse the repository at this point in the history
  • Loading branch information
sberyozkin authored Jul 5, 2023
1 parent 4a49a2d commit aaa5f8c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ default JwtClaimsBuilder claim(Claims name, Object value) {
*/
JwtClaimsBuilder claim(String name, Object value);

/**
* Remove a claim.
*
* @param name the claim name
* @return JwtClaimsBuilder
*/
JwtClaimsBuilder remove(String name);

/**
* Set JsonWebSignature headers and sign the claims by moving to {@link JwtSignatureBuilder}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,4 +378,10 @@ public Object verify(String name, Object value) {
}
}

@Override
public JwtClaimsBuilder remove(String name) {
claims.unsetClaim(name);
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
Expand All @@ -39,6 +40,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
Expand Down Expand Up @@ -110,6 +112,30 @@ void enhanceAndResignToken() throws Exception {
assertEquals("https://localhost:8081", claims.getAudience().get(0));
}

@Test
void enhanceAndResignTokenWithCustomClaimRemoved() throws Exception {
JwtClaims tokenClaims = signAndVerifyClaims();
assertEquals("custom-value", tokenClaims.getClaimValue("customClaim"));
JsonWebToken token = new TestJsonWebToken(tokenClaims);

String jwt = Jwt.claims(token).remove("customClaim")
// this just checks trying to remove non-existent claims does not cause some NPE
.remove(UUID.randomUUID().toString())
.claim("newClaim", "new-value").sign();

// verify
JsonWebSignature jws = getVerifiedJws(jwt);
JwtClaims claims = JwtClaims.parse(jws.getPayload());
assertEquals(6, claims.getClaimsMap().size());
checkDefaultClaimsAndHeaders(getJwsHeaders(jwt, 2), claims);
assertNull(claims.getClaimValue("customClaim"));

assertEquals("new-value", claims.getClaimValue("newClaim"));
assertEquals("https://default-issuer", claims.getIssuer());
assertEquals(1, claims.getAudience().size());
assertEquals("https://localhost:8081", claims.getAudience().get(0));
}

@Test
void enhanceAndResignTokenWithConfiguredIssuerAndAudUsed() throws Exception {
JsonWebToken token = new TestJsonWebToken(signAndVerifyClaims());
Expand Down

0 comments on commit aaa5f8c

Please sign in to comment.