Skip to content

Commit

Permalink
Add JWTUtils.decodeHeader
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel DeGroff committed Feb 23, 2019
1 parent 095947e commit 012a0ea
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
21 changes: 21 additions & 0 deletions src/main/java/io/fusionauth/jwt/JWTUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.fusionauth.jwt;

import io.fusionauth.jwt.domain.Header;
import io.fusionauth.jwt.domain.JWT;
import io.fusionauth.jwt.domain.KeyPair;
import io.fusionauth.jwt.domain.KeyType;
Expand Down Expand Up @@ -63,6 +64,26 @@ public static String convertThumbprintToFingerprint(String x5tHash) {
return HexUtils.fromBytes(bytes);
}

/**
* WARNING!! This is not a secure or safe way to decode a JWT, this will not perform any validation on the signature.
* <p>
* Consider the header returned from this method as un-trustworthy. This is intended for utility and a nice way to
* read the JWT header, but do not use it in production to verify the integrity.
*
* @param encodedJWT the encoded JWT
* @return a Header object
*/
public static Header decodeHeader(String encodedJWT) {
Objects.requireNonNull(encodedJWT);

String[] parts = encodedJWT.split("\\.");
if (parts.length == 3 || (parts.length == 2 && encodedJWT.endsWith("."))) {
return Mapper.deserialize(Base64.getUrlDecoder().decode(parts[0]), Header.class);
}

throw new InvalidJWTException("The encoded JWT is not properly formatted. Expected a three part dot separated string.");
}

/**
* WARNING!! This is not a secure or safe way to decode a JWT, this will not perform any validation on the signature.
* <p>
Expand Down
9 changes: 6 additions & 3 deletions src/test/java/io/fusionauth/jwt/JWTUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.fusionauth.jwt;

import io.fusionauth.jwt.domain.Algorithm;
import io.fusionauth.jwt.domain.JWT;
import io.fusionauth.jwt.domain.KeyPair;
import io.fusionauth.jwt.hmac.HMACSigner;
Expand Down Expand Up @@ -46,12 +47,14 @@ public void decodePayload() {
JWT jwt = new JWT().setSubject("123456789");

// HMAC signed
JWT actual = JWTUtils.decodePayload(JWT.getEncoder().encode(jwt, HMACSigner.newSHA512Signer("secret1")));
assertEquals(actual.subject, jwt.subject);
String encodedJWT = JWT.getEncoder().encode(jwt, HMACSigner.newSHA512Signer("secret1"));
assertEquals(JWTUtils.decodePayload(encodedJWT).subject, "123456789");
assertEquals(JWTUtils.decodeHeader(encodedJWT).algorithm, Algorithm.HS512);

// Test with an unsecured signer
String unsecuredJWT = JWT.getEncoder().encode(jwt, new UnsecuredSigner());
assertEquals(JWTUtils.decodePayload(unsecuredJWT).subject, jwt.subject);
assertEquals(JWTUtils.decodePayload(unsecuredJWT).subject, "123456789");
assertEquals(JWTUtils.decodeHeader(unsecuredJWT).algorithm, Algorithm.none);
}

@Test
Expand Down

0 comments on commit 012a0ea

Please sign in to comment.