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
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 doesend_presented, thensecond_factor.beginordevice_codes.complete, then eitherpending_answer(state, via)ormember+next+sessions.remember_device. - second step:
routes/second_factor.py:80-87(enrolment) and:124-135(verify) repeat theremember_device+member+nexttail. - What is wrong: the password login builds its member with
human_member(routes/auth.py:162-164), the others withmember_of(routes/auth.py:97-100), which addsinterface_language. So the device-code, mail and second-factor completions and the logout (:238) carryinterface_language, and the password login andPOST /auth/password(:274-275) do not. docs/api.md:6130-6132 says only the member ofGET /meandGET /auth/sessioncarries 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 insideauth/second_factor.py:538-539,:603-604,:625-626,:672-673.routes/second_factor.py:75and:109check it, andsecond_factor.confirm/second_factor.verifycheck it again. - the row:
routes/auth.py:290-292and:429-430(_session),auth/second_factor.py:247-248(_session),routes/second_factor.py:112-114and:148-150,routes/sign_in_settings.py:32-34,routes/notifications.py:353-355,routes/restricted.py:74. - Proposal:
auth.caller.require_session(caller) -> strandauth.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-36androutes/forge_links.py:32-38(agenthumans_only, otherwise no cookieforbidden);routes/orgs.py:46-51(the same, but no cookie needed);routes/device_codes.py:73-79(_creator: agent and runner bothhumans_only, no cookieforbidden);routes/auth.py:421-426(_session_caller:humans_only, then no cookieunauthenticated);routes/me.py:135-138(agenthumans_only, runner or no cookieunauthenticated). - What is wrong: the same caller (an agent, a runner, a human with a bearer credential) gets
humans_only, plainforbiddenorunauthenticateddepending on the route, and each route writes the test by hand. - Proposal: one dependency in
deps.py(for exampleHumanSession) 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 selecthuman_sessionbyid_hashand testended_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:19androutes/email_sign_in.py:32importmember_of,pending_answer,end_presented,set_session_cookiesandLOGIN_BY_ADDRESSfromroutes/auth.py.routes/email_sign_in.py:60and:347call the privatedevice_codes._anonymous;routes/password_reset.py:30imports the privateemail_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-43and used fromroutes/email_sign_in.py:103-106,:235and: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.pyorroutes/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(_environmentand_resolved),routes/device_codes.py:371-374,routes/invitations.py:318-321,routes/register_page.py:151-152and:314-317. All writehub.registration or registration.RegistrationEnvironment()and thenregistration.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 thisApiError(403, "forbidden", message, {"reason": reason}). - Proposal: move
refusedtoerrors.pyand 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:101and:105:900forexpires_inand the cookie'smax_age; the link's life isauth/email_links.py:23LIFETIME(15 minutes).routes/registration.py:48PASSWORD_MIN = 12copiesauth/passwords.py:12MIN_LENGTH.routes/auth.py:248androutes/password_reset.py:249write "at least 12 characters";cli.py:329formats 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 toauth/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 onlyGET /meandGET /auth/sessionas §6.2 says now? - Item 3: which answer is right for an agent or runner on a human-session route:
humans_onlyfor both, orhumans_onlyfor an agent and plainforbiddenfor a runner (docs/api.md:194)? Decide once, then the shared guard applies it.
Covered elsewhere
- The
routes/invitations.py:383-384TODO (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 inrate_count: tsk-2y0qbc1zm7r1. routes/registration.py:301-303androutes/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.