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

Issue#427 DefaultJWTTokenParser#parseClaims Exception Handling #429

Merged
merged 2 commits into from
Mar 1, 2021
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 @@ -152,8 +152,13 @@ private JwtContext parseClaims(String token, JWTAuthContextInfo authContextInfo,

return jwtContext;
} catch (InvalidJwtException e) {
PrincipalLogging.log.tokenInvalid();
throw PrincipalMessages.msg.failedToVerifyToken(e);
if (e.getCause() instanceof UnresolvableKeyException) {
PrincipalLogging.log.verificationKeyUnresolvable();
throw PrincipalMessages.msg.failedToVerifyToken(e.getCause());
} else {
PrincipalLogging.log.tokenInvalid();
throw PrincipalMessages.msg.failedToVerifyToken(e);
}
} catch (UnresolvableKeyException e) {
PrincipalLogging.log.verificationKeyUnresolvable();
throw PrincipalMessages.msg.failedToVerifyToken(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.eclipse.microprofile.jwt.JsonWebToken;
import org.jose4j.jwk.JsonWebKey;
import org.jose4j.jwk.PublicJsonWebKey;
import org.jose4j.jwt.consumer.InvalidJwtException;
import org.jose4j.lang.UnresolvableKeyException;
import org.junit.Test;

import io.smallrye.jwt.algorithm.KeyEncryptionAlgorithm;
Expand Down Expand Up @@ -75,9 +75,9 @@ public void testParseWithConfiguredCertAndThumbprintMissing() throws Exception {
JWTAuthContextInfo config = new JWTAuthContextInfo("/certificate.pem", "https://server.example.com");
config.setVerifyCertificateThumbprint(true);
JWTParser parser = new DefaultJWTParser(config);
ParseException thrown = assertThrows("InvalidJwtException is expected",
ParseException thrown = assertThrows("UnresolvableKeyException is expected",
ParseException.class, () -> parser.parse(jwtString));
assertTrue(thrown.getCause() instanceof InvalidJwtException);
assertTrue(thrown.getCause() instanceof UnresolvableKeyException);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void testVerifyWithJwkKeyWithNonMatchingKid() throws Exception {
verifyToken("key2", null, "publicKey.jwk");
Assert.fail("ParseException is expected");
} catch (ParseException ex) {
Assert.assertTrue(ex.getCause().getCause() instanceof UnresolvableKeyException);
Assert.assertTrue(ex.getCause() instanceof UnresolvableKeyException);
}
}

Expand All @@ -71,7 +71,7 @@ public void testVerifyWithJwkFromSetWithWrongKidAndRequiredKid() throws Exceptio
verifyToken("key2", "key1", "publicKeySet.jwk");
Assert.fail("ParseException is expected");
} catch (ParseException ex) {
Assert.assertTrue(ex.getCause().getCause() instanceof UnresolvableKeyException);
Assert.assertTrue(ex.getCause() instanceof UnresolvableKeyException);
}
}

Expand All @@ -81,7 +81,7 @@ public void testVerifyWithJwkKeyWithNonMatchingKidFromSet() throws Exception {
verifyToken("key3", null, "publicKeySet.jwk");
Assert.fail("ParseException is expected");
} catch (ParseException ex) {
Assert.assertTrue(ex.getCause().getCause() instanceof UnresolvableKeyException);
Assert.assertTrue(ex.getCause() instanceof UnresolvableKeyException);
}
}

Expand Down Expand Up @@ -156,7 +156,7 @@ public void testVerifyEcSignedTokenWithWrongKey() throws Exception {
new DefaultJWTTokenParser().parse(jwt, contextInfo);
Assert.fail("ParseException is expected due to the wrong key type");
} catch (ParseException ex) {
Assert.assertTrue(ex.getCause().getCause() instanceof UnresolvableKeyException);
Assert.assertTrue(ex.getCause() instanceof UnresolvableKeyException);
}
}

Expand Down