auth: housekeeping job — nothing removes stale sessions, login sessions or device codes

Open
#1,253 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
32/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
mongodb, postgresql, python

Research direction

Start with the existing sweep functions at campus/auth/resources/session.py:84 and campus/auth/resources/device_code.py:286, then docs/auth-login-flow.md and docs/auth-token-invariants.md for the delete and keep rules. The open decisions (session grace period, login_sessions retention) need a maintainer answer before code. Done means a cron-invoked, idempotent, chunked job that deletes only the listed expired records, never credentials or their tokens, and treats an empty match as success.

Written by the indexing model from the issue text.

Description

enhancement

Part of #1243 (cron jobs under Railway serverless). campus.audit gets a job for rollups and retention (#1249); campus.auth needs one too.

What is true today

campus.auth has two sweep functions and nothing calls either. No timer, startup hook, script or caller exists in campus, campus-core, campus-common or campus-model. There are no TTL indexes: both Mongo initialisation hooks are pass.

Store Backend Expires Removed when Grows by
auth_sessions Mongo expires_at, 10 min finalize; sweep() exists (campus/auth/resources/session.py:84) and is reachable only through POST /auth/v1/sessions/sweep, whose only callers are tests One per abandoned login flow
login_sessions Mongo expires_at, 30 days Logout; replaced by the next login from the same browser cookie. No sweep One per login that never logs out, and per CLI login, which has no cookie to replace
device_codes Mongo expires_at, 600 s Claim at token issue. sweep() exists (campus/auth/resources/device_code.py:286) with no caller One per abandoned device flow
tokens Mongo expires_at, 7 days Revoke, supersede, disconnect Only through two operator-only paths that orphan a document
yapper events Postgres none Nothing in the suite One per emitted event

The Postgres tables (credentials, app_credentials, users, vault_clients, access_grants, vault) are bounded by users, clients and admin actions and carry no expiry.

Expired records are ignored at read time, or marked (device_codes get state="expired"), never removed. Expired-but-unswept sessions appear in the admin session list, which loads the whole collection and paginates in Python.

The docs say otherwise: docs/auth-login-flow.md states that the 10-minute session TTL "is enforced by the sweep job (POST /auth/v1/sessions/sweep), not on every read". No such job exists, so a stale campus session can still complete a login.

What a job must and must not delete

Delete:

  • auth_sessions past expires_at, with a grace period of a day or more. A login that pauses for more than 10 minutes is otherwise cut off.
  • login_sessions past expires_at.
  • device_codes past expires_at.
  • tokens documents that no credentials.token_id or app_credentials.token_id references.

Leave alone:

  • credentials rows, and the tokens document a row points to, even when expires_at has passed. The row is the long-lived grant (docs/auth-token-invariants.md A4), the refresh grant resolves through that document, and the code exchange unions scopes from it. Deleting either forces a re-login or silently drops accumulated scopes.
  • users, vault_clients, access_grants, vault and app_credentials.

Shape

A Railway cron job, per #1249: catch-up work, chunked, idempotent, with each run recorded in a ledger. The job needs a credential, which raises the question of how the sweep is invoked:

  • Calling POST /auth/v1/sessions/sweep over HTTP needs a credential for campus.auth and wakes the service. The route now validates at_time (see the fix PR), but it has no operator gate: any authenticated client may call it, and it loads every session and deletes one at a time.
  • Running the sweep in-process against storage, as the campus-migrate job does for migrations, avoids both. It needs no API surface and no credential.

The second looks right. It also gives login_sessions and device_codes their missing callers.

Sharp edges for whoever writes it

  • delete_matching({}) deletes everything in both backends. Never call it with an empty query.
  • delete_matching raises NoChangesAppliedError when nothing matched, in both the Postgres and Mongo backends. A run with nothing to do is a success.
  • between is not translated for Mongo. Use lt or lte.
  • A retention figure for login_sessions is a decision: docs/auth-login-flow.md calls them "login audit records", and they carry device_id and user_agent. Logout already deletes them, so the code does not treat them as durable, but deleting them after 30 days ends any "who logged in from where" look-back.

To decide

  • Grace period for auth_sessions.
  • Whether login_sessions are audit records (keep for N months after expiry) or working state (delete at expiry).
  • Whether yapper events get a retention too.
Dominant language
Python
Stars
0
Forks
2
Avg merge
39m
Merged PRs (30d)
235

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from nyjc-computing/campus

All issues in nyjc-computing/campus

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.