Skip to content

Commit

Permalink
Add config option for JWT issuer and require iat/exp claims in token (#…
Browse files Browse the repository at this point in the history
…16)

Runtime users should be validating the JWT issuer in addition to the
JWKS. This commit adds logic to configure the expected issuer for a
JWT and updates the token parser to require the "iat" and "exp" claims
be present for a given token. Additionally, this commit fixes an error
where command line flags were not being bound to Viper configs
properly.

Signed-off-by: John Schaeffer <jschaeffer@equinix.com>
  • Loading branch information
jnschaeffer authored Feb 14, 2024
1 parent cbe866b commit 45c52c4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
4 changes: 4 additions & 0 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func init() {
jwt.AddFlags(cmdFlags)
permissions.AddFlags(cmdFlags)
server.AddFlags(cmdFlags)

if err := viper.BindPFlags(cmdFlags); err != nil {
panic(err)
}
}

func serve(_ context.Context, _ *viper.Viper, cfg config.Config) error {
Expand Down
3 changes: 2 additions & 1 deletion config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ server:
permissions:
host: permissions-api.enterprise.dev
jwt:
jwksuri: https://iam.enterprise.dev/jwks.json
jwksuri: https://identity-api.enterprise.dev/jwks.json
issuer: https://identity-api.enterprise.dev/
tracing:
enabled: false
2 changes: 2 additions & 0 deletions internal/jwt/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (

// Config represents the configuration for a JWT validator.
type Config struct {
Issuer string
JWKSURI string
}

// AddFlags sets the command line flags for JWT validation.
func AddFlags(flags *pflag.FlagSet) {
flags.String("jwt.issuer", "", "Issuer to use for JWT validation")
flags.String("jwt.jwksuri", "", "JWKS URI to use for JWT validation")
}
14 changes: 11 additions & 3 deletions internal/jwt/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ type Validator interface {
}

type validator struct {
kf jwt.Keyfunc
kf jwt.Keyfunc
parser *jwt.Parser
}

// NewValidator creates a validator with the given configuration.
Expand Down Expand Up @@ -51,15 +52,22 @@ func NewValidator(config Config) (Validator, error) {
return nil, err
}

parser := jwt.NewParser(
jwt.WithIssuedAt(),
jwt.WithExpirationRequired(),
jwt.WithIssuer(config.Issuer),
)

out := &validator{
kf: kf.Keyfunc,
kf: kf.Keyfunc,
parser: parser,
}

return out, nil
}

func (v *validator) ValidateToken(tokenString string) (map[string]string, error) {
tok, err := jwt.Parse(tokenString, v.kf)
tok, err := v.parser.Parse(tokenString, v.kf)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 45c52c4

Please sign in to comment.