diff --git a/echokit/authenticationmiddleware.go b/echokit/authenticationmiddleware.go index 60fe234..d539464 100644 --- a/echokit/authenticationmiddleware.go +++ b/echokit/authenticationmiddleware.go @@ -52,6 +52,11 @@ func NewAuthenticationMiddleware(authenticator Authenticator, options ...Authent err := authenticator.AuthenticateRequest(c) if err != nil { + // Preserve echo.HTTPError (e.g., 403 from JWT audience mismatch) + var httpErr *echo.HTTPError + if errors.As(err, &httpErr) { + return httpErr + } return kit.WrapError(err, "error authenticating request") } diff --git a/echokit/d3authjwtauthenticator.go b/echokit/d3authjwtauthenticator.go index bd87846..dd996cb 100644 --- a/echokit/d3authjwtauthenticator.go +++ b/echokit/d3authjwtauthenticator.go @@ -3,6 +3,7 @@ package echokit import ( "context" "errors" + "log/slog" "net/http" "net/url" "strings" @@ -93,7 +94,11 @@ func (a *D3AuthJWTAuthenticator) AuthenticateRequest(c echo.Context) error { validateResult, err := a.jwtValidator.ValidateToken(c.Request().Context(), authHeaderParts[1]) if err != nil { - return err + // The user presented a d3-auth JWT, so they are authenticated — but + // validation failed (wrong audience, expired, etc.), meaning they are + // not authorized for this API. Return 403, not 500. + slog.Debug("d3auth_jwt_validation_failed", "error", err.Error()) + return echo.NewHTTPError(http.StatusForbidden, "You do not have access to this application") } validatedClaims, ok := validateResult.(*validator.ValidatedClaims) diff --git a/pgkit/migrator.go b/pgkit/migrator.go index ebfac02..163b2af 100644 --- a/pgkit/migrator.go +++ b/pgkit/migrator.go @@ -23,6 +23,20 @@ type Migration struct { AppliedAt *time.Time } +// MigrationResult reports what happened when a single migration was applied. +type MigrationResult struct { + Filename string + Duration time.Duration +} + +// MigratorOption configures the migrator. +type MigratorOption func(*migrator) + +// WithOnApplied sets a callback invoked after each migration is applied. +func WithOnApplied(fn func(MigrationResult)) MigratorOption { + return func(m *migrator) { m.onApplied = fn } +} + // Migrator is an interface for running database migrations type Migrator interface { RunMigrations(db DB, dirPath string) error @@ -31,7 +45,9 @@ type Migrator interface { } // migrator implements Migrator -type migrator struct{} +type migrator struct { + onApplied func(MigrationResult) +} // parseMigrationVersion extracts the version number from a migration filename // Expected format: {number}_{description}.sql @@ -289,6 +305,7 @@ func (m *migrator) runMigrations(db DB, dirPath string, toVersion int) error { return kit.WrapError(err, "failed to read migration %s", filename) } + start := time.Now() _, err = db.Exec(context.Background(), string(content)) if err != nil { return kit.WrapError(err, "failed to execute migration %s", filename) @@ -300,6 +317,10 @@ func (m *migrator) runMigrations(db DB, dirPath string, toVersion int) error { return kit.WrapError(err, "failed to record migration %s", filename) } + if m.onApplied != nil { + m.onApplied(MigrationResult{Filename: filename, Duration: time.Since(start)}) + } + // Stop if we've reached the target version if isTargetVersion { break @@ -310,6 +331,10 @@ func (m *migrator) runMigrations(db DB, dirPath string, toVersion int) error { } // NewMigrator creates a new Migrator -func NewMigrator() Migrator { - return &migrator{} +func NewMigrator(opts ...MigratorOption) Migrator { + m := &migrator{} + for _, opt := range opts { + opt(m) + } + return m }