oauth: automatically clean up expired oauth state in db

This commit is contained in:
2026-01-05 15:56:53 -07:00
parent 55a7fcfede
commit e3e996ca7a
3 changed files with 63 additions and 7 deletions
+18
View File
@@ -81,6 +81,24 @@ func New(ctx context.Context, db *pgxpool.Pool) (*Authenticator, error) {
}, nil
}
func (a *Authenticator) RunBackgroundCleanup(ctx context.Context) error {
for {
if _, err := a.db.Exec(ctx, "DELETE FROM oauth_tokens WHERE expiry < NOW()"); err != nil {
return fmt.Errorf("failed to delete all oauth_tokens rows that are expired: %w", err)
}
if _, err := a.db.Exec(ctx, "DELETE FROM oauth_login_states WHERE expiration < NOW()"); err != nil {
return fmt.Errorf("failed to delete all oauth_login_states rows that are expired: %w", err)
}
select {
case <-ctx.Done():
return nil
case <-time.After(time.Minute):
}
}
}
// Exchange exchanges an auth code for an access token.
func (a *Authenticator) Exchange(ctx context.Context, state, code string) (accessToken string, expiration time.Time, err error) {
// validate state