Skip to content

Conversation

GurtejSohi
Copy link
Contributor

Description

Adds JWT decoder

@codecov
Copy link

codecov bot commented May 18, 2021

Codecov Report

Merging #15 (c0f7f8a) into main (d47c113) will increase coverage by 0.86%.
The diff coverage is 72.41%.

Impacted file tree graph

@@             Coverage Diff              @@
##               main      #15      +/-   ##
============================================
+ Coverage     66.47%   67.33%   +0.86%     
- Complexity       58       64       +6     
============================================
  Files            14       15       +1     
  Lines           170      199      +29     
  Branches          9        9              
============================================
+ Hits            113      134      +21     
- Misses           49       57       +8     
  Partials          8        8              
Flag Coverage Δ Complexity Δ
unit 67.33% <72.41%> (+0.86%) 64.00 <6.00> (+6.00)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ Complexity Δ
...ertrace/core/grpcutils/context/RequestContext.java 52.00% <16.66%> (-11.16%) 8.00 <0.00> (ø)
...g/hypertrace/core/grpcutils/context/JwtParser.java 86.95% <86.95%> (ø) 6.00 <6.00> (?)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d47c113...c0f7f8a. Read the comment docs.

@github-actions

This comment has been minimized.


public Optional<VerifiedJwt> fromJwt(String jwtValue) {
try {
return this.verifiedJwtCache.get(jwtValue, () -> this.decodeAndVerify(jwtValue));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why use a cache ?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we need to access it multiple times we could store in the context object instead ?

Copy link
Contributor

@aaron-steinfeld aaron-steinfeld May 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So my understanding is that we actually don't need or want to force verification here, instead relying on the proxy to do it. Either way though, everything around JWTs should be package protected, we only want to expose the abstracted information via the context.

If we do need to enforce it, then every service using this will need to take in env-specific values like the jwks url and audience, which is not ideal.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I think thats what we decided the other day. Verification is the responsibility of the proxy.
Should we just add both the APIs then? Depending on the use case, the callers can decide.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by both the apis? the verified and unverified one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed jwt verification and now, exposing the abstracted information via the context.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I meant decodeAndVerify and just decode

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can add the verification part later on if required(we may not even need that if we're relying on proxy for verification). Also, the verification part requires passing in some extra values which makes it messy (which we may be able to avoid).

@jcchavezs
Copy link

Do not forget to include tests on this one.

}
}

private static final class DefaultVerifiedJwt implements VerifiedJwt {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am curious about this class being final and private. Do we expect others to implement their own VerifiedJwt? if so, will that be based on this default one?

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Copy link
Contributor

@aaron-steinfeld aaron-steinfeld left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

private static final Logger LOG = LoggerFactory.getLogger(JwtParser.class);
private static final String BEARER_TOKEN_PREFIX = "Bearer ";

private final Cache<String, Optional<Jwt>> jwtCache =

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the cache being still used ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, using the cache to help avoid decoding the same token again.

@GurtejSohi GurtejSohi merged commit 614ba57 into main May 24, 2021
@GurtejSohi GurtejSohi deleted the jwt-decoder branch May 24, 2021 12:09
@github-actions
Copy link

Unit Test Results

10 files  +1  10 suites  +1   11s ⏱️ +4s
46 tests +4  46 ✔️ +4  0 💤 ±0  0 ❌ ±0 

Results for commit 614ba57. ± Comparison against base commit d47c113.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants