package console import ( "context" "fmt" "time" "git.golem15.com/golem15/sm-user-plugin/classes" "git.golem15.com/golem15/summercms/modules/backpack" "git.golem15.com/golem15/summercms/modules/bonfire" "git.golem15.com/golem15/summercms/modules/bouncer" "git.golem15.com/golem15/summercms/modules/lagoon" ) // JWTBlacklistSweep deletes the expired rows of the JWT blacklist. It uses // the blacklist store the plugin published or, when it runs as a console // command without a published database, a store over a database opened from // the config for the duration of the run. func JWTBlacklistSweep(app *backpack.App) bonfire.Command { return bonfire.Command{ Name: "user:jwt-blacklist-sweep", Description: "Delete the expired JWT blacklist rows", Long: `Deletes the jwt_blacklist rows whose expires_at has passed. A row that still blocks a token or its refresh is not touched, so running it twice changes nothing. The scheduler runs it every golem15.user.jwt.blacklist_sweep_interval on the job worker's scheduled queue. Run it by hand or from cron when no worker runs that queue. It ends with "Deleted the expired JWT blacklist rows."`, Examples: []bonfire.Example{ {Command: "user:jwt-blacklist-sweep", Description: "Delete the expired JWT blacklist rows"}, {Command: "user:jwt-blacklist-sweep -q", Description: "Run it quietly, printing only the result line"}, }, Run: func(ctx context.Context, _ bonfire.Input, out bonfire.Output) error { if app == nil { return fmt.Errorf("user:jwt-blacklist-sweep: app is nil") } store, ok := app.Lookup[bouncer.BlacklistStore]() if !ok || store == nil { sqlDB, _, err := lagoon.OpenFromApp(ctx, app) if err != nil { return fmt.Errorf("user:jwt-blacklist-sweep: %w", err) } defer sqlDB.Close() store = bouncer.NewPostgresBlacklist(sqlDB, classes.JWTBlacklistTable) } if err := store.Sweep(ctx, time.Now()); err != nil { return fmt.Errorf("user:jwt-blacklist-sweep: %w", err) } out.Success("Deleted the expired JWT blacklist rows.") return nil }, } }