Skip to content

Commit

Permalink
Merge pull request #256 from USACE/chore/validate-jwt-alg
Browse files Browse the repository at this point in the history
Validate jwt method
  • Loading branch information
dennisgsmith authored Feb 20, 2025
2 parents 50fc586 + fa5ed2f commit 001e779
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion api/internal/config/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type ServerConfig struct {
AuthJWTMocked bool `env:"AUTH_JWT_MOCKED"`
AuthAllowEmailSuffixes []string `env:"AUTH_ALLOW_EMAIL_SUFFIXES"`
AuthPublicKey string `env:"AUTH_PUBLIC_KEY"`
AuthSigningMethod string `env:"AUTH_SIGNING_METHOD"`
AuthSigningMethod string `env:"AUTH_SIGNING_METHOD" envDefault:"RS256"`
Debug bool `env:"DEBUG"`
ReportDownloadJobMocked bool `env:"REPORT_DOWNLOAD_JOB_MOCKED"`
RequestLoggerEnabled bool `env:"REQUEST_LOGGER_ENABLED"`
Expand Down
6 changes: 3 additions & 3 deletions api/internal/middleware/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (m *mw) JWT(ctx huma.Context, next func(huma.Context)) {
}
default:
pub := fmt.Sprintf("-----BEGIN PUBLIC KEY-----\n%s\n-----END PUBLIC KEY-----", m.cfg.AuthPublicKey)
userJWT, err = parseJWTFromHeader(ctx, pub)
userJWT, err = parseJWTFromHeader(ctx, pub, []string{m.cfg.AuthSigningMethod})
if err != nil {
httperr.SetResponse(ctx, httperr.Forbidden(errors.New("JWT invalid")))
return
Expand All @@ -48,7 +48,7 @@ func (m *mw) JWT(ctx huma.Context, next func(huma.Context)) {
next(ctx)
}

func parseJWTFromHeader(ctx huma.Context, publicKey string) (*jwt.Token, error) {
func parseJWTFromHeader(ctx huma.Context, publicKey string, validMethods []string) (*jwt.Token, error) {
rawToken, err := getJWTFromHeaderRaw(ctx)
if err != nil {
return nil, err
Expand All @@ -58,7 +58,7 @@ func parseJWTFromHeader(ctx huma.Context, publicKey string) (*jwt.Token, error)
return nil, errors.New("unexpected signing method")
}
return jwt.ParseRSAPublicKeyFromPEM([]byte(publicKey))
})
}, jwt.WithValidMethods(validMethods))
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 001e779

Please sign in to comment.