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

add square/go-jose.v2 token validator #84

Merged
merged 16 commits into from
May 25, 2021
Merged

Conversation

grounded042
Copy link
Contributor

In support of #73 this adds a square/go-jose.v2 implementation ValidateToken.

This comes complete with an example. The package also adds support for clock skew (#58), custom claims (#53), custom validation (#74), and expected claims. It also supports algorithm validation.

Note that I'm merging this into the v2 branch and not main as v2 is not ready to be released yet.


// UserContext is the struct that will be inserted into the context for the
// user. CustomClaims will be nil unless WithCustomClaims is passed to New.
type UserContext struct {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the struct that will be in the request context to identify the user.

// optional options which we will default if not specified
expectedClaims func() jwt.Expected
allowedClockSkew time.Duration
customClaims func() CustomClaims
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We use a function for this because we don't want to share the same struct across go routines that are handling requests. The same is true for line 94.

Copy link

Choose a reason for hiding this comment

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

🤔 are we actually writing to these structs? Or just read only?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We unmarshal into CustomClaims. expectedClaims can include a time to validate against so a func allows us or the user to set the time as now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See line 76 above.

Comment on lines +110 to +119
if signatureAlgorithm != "" && signatureAlgorithm != tok.Headers[0].Algorithm {
return nil, fmt.Errorf("expected %q signin algorithm but token specified %q", signatureAlgorithm, tok.Headers[0].Algorithm)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment on lines +120 to +129
if v.customClaims != nil {
claimDest = append(claimDest, v.customClaims())
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If customClaims contains duplicate fields of jwt.Claims the tok.Claims function on 124 will unmarshal into both.

Comment on lines +138 to +147
if err = userCtx.CustomClaims.Validate(ctx); err != nil {
return nil, fmt.Errorf("custom claims not validated: %w", err)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is where custom validation can happen!

jwtmiddleware.go Outdated Show resolved Hide resolved
Copy link

@cyx cyx left a comment

Choose a reason for hiding this comment

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

Looks good!

@JayHelton
Copy link

I like it. Soon I will take a stab at an implementation with JWX following this pattern. Nice work!

Signed-off-by: Jon Carl <jon.carl@auth0.com>
Signed-off-by: Jon Carl <jon.carl@auth0.com>
Signed-off-by: Jon Carl <jon.carl@auth0.com>
Signed-off-by: Jon Carl <jon.carl@auth0.com>
Signed-off-by: Jon Carl <jon.carl@auth0.com>
Signed-off-by: Jon Carl <jon.carl@auth0.com>
Signed-off-by: Jon Carl <jon.carl@auth0.com>
@codecov-commenter
Copy link

codecov-commenter commented May 14, 2021

Codecov Report

Merging #84 (67bbe07) into v2 (0d1f50b) will decrease coverage by 4.42%.
The diff coverage is 86.36%.

Impacted file tree graph

@@            Coverage Diff             @@
##               v2      #84      +/-   ##
==========================================
- Coverage   95.08%   90.65%   -4.43%     
==========================================
  Files           1        2       +1     
  Lines          61      107      +46     
==========================================
+ Hits           58       97      +39     
- Misses          2        9       +7     
  Partials        1        1              
Impacted Files Coverage Δ
validate/josev2/josev2.go 85.71% <85.71%> (ø)
jwtmiddleware.go 93.84% <100.00%> (-1.24%) ⬇️

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 0d1f50b...67bbe07. Read the comment docs.

Signed-off-by: Jon Carl <jon.carl@auth0.com>
Signed-off-by: Jon Carl <jon.carl@auth0.com>
Signed-off-by: Jon Carl <jon.carl@auth0.com>
Signed-off-by: Jon Carl <jon.carl@auth0.com>
Signed-off-by: Jon Carl <jon.carl@auth0.com>
Signed-off-by: Jon Carl <jon.carl@auth0.com>
Signed-off-by: Jon Carl <jon.carl@auth0.com>
Signed-off-by: Jon Carl <jon.carl@auth0.com>
Signed-off-by: Jon Carl <jon.carl@auth0.com>
@grounded042 grounded042 merged commit 665e7da into v2 May 25, 2021
@grounded042 grounded042 deleted the jon/go-jose-token-validator branch May 25, 2021 14:38
sergiught pushed a commit that referenced this pull request Nov 1, 2021
d10i pushed a commit to Hikely/go-jwt-middleware that referenced this pull request Nov 2, 2021
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.

None yet

4 participants