Files
co-lite/openspec/changes/add-admin-login-security/design.md
T

7.9 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/login route calls mobile-api POST /api/admin/auth/login (AdminAuthController). That calls the gateway, then services-auth /auth/authenticate, then loadUserByToken. Nothing is counted or recorded.
  • Only one way in: only the admin portal is public (Traefik; docs/deployment.md section 8). Only mobile-api holds a gateway key (appkeys.json, section 7). So AdminAuthController.login sees every admin sign-in.
  • Existing settings rows in service-system.system_property:
    • ADMIN_LOGIN_BLOCK_PERIOD (type ADMIN_LOGIN_BLOCK, value 1, status Y);
    • ADMIN_LOGIN_BLOCK_TIME_UNIT (value M);
    • the ADMIN_PWD_FORMAT rules (active: minimum length 8, 1 uppercase, 1 lowercase).
    • Nothing reads the block rows today.
  • Password checks: services-system UserController.adminPasswordRuleFailures applies ADMIN_PWD_FORMAT. services-system owns BCryptPasswordEncoder (Config), and /setAdminPassword hashes 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

  1. Table login_audit in service-system:

    • Columns: id, company_id NULL, username VARCHAR(191), datetime, event VARCHAR(20), ip_address VARCHAR(64), user_agent VARCHAR(512), message VARCHAR(255).
    • Keys: (username, datetime) and (event, datetime).
    • Values are cut to the column sizes before saving.
    • company_id is filled from the user whose username matches, else NULL.
  2. Lock rule, owned by services-system (LoginSecurityService):

    • Settings:
      • on is the status Y on ADMIN_LOGIN_BLOCK_PERIOD;
      • period is its value;
      • unit is ADMIN_LOGIN_BLOCK_TIME_UNIT (S, M, H or D);
      • max is the new row ADMIN_LOGIN_MAX_ATTEMPT (type ADMIN_LOGIN_BLOCK).
    • Locked means on and count(LOGIN_FAIL where username = ? and datetime > greatest(now - period, latestReset)) >= max. latestReset is the newest LOGIN_SUCCESS, UNLOCK_RESET or PASSWORD_CHANGED for 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 compares the trimmed username exactly. MySQL's default collation makes that comparison case-insensitive, which is acceptable.
  3. services-system endpoints (LoginSecurityController, under /loginSecurity; the gateway gets a matching route):

    • GET isLocked/{username} returns {locked}.
    • POST record takes {username, event, ip, userAgent, message}.
    • POST audit takes {username, event, from, to, page, size}. Dates are yyyy-MM-dd. It returns newest first, with size capped at 100.
    • GET locked returns [{username, failures, lastFailure, lockedUntil}]. It considers usernames with a LOGIN_FAIL inside the period.
    • POST unlock takes {username, by} and writes UNLOCK_RESET with message = "unlocked by <by>".
    • GET config and POST config take {enabled, maxAttempts, period, unit}. They validate maxAttempts and period ≥ 1 and the unit in S, M, H or D, and return 400 otherwise.
    • GET passwordRules and POST passwordRules take [{id, status, value}]. They only update existing ADMIN_PWD_FORMAT rows, and refuse any id outside that type with 400.
  4. services-system /user/changeAdminPassword takes {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: adminPasswordRuleFailures is not empty.
    • On OK it saves the bcrypt of the new password and keeps login = Y.
  5. mobile-api sign-in (AdminAuthController.login):

    • Blank fields: 401 BAD_LOGIN, nothing recorded.
    • Then isLocked:
      • Locked: record LOGIN_LOCKED and 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).
    • Then the existing check:
      • an unknown, wrong, disabled or upstream-failed check records LOGIN_FAIL;
      • a success records LOGIN_SUCCESS.
    • Recording goes through LoginAuditClient.record(...), which catches and logs every exception.
    • Logout records LOGOUT for the token's user.
  6. Client IP and user agent:

    • The portal's login and logout routes send X-Client-IP (the first entry of the incoming x-forwarded-for, else empty) and User-Agent to mobile-api.
    • mobile-api reads them from the request for the record only.
    • They are never used for any decision, since a client can forge them.
  7. mobile-api POST /api/admin/auth/change-password (@AdminModule(ANY_ADMIN), not allowPendingReset, so a pending user gets the existing 403 PASSWORD_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: record LOGIN_FAIL (message change-password) and return 400.
      • PASSWORD_SAME or PASSWORD_POLICY: return 400 with that code.
      • OK: record PASSWORD_CHANGED and return 204.
    • Error bodies follow the existing {code, message} shape the portal shows through t().
  8. mobile-api AdminLoginSecurityController (@AdminModule("BO_LOGIN_SECURITY"), /api/admin/login-security):

    • GET audit;
    • GET locked;
    • POST locked/{username}/unlock (204);
    • GET and PUT config;
    • GET and PUT password-rules.
    • Unlock and settings changes log "{action} by {admin}".
  9. Portal:

    • Header user menu: a "Change password" item opens ChangePasswordDialog (current, new, confirm). It reuses the PasswordFields patterns and calls /api/auth/change-password.
    • Login page: maps code === "ACCOUNT_LOCKED" to the locked message.
    • /login-security: gated by RequireModule and ROUTE_MODULES["/login-security"] = "BO_LOGIN_SECURITY". Three tabs:
      • History: a DataTable with useListFilters and useFitPageSize, filtered by username, event and From/To DatePicker.
      • 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.
    • Proxy: app/api/login-security/[[...path]]/route.ts for GET, POST and PUT, refusing dot segments. app/api/auth/change-password/route.ts.
  10. Module row: (41000000, 'BO_LOGIN_SECURITY', 'Login security', 'BO', 'M', 40000000, '/login-security', 'ShieldCheck'). 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 max by 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.