122 Commits

Author SHA1 Message Date
Quentin Gliech
bdca9cc6b3 Change queue_schedules FK to ON DELETE SET NULL
The cleanup-queue-jobs job was failing because it tried to delete
completed/failed jobs still referenced by `queue_schedules.last_scheduled_job_id`.
The FK defaulted to RESTRICT, blocking the delete.

Change it to `ON DELETE SET NULL` so cleanup can proceed, matching the
existing semantics (NULL = "never scheduled or cleaned up").

Fixes #5545
2026-03-24 11:44:23 +01:00
Quentin Gliech
93a72203a7 Clear out last active IP on each sessions after 30 days (#5448) 2026-01-26 13:57:01 +01:00
Quentin Gliech
4806da8f2d Cleanup finished user/browser sessions (#5444) 2026-01-23 18:27:45 +01:00
Quentin Gliech
7688a8f8b7 Cleanup finished OAuth 2.0 sessions (#5443) 2026-01-23 18:23:55 +01:00
Quentin Gliech
845a448b71 Re-enable the upstream authentication sessions cleanup job (#5439) 2026-01-23 18:17:11 +01:00
Quentin Gliech
9afb0dd4ce Clean up leftovers in the database schema, part 2 (#5408) 2026-01-23 18:13:29 +01:00
Quentin Gliech
82ba74d34f Clean up unsupported threepids from already deactivated users (#5407) 2026-01-23 17:42:46 +01:00
Quentin Gliech
b4025acc80 Add cleanup jobs for inactive session IP addresses
This adds three new scheduled cleanup jobs that clear the last_active_ip
field from sessions that have been inactive for more than 30 days:

- CleanupInactiveOAuth2SessionIpsJob
- CleanupInactiveCompatSessionIpsJob
- CleanupInactiveUserSessionIpsJob

This helps with data minimization by not retaining IP addresses longer
2026-01-23 16:29:29 +01:00
Quentin Gliech
85f71d2200 Add cleanup job for finished user sessions
Implements hard deletion of user/browser sessions that have been finished for more than 30 days, but only after all child sessions are cleaned up.

User sessions can only be deleted when no child sessions exist, ensuring backchannel logout propagation continues to work correctly.
2026-01-22 15:44:57 +01:00
Quentin Gliech
c508c7899e Fix FK constraint to preserve backchannel logout chain
Change compat_sessions.user_session_id FK from ON DELETE SET NULL to ON DELETE NO ACTION. This prevents deletion of user_sessions while compat_sessions still reference them, which is critical for backchannel logout propagation.

When an upstream IdP sends a backchannel logout, MAS must trace through:
  upstream_oauth_authorization_sessions -> user_sessions -> compat_sessions

If user_session_id links are SET NULL, logout propagation fails.

Uses two-step migration (DROP+ADD NOT VALID, then VALIDATE) to minimize table locking during deployment.
2026-01-22 15:44:57 +01:00
Quentin Gliech
3b0937ca8e Add cleanup job for finished OAuth2 sessions
Implements hard deletion of OAuth2 sessions that have been finished for more than 30 days, including their associated access and refresh tokens.
2026-01-22 15:44:56 +01:00
Quentin Gliech
b912fbc0c9 Add trigger and backfill for upstream OAuth user session tracking
Introduce a new trigger and a backfill migration to populate the
`user_session_id` column in `upstream_oauth_authorization_sessions`
based on `user_session_authentications`. This ensures historical data is
consistent and aids in backward compatibility.
2026-01-21 14:49:07 +01:00
Quentin Gliech
76b971ad45 Add index on the user_session_id foreign key for upstream auth sessions 2026-01-21 14:49:07 +01:00
Quentin Gliech
39e417b461 Only cleanup orphan upstream authorization sessions
This includes sessions that were never completed, and sessions where
user_session was cleaned up. This is to avoid breaking features like
OIDC Backchannel Logout after 30 days.
2026-01-21 12:25:42 +01:00
Quentin Gliech
63f02c4dea Track user session authenticated through upstream auth sessions
This will help us avoid clearing upstream authorization sessions that
might still be useful to keep around for OIDC Backchannel Logouts
2026-01-21 12:19:05 +01:00
Quentin Gliech
1254d9f8f0 Mark the next attempt foreign key as initially not valid 2026-01-19 15:44:22 +01:00
Quentin Gliech
1253bbd498 Implement cleanup job for queue jobs
Add scheduled cleanup job that removes old completed and failed queue
jobs after 30 days. Jobs are kept for debugging purposes.

Includes migration to change the next_attempt_id FK constraint from NO
ACTION to SET NULL, allowing cleanup of retry chains without breaking
foreign key constraints.

One caveat is that cleanup is based on their creation time, *not* when
they got completed/failed. This means that if the job takes a long time
(as in, several days) to get scheduled, it might get cleared as soon as
it runs. This is fine for now, we may want to revisit this if we start
scheduling jobs far in the future
2026-01-19 12:25:04 +01:00
Quentin Gliech
e7c07a8f88 Implement cleanup jobs for upstream OAuth sessions and links
Add two cleanup jobs scheduled hourly:

1. Upstream OAuth authorization sessions - removes sessions after 30 days
2. Orphaned upstream OAuth links - removes links after 7 days where user_id IS NULL. These are links created during upstream OAuth 2.0 login but never associated with a user
2026-01-19 12:24:13 +01:00
Quentin Gliech
87f4ec3e80 Cleanup finished compat sessions after 30 days 2026-01-15 12:29:43 +01:00
Quentin Gliech
186a887125 Hard delete expired, revoked and consumed OAuth 2.0 tokens after some time (#5409) 2026-01-13 17:23:16 +01:00
Quentin Gliech
f98957617e Cleanup consumed refresh tokens 2026-01-12 11:18:18 +01:00
Quentin Gliech
ab25c23829 Replace the FK constraint on the refresh token chain to nullify the
field on deletion

This will make garbage collecting refresh tokens easier
2026-01-12 09:36:52 +01:00
Quentin Gliech
3e521a105d Cleanup revoked refresh tokens 2026-01-09 18:37:09 +01:00
Quentin Gliech
04cbafbc5f Cleanup expired OAuth 2.0 access tokens 2026-01-09 13:38:50 +01:00
Quentin Gliech
bf2ad55b5c Speed up access token cleanup with an index 2026-01-08 19:03:09 +01:00
Quentin Gliech
1ff44b957b Clean up unused oauth2_consents table 2026-01-08 16:02:07 +01:00
Quentin Gliech
73cfee575d Clean up unsupported threepids from already deactivated users 2026-01-08 15:42:42 +01:00
Quentin Gliech
215ad75ab2 Cleanup id_token_claims trigger 2026-01-08 15:17:37 +01:00
Quentin Gliech
bc1ca15833 Remove leftovers from OAuth 2.0 consent tracking cleanup 2026-01-08 15:17:37 +01:00
Quentin Gliech
5cdf938129 Remove leftovers from the old email verification system 2026-01-08 15:17:37 +01:00
Quentin Gliech
5067e420c6 Remove leftover in the scheam from apalis 2026-01-08 15:17:25 +01:00
Olivier 'reivilibre
1dd51c77f4 Reapply "Add 'IF NOT EXISTS' to all 'CREATE INDEX CONCURRENTLY' statements to avoid deadlocks (#5297)" (#5299)
This reverts commit 2c3054bb24, reversing
changes made to 396950806c.
2025-12-01 12:45:19 +00:00
Olivier 'reivilibre
21e45ce972 Revert "Add 'IF NOT EXISTS' to all 'CREATE INDEX CONCURRENTLY' statements to avoid deadlocks (#5297)"
This reverts commit 396950806c, reversing
changes made to ef563f33c6.
2025-12-01 12:14:31 +00:00
Ben Banfield-Zanin
e065f830e9 Add 'IF NOT EXISTS' to all 'CREATE INDEX CONCURRENTLY' statements to avoid deadlocks 2025-11-28 15:18:53 +00:00
Quentin Gliech
bd3173baa1 Create the new index CONCURRENTLY 2025-11-27 16:04:23 +01:00
Quentin Gliech
c9b89c2f8d Store upstream OAuth sessions on user registrations
This will allow us creating user registrations from upstream OAuth auth
sessions
2025-11-21 19:31:37 +01:00
Olivier 'reivilibre
07f77778af Fix UNIQUE constraint on active personal access tokens per session 2025-10-23 15:01:39 +01:00
Olivier 'reivilibre
b9e1cdb554 Support OAuth2 clients as owners of personal sessions 2025-10-07 19:54:59 +01:00
Olivier 'reivilibre
4920bab254 Add tables for personal access tokens 2025-10-07 13:12:54 +01:00
Quentin Gliech
cb8c408489 Admin API filter to search users by username 2025-09-15 14:12:31 +02:00
matrixbot
38f5b8dac8 Automatic merge back to main (#4781) 2025-07-10 17:28:11 +02:00
Quentin Gliech
eb444b28b1 Split the migration in two parts, two transactions. 2025-07-09 15:10:53 +02:00
Quentin Gliech
8b2da5b291 Only apply the trigger on rows without the id_token_claims set
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-07-09 14:55:02 +02:00
Quentin Gliech
d625aaf530 Backfill the id_token_claims column in the upstream_oauth_authorization_sessions table 2025-07-09 14:43:47 +02:00
Quentin Gliech
39b3dbe5db Make email address lookups case-insensitive 2025-07-08 18:01:20 +02:00
Quentin Gliech
db8c557f81 Backchannel logout behavior settings on upstream providers 2025-07-04 16:27:10 +02:00
Quentin Gliech
aaf4bf588f Allow filtering upstream sessions by sub and sid claims 2025-07-04 16:27:09 +02:00
Quentin Gliech
5b7bf232d6 Record the decoded ID token claims on upstream auth sessions 2025-07-04 16:27:09 +02:00
Quentin Gliech
e28221ac49 Data model and repository for user registration tokens 2025-06-03 17:42:52 +02:00
Quentin Gliech
955bd28590 Don't generate and send a nonce for non-OIDC-compliant auth requests 2025-05-07 15:34:27 +02:00