oliver-zehentleitner / maity

clean-up: G7 sign-in routes: one sign-in completion, one session guard, shared helpers out of routes/auth.py and routes/device_codes.py

Sign in to report

tsk-38hws7ep0jha Open

From the clean-up audit tsk-hgnbapyw7hnh (uncovered files, G7 sign-in, joining, landing and notification routes), read at 9dfd4b65. Every line number is at 9dfd4b65. Paths are under hub/src/maity_hub/ unless they start with docs/.

The module code of area 14 (auth/) is on tsk-j967n09yz6ps, tsk-f5sdcgr7vzj0 and tsk-fr6hmpdabmgr. This card is the route layer above it.

1. Duplication and docs drift: completing a sign-in is written three times, and the answers differ

  • Kind: duplication, docs drift.
  • Files:
  • first factor: routes/auth.py:142-169 (password login), routes/device_codes.py:329-347 (device code), routes/email_sign_in.py:208-229 (_complete, mail link and mail code). Each does end_presented, then second_factor.begin or device_codes.complete, then either pending_answer(state, via) or member + next + sessions.remember_device.
  • second step: routes/second_factor.py:80-87 (enrolment) and :124-135 (verify) repeat the remember_device + member + next tail.
  • What is wrong: the password login builds its member with human_member (routes/auth.py:162-164), the others with member_of (routes/auth.py:97-100), which adds interface_language. So the device-code, mail and second-factor completions and the logout (:238) carry interface_language, and the password login and POST /auth/password (:274-275) do not. docs/api.md:6130-6132 says only the member of GET /me and GET /auth/session carries it. The dashboard reads it from the session only (web/js/api.js:6079), so nothing breaks today.
  • Proposal: one finish_sign_in(conn, request, human_id, state, next) that returns the answer and the device value, and one member builder for every sign-in answer. Then align code and §6.2 (see the open question).
  • Effort: M. Risk: low to medium (answer shapes and cookies; test_device_codes, test_email_sign_in and test_two_factor cover them).

2. Duplication: "needs a dashboard session" and "read the caller's session row" are written in many places

  • Kind: duplication.
  • Files:
  • the guard: routes/auth.py:440-443 (_require_cookie), routes/second_factor.py:46-49 (_session), routes/sign_in_settings.py:29-31, and inside auth/second_factor.py:538-539, :603-604, :625-626, :672-673. routes/second_factor.py:75 and :109 check it, and second_factor.confirm / second_factor.verify check it again.
  • the row: routes/auth.py:290-292 and :429-430 (_session), auth/second_factor.py:247-248 (_session), routes/second_factor.py:112-114 and :148-150, routes/sign_in_settings.py:32-34, routes/notifications.py:353-355, routes/restricted.py:74.
  • Proposal: auth.caller.require_session(caller) -> str and auth.caller.session_row(conn, caller); drop the route-level copies where the domain function checks anyway.
  • Effort: S. Risk: low (same error, same message).

3. Duplication: the "a human with a cookie session" guard, with different answers per route

  • Kind: duplication, design.
  • Files: routes/notifications.py:59-64, routes/notification_settings.py:30-36 and routes/forge_links.py:32-38 (agent humans_only, otherwise no cookie forbidden); routes/orgs.py:46-51 (the same, but no cookie needed); routes/device_codes.py:73-79 (_creator: agent and runner both humans_only, no cookie forbidden); routes/auth.py:421-426 (_session_caller: humans_only, then no cookie unauthenticated); routes/me.py:135-138 (agent humans_only, runner or no cookie unauthenticated).
  • What is wrong: the same caller (an agent, a runner, a human with a bearer credential) gets humans_only, plain forbidden or unauthenticated depending on the route, and each route writes the test by hand.
  • Proposal: one dependency in deps.py (for example HumanSession) with the answers of docs/api.md:194, and a message argument per route.
  • Effort: S. Risk: low to medium; the answer changes on some routes (see the open question).

4. Duplication: "is this session row alive" three times

  • Kind: duplication.
  • Files: routes/notifications.py:77-82 (_require_live), routes/email_sign_in.py:64-68 (inside _anonymous), auth/caller.py:303-316 (has_live_session). All three select human_session by id_hash and test ended_at is None and session_alive(row, now).
  • Proposal: auth.caller.live_session(conn, id_hash) -> row | None, used by the three.
  • Effort: S. Risk: low.

5. Design: the sign-in route modules import each other, private names included

  • Kind: design (layering inside routes/).
  • Files:
  • routes/device_codes.py:38, routes/second_factor.py:19 and routes/email_sign_in.py:32 import member_of, pending_answer, end_presented, set_session_cookies and LOGIN_BY_ADDRESS from routes/auth.py.
  • routes/email_sign_in.py:60 and :347 call the private device_codes._anonymous; routes/password_reset.py:30 imports the private email_sign_in._anonymous, which calls it.
  • The pending cookie of §2.6.5 (docs/api.md:1883-1888), shared by the device code and the mail flows, is defined in routes/device_codes.py:42-43 and used from routes/email_sign_in.py:103-106, :235 and :253.
  • What is wrong: the shared sign-in pieces live in two of the route modules that use them. (This is route to route, so it is not part of tsk-dwrtrhdgzezs, which is about domain modules.)
  • Proposal: one module for them, for example auth/browser.py or routes/sign_in_common.py: the anonymous guard, the pending cookie, the completion of item 1, the cookie setters.
  • Effort: M. Risk: low (a move).

6. Duplication: "the registration settings in effect" is resolved in five places

  • Kind: duplication, debt.
  • Files: routes/registration.py:93-101 (_environment and _resolved), routes/device_codes.py:371-374, routes/invitations.py:318-321, routes/register_page.py:151-152 and :314-317. All write hub.registration or registration.RegistrationEnvironment() and then registration.resolve(conn, env, registration.mail_ready(conn, hub), hub.public_url). routes/registration.py:99 (found = hub.registration if hub.registration is not None else None) is a no-op.
  • Proposal: registration.resolved(conn, hub) in the domain module, used by all five.
  • Effort: S. Risk: none.

7. Duplication: the method_disabled refusal is built inline three times

  • Kind: duplication.
  • Files: routes/device_codes.py:95-97, routes/email_sign_in.py:54-56, routes/password_reset.py:55-57. auth/second_factor.py:476-477 (refused) already builds exactly this ApiError(403, "forbidden", message, {"reason": reason}).
  • Proposal: move refused to errors.py and use it in all four places.
  • Effort: S. Risk: none.

8. Debt: literals that copy a constant

  • Kind: debt.
  • Files:
  • routes/email_sign_in.py:101 and :105: 900 for expires_in and the cookie's max_age; the link's life is auth/email_links.py:23 LIFETIME (15 minutes).
  • routes/registration.py:48 PASSWORD_MIN = 12 copies auth/passwords.py:12 MIN_LENGTH.
  • routes/auth.py:248 and routes/password_reset.py:249 write "at least 12 characters"; cli.py:329 formats it from the constant.
  • Proposal: use the constants.
  • Effort: S. Risk: none.

Order

  • Item 5 first, or in the same PR as item 1: item 1's helper goes into the new module.
  • tsk-j967n09yz6ps, tsk-f5sdcgr7vzj0 and tsk-fr6hmpdabmgr edit auth/; items 2 and 4 add to auth/caller.py, so rebase rather than conflict.

Open questions

  • Item 1: should the member of every sign-in answer carry interface_language (and §6.2 say so), or only GET /me and GET /auth/session as §6.2 says now?
  • Item 3: which answer is right for an agent or runner on a human-session route: humans_only for both, or humans_only for an agent and plain forbidden for a runner (docs/api.md:194)? Decide once, then the shared guard applies it.

Covered elsewhere

  • The routes/invitations.py:383-384 TODO (accept only from a session in good standing): tsk-vk79jr6gsexm.
  • The registration limits copy, the device-code breaker and the per-owner caps in orgs.create: tsk-5sfts81hr516. The plain address in rate_count: tsk-2y0qbc1zm7r1.
  • routes/registration.py:301-303 and routes/landing.py:23-25 _require_admin: tsk-hbpjgp03esez item 1.
  • lock_address (routes/registration.py:258-264): tsk-dwrtrhdgzezs.
  • The anonymous entry guards (Sec-Fetch-Site, credential refusal): the separate security-flagged card of this audit group.

Effort sum: 2 M + 6 S.