Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions echokit/authenticationmiddleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
7 changes: 6 additions & 1 deletion echokit/d3authjwtauthenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package echokit
import (
"context"
"errors"
"log/slog"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -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)
Expand Down
31 changes: 28 additions & 3 deletions pgkit/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
}
Loading