Skip to content

Commit

Permalink
Merge pull request #53 from mdemille/master
Browse files Browse the repository at this point in the history
Add an encode method that accepts a `Supplier<Header>` so any Header class can be provided
  • Loading branch information
robotdan authored Sep 27, 2023
2 parents 2fe8c5c + 6253ac6 commit abb9e6b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
31 changes: 27 additions & 4 deletions src/main/java/io/fusionauth/jwt/JWTEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
* @author Daniel DeGroff
Expand All @@ -41,6 +42,24 @@ public String encode(JWT jwt, Signer signer) {
return encode(jwt, signer, h -> h.set("kid", signer.getKid()));
}

/**
* Encode the JWT to produce a dot separated encoded string that can be sent in an HTTP request header.
*
* @param jwt The JWT.
* @param signer The signer used to add a signature to the JWT.
* @param supplier A header supplier to optionally add header values to the encoded JWT. May be null.
* @return the encoded JWT string.
*/
public String encode(JWT jwt, Signer signer, Supplier<Header> supplier) {
final Header header;
if (supplier != null) {
header = supplier.get();
} else {
header = new Header();
}
return encode(jwt, signer, header);
}

/**
* Encode the JWT to produce a dot separated encoded string that can be sent in an HTTP request header.
*
Expand All @@ -50,14 +69,18 @@ public String encode(JWT jwt, Signer signer) {
* @return the encoded JWT string.
*/
public String encode(JWT jwt, Signer signer, Consumer<Header> consumer) {
Objects.requireNonNull(jwt);
Objects.requireNonNull(signer);

List<String> parts = new ArrayList<>(3);
Header header = new Header();
if (consumer != null) {
consumer.accept(header);
}
return encode(jwt, signer, header);
}

private String encode(JWT jwt, Signer signer, Header header) {
Objects.requireNonNull(jwt);
Objects.requireNonNull(signer);

List<String> parts = new ArrayList<>(3);
// Set this after we pass the header to the consumer to ensure it isn't tampered with, only the signer can set the algorithm.
header.algorithm = signer.getAlgorithm();
parts.add(base64Encode(Mapper.serialize(header)));
Expand Down
9 changes: 6 additions & 3 deletions src/test/java/io/fusionauth/jwt/JWTTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -563,9 +563,12 @@ public void test_complexPayload() {
Signer signer = HMACSigner.newSHA256Signer("secret");
Verifier verifier = HMACVerifier.newVerifier("secret");

String encodedJWT = JWT.getEncoder().encode(expectedJWT, signer, header -> header
.set("gty", Collections.singletonList("client_credentials"))
.set("kid", "1234"));
String encodedJWT = JWT.getEncoder().encode(expectedJWT, signer, () -> {
final Header header = new Header();
header.set("gty", Collections.singletonList("client_credentials"));
header.set("kid", "1234");
return header;
});
JWT actualJwt = JWT.getDecoder().decode(encodedJWT, verifier);

assertEquals(actualJwt.header.algorithm, Algorithm.HS256);
Expand Down

0 comments on commit abb9e6b

Please sign in to comment.