-
Notifications
You must be signed in to change notification settings - Fork 165
Improve jwtAuth error reporting #2947
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
Open
shreyash24-tech
wants to merge
2
commits into
membrane:master
Choose a base branch
from
shreyash24-tech:fix-jwt-error-message
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,9 +31,9 @@ | |
| import static org.apache.commons.text.StringEscapeUtils.*; | ||
|
|
||
| /** | ||
| * @description Validates a JWT on requests (signature via JWKS, required exp/sub) and exposes claims in exchange properties ("jwt"). | ||
| * @yaml | ||
| * <pre><code> | ||
| * @description Validates a JWT on requests (signature via JWKS, required | ||
| * exp/sub) and exposes claims in exchange properties ("jwt"). | ||
| * @yaml <pre><code> | ||
| * jwtAuth: | ||
| * expectedAud: my-audience | ||
| * expectedTid: 67c859d3-0cd4-4a99-86db-088bed1a9601 | ||
|
|
@@ -65,7 +65,6 @@ public static String ERROR_JWT_VALUE_NOT_PRESENT(String key) { | |
| String expectedAud; | ||
| String expectedTid; | ||
|
|
||
|
|
||
| public JwtAuthInterceptor() { | ||
| name = "jwt checker."; | ||
| setAppliedFlow(of(REQUEST)); | ||
|
|
@@ -74,8 +73,9 @@ public JwtAuthInterceptor() { | |
| @Override | ||
| public void init() { | ||
| super.init(); | ||
| if(jwtRetriever == null) | ||
| jwtRetriever = new HeaderJwtRetriever("Authorization","Bearer"); | ||
| if (jwtRetriever == null) { | ||
| jwtRetriever = new HeaderJwtRetriever("Authorization", "Bearer"); | ||
| } | ||
|
|
||
| jwks.init(router); | ||
| } | ||
|
|
@@ -101,12 +101,14 @@ public Outcome handleRequest(Exchange exc) { | |
| .buildAndSetResponse(exc); | ||
| return RETURN; | ||
| } catch (InvalidJwtException e) { | ||
| log.error("JWT validation failed: {}", e.getMessage(), e); | ||
| ProblemDetails.security(router.getConfiguration().isProduction(), "jwt-auth") | ||
| .detail(ERROR_VALIDATION_FAILED) | ||
| .detail(e.getMessage()) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. .detail("JWT validation failed: " +e.getMessage()) |
||
| .addSubSee(ERROR_VALIDATION_FAILED_ID) | ||
| .stacktrace(false) | ||
| .status(400) | ||
| .buildAndSetResponse(exc); | ||
|
|
||
| return RETURN; | ||
| } catch (Exception e) { | ||
| ProblemDetails.security(router.getConfiguration().isProduction(), "jwt-auth") | ||
|
|
@@ -120,8 +122,9 @@ public Outcome handleRequest(Exchange exc) { | |
| } | ||
|
|
||
| public Outcome handleJwt(Exchange exc, String jwt) throws JWTException, JsonProcessingException, InvalidJwtException { | ||
| if (jwt == null) | ||
| if (jwt == null) { | ||
| throw new JWTException(ERROR_JWT_NOT_FOUND, ERROR_JWT_NOT_FOUND_ID); | ||
| } | ||
|
|
||
| var decodedJwt = new JsonWebToken(jwt); | ||
| var kid = decodedJwt.getHeader().kid(); | ||
|
|
@@ -132,7 +135,7 @@ public Outcome handleJwt(Exchange exc, String jwt) throws JWTException, JsonProc | |
|
|
||
| Map<String, Object> jwtClaims = createValidator(key).processToClaims(jwt).getClaimsMap(); | ||
|
|
||
| exc.getProperties().put("jwt",jwtClaims); | ||
| exc.getProperties().put("jwt", jwtClaims); | ||
|
|
||
| new JWTSecurityScheme(jwtClaims).add(exc); | ||
|
|
||
|
|
@@ -146,17 +149,19 @@ private JwtConsumer createValidator(RsaJsonWebKey key) { | |
| .setRequireSubject() | ||
| .setVerificationKey(key.getRsaPublicKey()); | ||
|
|
||
| if (acceptAnyAud()) | ||
| jwtConsumerBuilder.setSkipDefaultAudienceValidation(); | ||
| else { | ||
| if (expectedAud != null && !expectedAud.isEmpty()) | ||
| if (acceptAnyAud()) { | ||
| jwtConsumerBuilder.setSkipDefaultAudienceValidation(); | ||
| }else { | ||
| if (expectedAud != null && !expectedAud.isEmpty()) { | ||
| jwtConsumerBuilder | ||
| .setExpectedAudience(expectedAud); | ||
| } | ||
| } | ||
|
|
||
| if (expectedTid != null && !expectedTid.isEmpty()) | ||
| if (expectedTid != null && !expectedTid.isEmpty()) { | ||
| jwtConsumerBuilder | ||
| .registerValidator(new TidValidator(expectedTid)); | ||
| } | ||
|
|
||
| return jwtConsumerBuilder.build(); | ||
| } | ||
|
|
@@ -193,8 +198,11 @@ public String getExpectedTid() { | |
|
|
||
| /** | ||
| * @description | ||
| * <p>Expected audience ('aud') value of the token.</p> | ||
| * <p>Use "any!!" to allow any audience value. This is strongly discouraged.</p> | ||
| * <p> | ||
| * Expected audience ('aud') value of the token.</p> | ||
| * <p> | ||
| * Use "any!!" to allow any audience value. This is strongly | ||
| * discouraged.</p> | ||
| */ | ||
| @MCAttribute | ||
| public void setExpectedAud(String expectedAud) { | ||
|
|
@@ -203,7 +211,8 @@ public void setExpectedAud(String expectedAud) { | |
|
|
||
| /** | ||
| * @description | ||
| * <p>Expected tenant ID ('tid') value of the token.</p> | ||
| * <p> | ||
| * Expected tenant ID ('tid') value of the token.</p> | ||
| * @default not set | ||
| * @example 67c869d3-0cd4-4a99-86db-088bed1a9601 | ||
| */ | ||
|
|
@@ -219,11 +228,11 @@ public String getShortDescription() { | |
|
|
||
| @Override | ||
| public String getLongDescription() { | ||
| return "Checks for a valid JWT.<br/>" + | ||
| (acceptAnyAud() ? | ||
| "Accepts any value for the <font style=\"font-family: monospace\">aud</font> field. <b>THIS IS STRONGLY DISCOURAGED!</b><br/>" : | ||
| "Accepts <font style=\"font-family: monospace\">" + escapeHtml4(expectedAud) + "</font> as valid value for the <font style=\"font-family: monospace\">aud</font> payload entry.<br/>") + | ||
| (jwks != null ? "Validates the JWT signature against " + jwks.getLongDescription() + " ." : ""); | ||
| return "Checks for a valid JWT.<br/>" | ||
| + (acceptAnyAud() | ||
| ? "Accepts any value for the <font style=\"font-family: monospace\">aud</font> field. <b>THIS IS STRONGLY DISCOURAGED!</b><br/>" | ||
| : "Accepts <font style=\"font-family: monospace\">" + escapeHtml4(expectedAud) + "</font> as valid value for the <font style=\"font-family: monospace\">aud</font> payload entry.<br/>") | ||
| + (jwks != null ? "Validates the JWT signature against " + jwks.getLongDescription() + " ." : ""); | ||
| } | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use level info. That wrong input is sent is normal operation. INFO is default log level, so it appears in the log.