Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
8.8 KiB
Design
Full design with approaches and the user's approved calls: docs/superpowers/specs/2026-11-30-admin-login-security-design.md.
Context
- Admin sign-in path: the portal's
/api/auth/loginroute calls mobile-apiPOST /api/admin/auth/login(AdminAuthController). That calls the gateway, then services-auth/auth/authenticate, thenloadUserByToken. Nothing is counted or recorded. - Only one way in: only the admin portal is public (Traefik;
docs/deployment.mdsection 8). Only mobile-api holds a gateway key (appkeys.json, section 7). SoAdminAuthController.loginsees every admin sign-in. - Existing settings rows in
service-system.system_property:ADMIN_LOGIN_BLOCK_PERIOD(typeADMIN_LOGIN_BLOCK, value 1, status Y);ADMIN_LOGIN_BLOCK_TIME_UNIT(value M);- the
ADMIN_PWD_FORMATrules (active: minimum length 8, 1 uppercase, 1 lowercase). - Nothing reads the block rows today.
- Password checks: services-system
UserController.adminPasswordRuleFailuresappliesADMIN_PWD_FORMAT. services-system ownsBCryptPasswordEncoder(Config), and/setAdminPasswordhashes there. - Reference: babyboo archive
2026-09-28-add-admin-login-security. It enforces in services-auth and adds session policy; Co-Lite does neither.
Goals / Non-Goals
Goals: login history, lockout with unlock, editable lockout and password rules, own password change.
Non-Goals:
- session policy;
- MFA and CAPTCHA;
- password aging;
- non-admin logins;
- purging history.
Decisions
-
Table
login_auditinservice-system:- Columns:
id,company_idNULL,usernameVARCHAR(191),datetime,eventVARCHAR(20),ip_addressVARCHAR(64),user_agentVARCHAR(512),messageVARCHAR(255). - Keys:
(username, datetime)and(event, datetime). - Values are cut to the column sizes before saving.
company_idis filled from the user whoseusernamematches, else NULL.
- Columns:
-
Lock rule, owned by services-system (
LoginSecurityService):- Settings:
onis the statusYonADMIN_LOGIN_BLOCK_PERIOD;periodis its value;unitisADMIN_LOGIN_BLOCK_TIME_UNIT(S, M, H or D);maxis the new rowADMIN_LOGIN_MAX_ATTEMPT(typeADMIN_LOGIN_BLOCK).
- Locked means
onandcount(LOGIN_FAIL where username = ? and datetime > greatest(now - period, latestReset)) >= max.latestResetis the newestLOGIN_SUCCESS,UNLOCK_RESETorPASSWORD_CHANGEDfor that username. - Lock end is the datetime of the oldest counted failure that keeps the count at
max, plus the period. The locked list uses it, and so do the tests. - Username matching: the lock rule uses the trimmed username, cut to 191 characters, the same key the history stores.
login_audit.usernameusesutf8mb4_general_ci, so it compares likeuser.username(case-insensitive).
- Settings:
-
services-system endpoints (
LoginSecurityController, under/loginSecurity; the gateway gets a matching route):GET isLocked/{username}returns{locked}.POST recordtakes{username, event, ip, userAgent, message}.POST audittakes{username, event, from, to, page, size}. Dates areyyyy-MM-dd. It returns newest first, with size capped at 100.GET lockedreturns[{username, failures, lastFailure, lockedUntil}]. It considers usernames with aLOGIN_FAILinside the period.POST unlocktakes{username, by}and writesUNLOCK_RESETwithmessage = "unlocked by <by>".GET configandPOST configtake{enabled, maxAttempts, period, unit}. They validatemaxAttemptsbetween 1 and 1000,periodbetween 1 and 10000, and the unit in S, M, H or D, and return 400 otherwise. The upper limits keep the lock window computable.GET passwordRulesandPOST passwordRulestake[{id, status, value}]. They only update existingADMIN_PWD_FORMATrows, and refuse any id outside that type with 400.
-
services-system
/user/changeAdminPasswordtakes{id, currentPassword, newPassword}and returns one result string:OK;NOT_FOUND: not a type A user, or not enabled;CURRENT_PASSWORD_WRONG: the bcrypt match fails;PASSWORD_SAME;PASSWORD_POLICY:adminPasswordRuleFailuresis not empty.- On
OKit saves the bcrypt of the new password and keepslogin = Y.
-
mobile-api sign-in (
AdminAuthController.login):- Blank fields: 401
BAD_LOGIN, nothing recorded. - Then
isLocked:- Locked: record
LOGIN_LOCKEDand return 401{result: "false", code: "ACCOUNT_LOCKED", message: "Too many failed sign-ins. Try again later or ask an administrator to unlock your account."}. - Error: log a warning and continue (fail open).
- Locked: record
- Then the existing check:
- an unknown, wrong, disabled or upstream-failed check records
LOGIN_FAIL; - a success records
LOGIN_SUCCESS.
- an unknown, wrong, disabled or upstream-failed check records
- The lock check and recording are methods on
AdminAuthenticationService(isLocked, which fails open, andrecord, which catches and logs every exception). No new class, so the existing controller tests and their mocks stay unchanged. - Logout records
LOGOUTfor the token's user.
- Blank fields: 401
-
Client IP and user agent:
- The portal's login and logout routes already send
X-Client-Ip(the lastx-forwarded-forhop, the one the ingress adds, elsex-real-ip) andX-Client-User-Agent(the browser's user agent) to mobile-api, throughapp/lib/server/clientContext.ts, which was ported from babyboo. - mobile-api reads them from the request for the record only.
- They are never used for any decision, since a client can forge them.
- The portal's login and logout routes already send
-
mobile-api
POST /api/admin/auth/change-password(@AdminModule(ANY_ADMIN), notallowPendingReset, so a pending user gets the existing 403PASSWORD_RESET_REQUIRED):- Missing field: 400
REQUIRED_FIELD. - New and confirm differ: 400
PASSWORD_MISMATCH. - Locked: 400
ACCOUNT_LOCKED. - Otherwise call
/user/changeAdminPassword:CURRENT_PASSWORD_WRONG: recordLOGIN_FAIL(messagechange-password) and return 400.PASSWORD_SAMEorPASSWORD_POLICY: return 400 with that code.OK: recordPASSWORD_CHANGEDand return 204.
- Error bodies follow the existing
{code, message}shape the portal shows throught().
- Missing field: 400
-
mobile-api
AdminLoginSecurityController(@AdminModule("BO_LOGIN_SECURITY"),/api/admin/login-security):GET audit;GET locked;POST locked/{username}/unlock(204);GETandPUT config;GETandPUT password-rules.- Unlock and settings changes log
"{action} by {admin}".
-
Portal:
- Header user menu: a "Change password" item opens
ChangePasswordDialog(current, new, confirm). It reuses thePasswordFieldspatterns and calls/api/auth/change-password. - Login page: maps
code === "ACCOUNT_LOCKED"to the locked message. /login-security: gated byRequireModuleandROUTE_MODULES["/login-security"] = "BO_LOGIN_SECURITY". Three tabs:- History: a DataTable with
useListFiltersanduseFitPageSize, filtered by username, event and From/ToDatePicker. - Locked: rows with an Unlock button and a confirm dialog.
- Settings: a lockout form, then a rules table with an on/off switch and a value.
- History: a DataTable with
- Proxy:
app/api/login-security/[[...path]]/route.tsfor GET, POST and PUT, refusing dot segments.app/api/auth/change-password/route.ts.
- Header user menu: a "Change password" item opens
-
Module row:
(41000000, 'BO_LOGIN_SECURITY', 'Login security', 'BO', 'M', 40000000, '/login-security', 'Shield'). No role grants, so only super admins see it until granted.
Risks / Trade-offs
- Targeted lockout: anyone can lock a real username for one period by failing on purpose. Mitigations: the lock expires, and admins can unlock. Counting unknown usernames keeps locks from revealing which accounts exist.
- Fail open: when services-system is down, the lock is not enforced. Accepted: an outage must not lock every admin out, and the password check still runs.
- Concurrency: two simultaneous wrong guesses may both pass the pre-check, so the count can exceed
maxby the number of parallel requests. Accepted; it is a limit, not a security boundary. - History growth: no purge. One company and a handful of staff make this small. A purge can come with the audit viewer.
- Forged IP or user agent: these are stored for reading only, and never trusted.
- Unauthenticated writes behind the gateway key:
/loginSecurity/recordneeds no user token, so it can writeLOGIN_SUCCESSorPASSWORD_CHANGEDrows, and both clear a lock. The older/blockLoginPeriod/**routes also skip the token, and their save can rewrite theADMIN_LOGIN_BLOCKrows. Both are reachable only with a gateway api-key from an allowed IP, which today only mobile-api holds (docs/deployment.mdsection 7). A later change can narrow the/blockLoginPeriod/bypass to its read endpoints.