docs(openspec): propose add-admin-login-security
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
1 parent
c82498e5d8
commit
bbf268e00a
6 files changed
+482
No files matched your search
@@ -0,0 +1,2 @@
|
|||||||
|
schema: spec-driven
|
||||||
|
created: 2026-11-30
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
# 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.
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
# Proposal
|
||||||
|
|
||||||
|
## Why
|
||||||
|
|
||||||
|
Go-live is 2027-01-15. Today the admin login counts nothing: anyone who reaches the login page can guess passwords without limit, and nothing records who signed in. Staff also cannot change their own password; only an admin can set a temporary one for them.
|
||||||
|
|
||||||
|
The legacy backoffice blocked a username after 3 failed logins, using an in-memory counter. Co-Lite's new login path (portal, then mobile-api, then services-auth) never ported that rule. babyboo solved the same gap with a persisted `login_audit` table, and this change follows it, trimmed to what Co-Lite needs. The history it keeps will also serve the later audit-log viewer.
|
||||||
|
|
||||||
|
Design: `docs/superpowers/specs/2026-11-30-admin-login-security-design.md`.
|
||||||
|
|
||||||
|
## What Changes
|
||||||
|
|
||||||
|
- **Login history:** every admin login success, failure, locked refusal, logout, unlock and own password change is saved to a new `service-system`.`login_audit` table. Each row has the time, username, client IP and user agent.
|
||||||
|
- **Lockout:**
|
||||||
|
- After a set number of failed logins inside a set period, the username is locked. The defaults are 5 attempts in 15 minutes.
|
||||||
|
- A locked username is refused even with the right password.
|
||||||
|
- The lock ends by itself when the failures age out, or when an admin unlocks it.
|
||||||
|
- The settings reuse the existing `ADMIN_LOGIN_BLOCK` rows in `system_property`, plus one new row for the maximum attempts.
|
||||||
|
- **Enforcement point:** mobile-api `AdminAuthController.login`, the only way in (only the admin portal is public). services-auth is not changed.
|
||||||
|
- **Change my password:** a new `POST /api/admin/auth/change-password` and a dialog in the portal's user menu. It needs the current password and applies the admin password rules. A wrong current password counts as a failed login.
|
||||||
|
- **Login security screen** (`/login-security`, module `BO_LOGIN_SECURITY`):
|
||||||
|
- History tab: login history with filters.
|
||||||
|
- Locked tab: locked usernames with an Unlock button.
|
||||||
|
- Settings tab: edits the lockout settings and the admin password rules (`ADMIN_PWD_FORMAT`).
|
||||||
|
- **Login page:** shows a clear message when the username is locked.
|
||||||
|
- **Not in this change:** session policy (logout revocation, sliding sessions, single session), MFA, CAPTCHA, password aging, member or other non-admin logins, and purging old history.
|
||||||
|
|
||||||
|
## Capabilities
|
||||||
|
|
||||||
|
### New Capabilities
|
||||||
|
- `admin-login-security`: the login history, the lock rule and its enforcement on admin login, unlock, the lockout and password-rule settings, and the Login security screen.
|
||||||
|
- `admin-own-password`: an admin changing their own password.
|
||||||
|
|
||||||
|
### Modified Capabilities
|
||||||
|
None.
|
||||||
|
|
||||||
|
## Impact
|
||||||
|
|
||||||
|
- **`solvway-services-system`:**
|
||||||
|
- A new `LoginAudit` entity and repository, a service and a `/loginSecurity` controller (isLocked, record, audit, locked, unlock, config, password rules).
|
||||||
|
- A new `/user/changeAdminPassword` endpoint that checks the current password with the existing `BCryptPasswordEncoder`.
|
||||||
|
- **`solvway-mobile-api`:**
|
||||||
|
- `AdminAuthController` login and logout record events and check the lock.
|
||||||
|
- New `change-password` endpoint.
|
||||||
|
- New `AdminLoginSecurityController` under `BO_LOGIN_SECURITY`.
|
||||||
|
- **`solvway-admin-portal`:**
|
||||||
|
- The login and logout proxy routes forward the client IP and user agent.
|
||||||
|
- The login page shows the locked message.
|
||||||
|
- Change-password dialog in the header.
|
||||||
|
- New `/login-security` page with proxy routes, a service and i18n keys.
|
||||||
|
- **Database (`sql/` in this change, mirrored in `local/seed/03-colite.sql`):**
|
||||||
|
- Create `login_audit`, with `company_id`.
|
||||||
|
- Insert `ADMIN_LOGIN_MAX_ATTEMPT`.
|
||||||
|
- Set the block period to 15 minutes.
|
||||||
|
- Insert the `BO_LOGIN_SECURITY` module row.
|
||||||
|
- A rollback script.
|
||||||
|
- **Security:** this changes the admin sign-in path. The lock check fails open when services-system errors, so an outage cannot lock everyone out. The audit write is best-effort.
|
||||||
|
- **Docs:** `docs/deployment.md` lists the new SQL.
|
||||||
|
- No new dependency.
|
||||||
@@ -0,0 +1,127 @@
|
|||||||
|
## ADDED Requirements
|
||||||
|
|
||||||
|
### Requirement: Login history
|
||||||
|
|
||||||
|
The system SHALL record every admin sign-in attempt, sign-out, unlock and own password change, with the time, username as typed, event, client IP, user agent and, for a known username, the user's company. Events: `LOGIN_SUCCESS`, `LOGIN_FAIL`, `LOGIN_LOCKED`, `LOGOUT`, `UNLOCK_RESET`, `PASSWORD_CHANGED`. Recording SHALL be best-effort and MUST NOT change the sign-in answer. A blank username or password SHALL NOT be recorded.
|
||||||
|
|
||||||
|
#### Scenario: Successful sign-in is recorded
|
||||||
|
|
||||||
|
- **WHEN** `sales1` signs in with the right password
|
||||||
|
- **THEN** a `LOGIN_SUCCESS` record for `sales1` is saved with the time, client IP and user agent
|
||||||
|
|
||||||
|
#### Scenario: Failed sign-in is recorded
|
||||||
|
|
||||||
|
- **WHEN** someone signs in as `sales1` with a wrong password
|
||||||
|
- **THEN** a `LOGIN_FAIL` record for `sales1` is saved
|
||||||
|
|
||||||
|
#### Scenario: Unknown username is recorded
|
||||||
|
|
||||||
|
- **WHEN** someone signs in as `nobody1`, which matches no user
|
||||||
|
- **THEN** a `LOGIN_FAIL` record for `nobody1` is saved with no company
|
||||||
|
|
||||||
|
#### Scenario: Sign-out is recorded
|
||||||
|
|
||||||
|
- **WHEN** `sales1` signs out
|
||||||
|
- **THEN** a `LOGOUT` record for `sales1` is saved
|
||||||
|
|
||||||
|
#### Scenario: History write failure does not block sign-in
|
||||||
|
|
||||||
|
- **GIVEN** the history cannot be written
|
||||||
|
- **WHEN** `sales1` signs in with the right password
|
||||||
|
- **THEN** the sign-in succeeds
|
||||||
|
- **AND** the write failure is logged
|
||||||
|
|
||||||
|
### Requirement: Lockout after repeated failures
|
||||||
|
|
||||||
|
The system SHALL lock a username when lockout is on and its `LOGIN_FAIL` records reach the configured maximum, counting only records inside the configured period and after its latest `LOGIN_SUCCESS`, `UNLOCK_RESET` or `PASSWORD_CHANGED`. The maximum, period, unit and on/off switch SHALL come from settings. Failures SHALL count for unknown usernames too, so a lock does not reveal whether an account exists. The lock SHALL end once the counted failures leave the period.
|
||||||
|
|
||||||
|
#### Scenario: Maximum reached locks the username
|
||||||
|
|
||||||
|
- **GIVEN** lockout is on with a maximum of 5 in 15 minutes
|
||||||
|
- **WHEN** `sales1` fails to sign in 5 times within 15 minutes
|
||||||
|
- **THEN** `sales1` is locked
|
||||||
|
|
||||||
|
#### Scenario: Old failures do not count
|
||||||
|
|
||||||
|
- **GIVEN** lockout is on with a maximum of 5 in 15 minutes
|
||||||
|
- **WHEN** `sales1` has 4 failures within the last 15 minutes and 3 older ones
|
||||||
|
- **THEN** `sales1` is not locked
|
||||||
|
|
||||||
|
#### Scenario: A success resets the count
|
||||||
|
|
||||||
|
- **WHEN** `sales1` fails 4 times, signs in successfully, then fails 4 more times within 15 minutes
|
||||||
|
- **THEN** `sales1` is not locked
|
||||||
|
|
||||||
|
#### Scenario: Lockout switched off
|
||||||
|
|
||||||
|
- **GIVEN** lockout is off
|
||||||
|
- **WHEN** `sales1` fails 10 times within 15 minutes
|
||||||
|
- **THEN** `sales1` is not locked
|
||||||
|
|
||||||
|
### Requirement: Locked username is refused
|
||||||
|
|
||||||
|
The system SHALL refuse sign-in for a locked username before checking the password, with the code `ACCOUNT_LOCKED` and a message telling the user to try later or ask an administrator, and SHALL record `LOGIN_LOCKED`. When the lock state cannot be read because of an infrastructure error, sign-in SHALL proceed to the normal password check.
|
||||||
|
|
||||||
|
#### Scenario: Right password while locked
|
||||||
|
|
||||||
|
- **GIVEN** `sales1` is locked
|
||||||
|
- **WHEN** `sales1` signs in with the right password
|
||||||
|
- **THEN** sign-in is refused with `ACCOUNT_LOCKED`
|
||||||
|
- **AND** no session is issued
|
||||||
|
- **AND** a `LOGIN_LOCKED` record is saved
|
||||||
|
|
||||||
|
#### Scenario: Lock check unavailable
|
||||||
|
|
||||||
|
- **GIVEN** the lock state cannot be read
|
||||||
|
- **WHEN** `sales1` signs in with the right password
|
||||||
|
- **THEN** the sign-in succeeds
|
||||||
|
|
||||||
|
#### Scenario: Portal shows the locked message
|
||||||
|
|
||||||
|
- **WHEN** sign-in is refused with `ACCOUNT_LOCKED`
|
||||||
|
- **THEN** the login page shows the locked message instead of "Invalid username or password"
|
||||||
|
|
||||||
|
### Requirement: Unlock
|
||||||
|
|
||||||
|
The system SHALL let an admin with `BO_LOGIN_SECURITY` list the usernames that are locked now, with their failure count, last failure time and when the lock ends, and SHALL let that admin unlock one. Unlocking SHALL save an `UNLOCK_RESET` record naming the admin who unlocked.
|
||||||
|
|
||||||
|
#### Scenario: Unlock a locked username
|
||||||
|
|
||||||
|
- **GIVEN** `sales1` is locked
|
||||||
|
- **WHEN** an admin with `BO_LOGIN_SECURITY` unlocks `sales1`
|
||||||
|
- **THEN** `sales1` no longer appears in the locked list
|
||||||
|
- **AND** `sales1` can sign in with the right password
|
||||||
|
- **AND** an `UNLOCK_RESET` record for `sales1` names the admin
|
||||||
|
|
||||||
|
### Requirement: Login history screen
|
||||||
|
|
||||||
|
The system SHALL let an admin with `BO_LOGIN_SECURITY` read the login history, newest first and paged, filtered by username, event and date range.
|
||||||
|
|
||||||
|
#### Scenario: Filter by event
|
||||||
|
|
||||||
|
- **WHEN** an admin filters the history by event `LOGIN_FAIL`
|
||||||
|
- **THEN** only `LOGIN_FAIL` records are listed, newest first
|
||||||
|
|
||||||
|
#### Scenario: Without the module
|
||||||
|
|
||||||
|
- **WHEN** an admin without `BO_LOGIN_SECURITY` calls the login-security API
|
||||||
|
- **THEN** the request is refused with 403
|
||||||
|
|
||||||
|
### Requirement: Lockout and password-rule settings
|
||||||
|
|
||||||
|
The system SHALL let an admin with `BO_LOGIN_SECURITY` read and change the lockout settings (on/off, maximum attempts, period, time unit) and the admin password rules (each rule's on/off status and value). The system SHALL reject a maximum or period below 1 and a time unit other than S, M, H or D. Password rules SHALL only be edited, never added or removed. Every change SHALL write a log line naming the admin.
|
||||||
|
|
||||||
|
#### Scenario: Change the lockout settings
|
||||||
|
|
||||||
|
- **WHEN** an admin sets the maximum to 3 and the period to 30 minutes
|
||||||
|
- **THEN** the next lock decision uses 3 attempts in 30 minutes
|
||||||
|
|
||||||
|
#### Scenario: Invalid setting
|
||||||
|
|
||||||
|
- **WHEN** an admin sets the maximum to 0
|
||||||
|
- **THEN** the change is refused with a validation error and the settings stay as they were
|
||||||
|
|
||||||
|
#### Scenario: Change a password rule
|
||||||
|
|
||||||
|
- **WHEN** an admin sets the minimum length rule to 10
|
||||||
|
- **THEN** a new password of 9 characters is refused by every admin password check
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
## ADDED Requirements
|
||||||
|
|
||||||
|
### Requirement: Change own password
|
||||||
|
|
||||||
|
The system SHALL let a signed-in admin change their own password by giving the current password, a new password and its confirmation. The new password SHALL pass the admin password rules and SHALL differ from the current one. On success the system SHALL keep the admin signed in, keep their first-login state complete, and record `PASSWORD_CHANGED`. An admin whose first-login password change is still pending SHALL use the first-login flow instead.
|
||||||
|
|
||||||
|
#### Scenario: Successful change
|
||||||
|
|
||||||
|
- **WHEN** `sales1` gives the right current password and a new password that passes the rules, twice
|
||||||
|
- **THEN** the new password works at the next sign-in and the old one does not
|
||||||
|
- **AND** `sales1` stays signed in
|
||||||
|
- **AND** a `PASSWORD_CHANGED` record is saved
|
||||||
|
|
||||||
|
#### Scenario: Confirmation differs
|
||||||
|
|
||||||
|
- **WHEN** the new password and its confirmation differ
|
||||||
|
- **THEN** the change is refused with `PASSWORD_MISMATCH`
|
||||||
|
|
||||||
|
#### Scenario: Same as current
|
||||||
|
|
||||||
|
- **WHEN** the new password equals the current password
|
||||||
|
- **THEN** the change is refused with `PASSWORD_SAME`
|
||||||
|
|
||||||
|
#### Scenario: Fails the rules
|
||||||
|
|
||||||
|
- **WHEN** the new password breaks an admin password rule
|
||||||
|
- **THEN** the change is refused with `PASSWORD_POLICY`
|
||||||
|
|
||||||
|
#### Scenario: Pending first login
|
||||||
|
|
||||||
|
- **GIVEN** `sales1` must still complete the first-login password change
|
||||||
|
- **WHEN** `sales1` calls change password
|
||||||
|
- **THEN** the request is refused and `sales1` is sent to the first-login flow
|
||||||
|
|
||||||
|
### Requirement: Wrong current password counts as a failed sign-in
|
||||||
|
|
||||||
|
The system SHALL refuse a change whose current password is wrong with `CURRENT_PASSWORD_WRONG`, and SHALL record it as `LOGIN_FAIL` so that repeated wrong guesses lock the username. A locked username SHALL be refused with `ACCOUNT_LOCKED` before the current password is checked.
|
||||||
|
|
||||||
|
#### Scenario: Wrong current password
|
||||||
|
|
||||||
|
- **WHEN** `sales1` gives a wrong current password
|
||||||
|
- **THEN** the change is refused with `CURRENT_PASSWORD_WRONG`
|
||||||
|
- **AND** a `LOGIN_FAIL` record for `sales1` is saved
|
||||||
|
|
||||||
|
#### Scenario: Locked while changing
|
||||||
|
|
||||||
|
- **GIVEN** `sales1` is locked
|
||||||
|
- **WHEN** `sales1` calls change password with the right current password
|
||||||
|
- **THEN** the change is refused with `ACCOUNT_LOCKED`
|
||||||
|
- **AND** the password is unchanged
|
||||||
@@ -0,0 +1,130 @@
|
|||||||
|
# Tasks
|
||||||
|
|
||||||
|
Design: `design.md`. All tasks run serially: each builds on the one before.
|
||||||
|
|
||||||
|
## 1. Data
|
||||||
|
|
||||||
|
- [ ] 1.1 SQL for the table, settings and module (design decisions 1, 2 and 10).
|
||||||
|
- Files:
|
||||||
|
- `sql/01-login-audit.sql`: `CREATE TABLE IF NOT EXISTS login_audit`.
|
||||||
|
- `sql/02-lockout-settings.sql`: insert `ADMIN_LOGIN_MAX_ATTEMPT` = 5, status Y, type `ADMIN_LOGIN_BLOCK`. Set `ADMIN_LOGIN_BLOCK_PERIOD` to 15.
|
||||||
|
- `sql/03-login-security-module.sql`: the module row.
|
||||||
|
- `sql/99-rollback.sql`: drops the table, deletes both rows, and sets the period back to 1.
|
||||||
|
- Mirror 01 to 03 in `local/seed/03-colite.sql`.
|
||||||
|
- Apply them to the running local database. This is a local-only, additive change.
|
||||||
|
- Scope: `local/seed/03-colite.sql`, `openspec/changes/add-admin-login-security/sql/**`
|
||||||
|
- Verify: `select` shows the table, both settings and the module row.
|
||||||
|
|
||||||
|
## 2. services-system
|
||||||
|
|
||||||
|
- [ ] 2.1 `LoginAudit` entity and repository, `LoginSecurityService` and `LoginSecurityController` (design decisions 2 and 3), plus the gateway route `/loginSecurity/**` to services-system.
|
||||||
|
- Before coding: check how mobile-api reaches services-system before a user has a token (the forgot-password path). Match that for `isLocked` and `record`, and note the finding in the task report.
|
||||||
|
- **Tests first:**
|
||||||
|
- locked at `max` failures inside the period;
|
||||||
|
- not locked when older failures fall outside it;
|
||||||
|
- a `LOGIN_SUCCESS`, `UNLOCK_RESET` or `PASSWORD_CHANGED` resets the count;
|
||||||
|
- lockout off means never locked;
|
||||||
|
- settings are read from `system_property`, with S, M, H and D handled;
|
||||||
|
- `lockedUntil` is computed as the design says;
|
||||||
|
- the locked list includes only locked usernames;
|
||||||
|
- unlock writes `UNLOCK_RESET` with who unlocked;
|
||||||
|
- `record` cuts long values and fills `company_id` for a known username, NULL otherwise;
|
||||||
|
- `audit` filters by username, event and dates, returns newest first, and caps the size at 100;
|
||||||
|
- `config` rejects a max or period below 1 and an unknown unit;
|
||||||
|
- `passwordRules` updates only `ADMIN_PWD_FORMAT` rows and refuses other ids.
|
||||||
|
- Scope: `solvway-services-system/src/**` (new login-security files, tests), `solvway-api-gateway/src/main/resources/application.yml`
|
||||||
|
- Verify: `mvn -T 1C -B -q -pl solvway-services-system,solvway-api-gateway -am verify`
|
||||||
|
|
||||||
|
- [ ] 2.2 `/user/changeAdminPassword` (design decision 4).
|
||||||
|
- **Tests first,** one for each result:
|
||||||
|
- `OK` saves the bcrypt hash and keeps `login = Y`;
|
||||||
|
- `NOT_FOUND` for a non-type-A or disabled user;
|
||||||
|
- `CURRENT_PASSWORD_WRONG`;
|
||||||
|
- `PASSWORD_SAME`;
|
||||||
|
- `PASSWORD_POLICY`.
|
||||||
|
- Scope: `solvway-services-system/src/main/java/**/controller/UserController.java`, its tests
|
||||||
|
- Verify: `mvn -T 1C -B -q -pl solvway-services-system -am verify`
|
||||||
|
|
||||||
|
## 3. mobile-api
|
||||||
|
|
||||||
|
- [ ] 3.1 Sign-in and sign-out enforcement and recording (design decisions 5 and 6).
|
||||||
|
- **Tests first:**
|
||||||
|
- blank fields record nothing;
|
||||||
|
- locked returns 401 `ACCOUNT_LOCKED`, records `LOGIN_LOCKED`, and never calls services-auth;
|
||||||
|
- a lock-check error continues to the password check;
|
||||||
|
- a wrong password, an unknown user or a disabled user records `LOGIN_FAIL`;
|
||||||
|
- a success records `LOGIN_SUCCESS`;
|
||||||
|
- a record failure does not change the answer;
|
||||||
|
- logout records `LOGOUT`;
|
||||||
|
- the IP and user agent are taken from `X-Client-IP` and `User-Agent`.
|
||||||
|
- Scope: `solvway-mobile-api/src/**` (`AdminAuthController`, a new login-audit client, constants, tests)
|
||||||
|
- Verify: `mvn -T 1C -B -q -pl solvway-mobile-api -am verify`
|
||||||
|
|
||||||
|
- [ ] 3.2 `POST /api/admin/auth/change-password` (design decision 7).
|
||||||
|
- **Tests first:**
|
||||||
|
- each 400 code;
|
||||||
|
- `CURRENT_PASSWORD_WRONG` records `LOGIN_FAIL`;
|
||||||
|
- locked is refused before the services-system call;
|
||||||
|
- success records `PASSWORD_CHANGED` and returns 204;
|
||||||
|
- a pending first-login user gets 403 `PASSWORD_RESET_REQUIRED`.
|
||||||
|
- Scope: `solvway-mobile-api/src/**` (`AdminAuthController`, `AdminAuthenticationService`, request record, tests)
|
||||||
|
- Verify: `mvn -T 1C -B -q -pl solvway-mobile-api -am verify`
|
||||||
|
|
||||||
|
- [ ] 3.3 `AdminLoginSecurityController` and service (design decision 8).
|
||||||
|
- **Tests first:**
|
||||||
|
- class-level `@AdminModule("BO_LOGIN_SECURITY")`;
|
||||||
|
- each endpoint maps its parameters;
|
||||||
|
- unlock passes the admin's username as `by` and returns 204;
|
||||||
|
- a services-system 400 on config or rules passes through as 400 with its message;
|
||||||
|
- unlock and settings changes write the log line.
|
||||||
|
- Scope: `solvway-mobile-api/src/**` (new login-security files, tests)
|
||||||
|
- Verify: `mvn -T 1C -B -q -pl solvway-mobile-api -am verify`
|
||||||
|
- Live check through the stack, as the dev admin and made-up local users:
|
||||||
|
- fail 5 times and see the lock;
|
||||||
|
- unlock and sign in;
|
||||||
|
- change a password;
|
||||||
|
- edit and restore the settings.
|
||||||
|
|
||||||
|
## 4. Portal
|
||||||
|
|
||||||
|
- [ ] 4.1 Sign-in pieces (design decisions 6 and 9).
|
||||||
|
- The login and logout routes forward `X-Client-IP` and `User-Agent`.
|
||||||
|
- The login page shows the locked message on `ACCOUNT_LOCKED`.
|
||||||
|
- Header "Change password" item opens `ChangePasswordDialog`, using the `/api/auth/change-password` route.
|
||||||
|
- **Tests first:**
|
||||||
|
- header forwarding;
|
||||||
|
- the locked message;
|
||||||
|
- the dialog: validation, the server code shown through `t()`, close on 204;
|
||||||
|
- the route passes refusals through with their code.
|
||||||
|
- Scope: `solvway-admin-portal/app/api/auth/**`, `solvway-admin-portal/app/login/**`, `solvway-admin-portal/app/components/app/**`, `solvway-admin-portal/app/components/users/**`, `solvway-admin-portal/app/lib/**`
|
||||||
|
- Verify: `cd solvway-admin-portal && npx vitest run && npx tsc --noEmit && npm run lint && npm run build`
|
||||||
|
|
||||||
|
- [ ] 4.2 `/login-security` page with History, Locked and Settings tabs, the proxy route, a service and i18n keys (design decision 9).
|
||||||
|
- **Tests first:**
|
||||||
|
- History rows and filters go into the query;
|
||||||
|
- Locked rows, with Unlock behind a confirm, refreshing the list;
|
||||||
|
- Settings loads, saves, and shows a server validation error;
|
||||||
|
- the rules table saves status and value;
|
||||||
|
- `RequireModule` gates the page;
|
||||||
|
- the proxy refuses dot segments.
|
||||||
|
- Scope: `solvway-admin-portal/app/login-security/**`, `solvway-admin-portal/app/api/login-security/**`, `solvway-admin-portal/app/lib/**`
|
||||||
|
- Verify: `cd solvway-admin-portal && npx vitest run && npx tsc --noEmit && npm run lint && npm run build`
|
||||||
|
|
||||||
|
## 5. Check and docs
|
||||||
|
|
||||||
|
- [ ] 5.1 Browser check and handover.
|
||||||
|
- **Playwright on the local stack, with made-up local users:**
|
||||||
|
- fail sign-in until locked and see the locked message;
|
||||||
|
- as admin, see the user under Locked, unlock them, and they sign in;
|
||||||
|
- History shows the events;
|
||||||
|
- change own password from the header, with a wrong current password first, then success;
|
||||||
|
- edit Settings and see a validation error, then restore;
|
||||||
|
- as `noevents`, see no Login security menu entry, and Not authorized at `/login-security`;
|
||||||
|
- check the pages at 360px.
|
||||||
|
- **Docs:** `docs/deployment.md` section 3 lists the new SQL files.
|
||||||
|
- Scope: `docs/deployment.md`
|
||||||
|
- Verify: screenshots, and the steps above observed.
|
||||||
|
|
||||||
|
## Workflow follow-up
|
||||||
|
|
||||||
|
- Whole-branch review, push, PR to main and merge, then archive on its own branch.
|
||||||
Reference in new issue
Block a user