-
Notifications
You must be signed in to change notification settings - Fork 801
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
Replace JWT validation library #5592
Merged
davidporter-id-au
merged 5 commits into
cadence-workflow:master
from
mantas-sidlauskas:jwt_parser
Jan 24, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
700bc49
Replace JWT validation library
mantas-sidlauskas ff66c5d
Remove unneeded issuedAt check
mantas-sidlauskas 2e7cada
fix test
mantas-sidlauskas f410493
run ./scripts/buildkite/golint.sh
mantas-sidlauskas d298494
Merge branch 'master' into jwt_parser
mantas-sidlauskas 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 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -22,13 +22,12 @@ package authorization | |
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/cristalhq/jwt/v3" | ||
"github.com/golang-jwt/jwt/v5" | ||
"go.uber.org/yarpc" | ||
|
||
"github.com/uber/cadence/common" | ||
|
@@ -38,20 +37,24 @@ import ( | |
"github.com/uber/cadence/common/log/tag" | ||
) | ||
|
||
var _ jwt.Claims = (*JWTClaims)(nil) | ||
|
||
type oauthAuthority struct { | ||
authorizationCfg config.OAuthAuthorizer | ||
domainCache cache.DomainCache | ||
log log.Logger | ||
verifier jwt.Verifier | ||
parser *jwt.Parser | ||
publicKey interface{} | ||
} | ||
|
||
// JWTClaims is a Cadence specific claim with embeded Claims defined https://datatracker.ietf.org/doc/html/rfc7519#section-4.1 | ||
type JWTClaims struct { | ||
Sub string | ||
jwt.RegisteredClaims | ||
|
||
Name string | ||
Groups string // separated by space | ||
Admin bool | ||
Iat int64 | ||
TTL int64 | ||
TTL int64 // TODO should be removed. ExpiresAt should be used | ||
} | ||
|
||
func (j JWTClaims) GetGroups() []string { | ||
|
@@ -66,25 +69,25 @@ func NewOAuthAuthorizer( | |
log log.Logger, | ||
domainCache cache.DomainCache, | ||
) (Authorizer, error) { | ||
publicKey, err := common.LoadRSAPublicKey(authorizationCfg.JwtCredentials.PublicKey) | ||
|
||
key, err := common.LoadRSAPublicKey(authorizationCfg.JwtCredentials.PublicKey) | ||
if err != nil { | ||
return nil, fmt.Errorf("loading RSA public key: %w", err) | ||
} | ||
|
||
verifier, err := jwt.NewVerifierRS( | ||
jwt.Algorithm(authorizationCfg.JwtCredentials.Algorithm), | ||
publicKey, | ||
) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("creating JWT verifier: %w", err) | ||
if authorizationCfg.JwtCredentials.Algorithm != jwt.SigningMethodRS256.Name { | ||
return nil, fmt.Errorf("algorithm %q is not supported", authorizationCfg.JwtCredentials.Algorithm) | ||
} | ||
|
||
return &oauthAuthority{ | ||
authorizationCfg: authorizationCfg, | ||
domainCache: domainCache, | ||
log: log, | ||
verifier: verifier, | ||
parser: jwt.NewParser( | ||
jwt.WithValidMethods([]string{authorizationCfg.JwtCredentials.Algorithm}), | ||
jwt.WithIssuedAt(), | ||
), | ||
publicKey: key, | ||
}, nil | ||
} | ||
|
||
|
@@ -101,49 +104,63 @@ func (a *oauthAuthority) Authorize( | |
return Result{Decision: DecisionDeny}, nil | ||
} | ||
|
||
claims, err := a.parseToken(token, a.verifier) | ||
var claims JWTClaims | ||
|
||
_, err := a.parser.ParseWithClaims(token, &claims, a.keyFunc) | ||
|
||
if err != nil { | ||
a.log.Debug("request is not authorized", tag.Error(err)) | ||
return Result{Decision: DecisionDeny}, nil | ||
} | ||
|
||
if err := a.validateTTL(claims); err != nil { | ||
if err := a.validateTTL(&claims); err != nil { | ||
a.log.Debug("request is not authorized", tag.Error(err)) | ||
return Result{Decision: DecisionDeny}, nil | ||
} | ||
|
||
if claims.Admin { | ||
return Result{Decision: DecisionAllow}, nil | ||
} | ||
|
||
domain, err := a.domainCache.GetDomain(attributes.DomainName) | ||
if err != nil { | ||
return Result{Decision: DecisionDeny}, err | ||
} | ||
|
||
if err := validatePermission(claims, attributes, domain.GetInfo().Data); err != nil { | ||
if err := validatePermission(&claims, attributes, domain.GetInfo().Data); err != nil { | ||
a.log.Debug("request is not authorized", tag.Error(err)) | ||
return Result{Decision: DecisionDeny}, nil | ||
} | ||
|
||
return Result{Decision: DecisionAllow}, nil | ||
} | ||
|
||
func (a *oauthAuthority) parseToken(tokenStr string, verifier jwt.Verifier) (*JWTClaims, error) { | ||
token, err := jwt.ParseAndVerifyString(tokenStr, verifier) | ||
if err != nil { | ||
return nil, fmt.Errorf("parse token: %w", err) | ||
} | ||
var claims JWTClaims | ||
_ = json.Unmarshal(token.RawClaims(), &claims) | ||
return &claims, nil | ||
// keyFunc returns correct key to check signature | ||
func (a *oauthAuthority) keyFunc(token *jwt.Token) (interface{}, error) { | ||
// only local public key is supported currently | ||
return a.publicKey, nil | ||
} | ||
|
||
func (a *oauthAuthority) validateTTL(claims *JWTClaims) error { | ||
if claims.TTL > a.authorizationCfg.MaxJwtTTL { | ||
return fmt.Errorf("token TTL: %d is larger than MaxTTL allowed: %d", claims.TTL, a.authorizationCfg.MaxJwtTTL) | ||
// Fill ExpiresAt when TTL is passed | ||
if claims.TTL > 0 { | ||
claims.ExpiresAt = jwt.NewNumericDate(claims.IssuedAt.Time.Add(time.Second * time.Duration(claims.TTL))) | ||
} | ||
if claims.Iat+claims.TTL < time.Now().Unix() { | ||
return errors.New("JWT has expired") | ||
|
||
exp, err := claims.GetExpirationTime() | ||
|
||
if err != nil || exp == nil { | ||
return errors.New("ExpiresAt is not set") | ||
} | ||
|
||
timeLeft := exp.Unix() - time.Now().Unix() | ||
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. my comment wasn't great, I actually meant this: I thought this would be something that the library would do natively |
||
if timeLeft < 0 { | ||
return errors.New("token is expired") | ||
} | ||
|
||
if timeLeft > a.authorizationCfg.MaxJwtTTL { | ||
return fmt.Errorf("token TTL: %d is larger than MaxTTL allowed: %d", timeLeft, a.authorizationCfg.MaxJwtTTL) | ||
} | ||
|
||
return nil | ||
} |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
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.
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.
I'm a little surprised this is necessary, it's not a problem, but I wouldj have throught that the JWT lib would do this natively
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.
Our clients (java and go) are not setting
ExpiresAt
claim. Instead, they are providing TTL claim. This is to support backwards compatibility. Later on, clients needs to be updated to fill inExpiresAt
and we can delegate this check to JWT lib.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.
After re-reading your comment, I realized that it was not about TTL check, but for
IssuedAt
check. And yes, you are right, this is not needed as library will do this validation. Thanks!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.
ah, sorry, yeah, i wasn't being super clear and to be honest, i get confused between the different values. I'll don't think it's a blocker, so stamping. Thank you for looking into it.