Skip to content

User Management & Authentication

This page covers roles and permissions, the token lifecycle, and the account-security features (lockout, password history, password reset).

User Roles

Role Description
Super User (super) Full system access: every user, role changes, activities, system logs and the SUPER dashboard. The first one is seeded from SUPER_USER_*.
Admin (admin) Staff administrator. Manages regular users only, and never sees SUPER users.
User (user) End user. Manages their own profile, password and ToDo lists.

All rules live in one place: app/services/users.py (can_view, can_manage, and the command functions). Endpoints only call into it.

Permissions Matrix

Action Super Admin User
Create SUPER / ADMIN user ✅ ❌ ❌
Create regular user ✅ ✅ ❌
List users (GET /users/) ✅ ✅ (excl. SUPER) ❌
View a user (GET /users/{id}) ✅ ✅ (excl. SUPER) Self only
Update another user's profile / active flag ✅ Regular users only ❌
Change any role ✅ ❌ ❌
Change own role / active flag ✅ ❌ ❌
Delete a user ✅ Regular users only ❌
Delete own account ❌ ❌ ❌
View / edit own profile (/users/me) ✅ ✅ ✅
ToDos All Own + regular users' Own
Activities (all users), system logs, SUPER dashboard ✅ ❌ ❌
Admin dashboard stats ✅ ✅ ❌

Changing a user's role or deactivating them revokes all of their tokens immediately.

Creating Users

Administrative creation

POST /api/v1/users/ (SUPER or ADMIN) returns 201 Created. Admins can only create user-role accounts; anything else is 403. A welcome email (without the password) is sent in the background.

{
  "email": "user@example.com",
  "password": "a-strong-password",
  "full_name": "Jane Doe",
  "role": "user",
  "is_active": true
}

Public registration

POST /api/v1/login/signup creates a user-role account (201). Any role field in the body is ignored. Emails are trimmed and stored lower-case, and uniqueness is case-insensitive (409 Conflict on duplicates).

Tokens

POST /api/v1/login/access-token (OAuth2 password form) returns:

{
  "access_token": "…",
  "refresh_token": "…",
  "token_type": "bearer",
  "expires_in": 1800
}
Token Lifetime Use
Access (typ=access) ACCESS_TOKEN_EXPIRE_MINUTES Authorization: Bearer … on API calls
Refresh (typ=refresh) REFRESH_TOKEN_EXPIRE_DAYS POST /login/refresh-token → new pair
Reset (typ=reset) EMAIL_RESET_TOKEN_EXPIRE_HOURS POST /login/reset-password only

Each token type is accepted only where it is expected. Every token also carries iss, aud, jti and ver. ver is the user's token_version, and incrementing it revokes every outstanding token for that user. That happens on:

  • POST /login/logout (signs out all devices)
  • password change or reset
  • deactivation or role change

Account Security

  • Lockout: after MAX_FAILED_LOGIN_ATTEMPTS wrong passwords the account is locked for ACCOUNT_LOCKOUT_MINUTES (423 Locked). A successful login or a password reset clears the counter.
  • Timing-safe login: unknown emails are verified against a dummy hash built with the same Argon2 parameters, so response time does not reveal which emails exist.
  • Password history: a new password cannot match the current one or the last PASSWORD_HISTORY_DEPTH passwords. GET /users/me/password-history lists change timestamps (never hashes).
  • Transparent rehash: hashes made with older Argon2 parameters are upgraded on the next successful login.
  • Rate limits: 5/min on login, signup and password reset, 3/min on password recovery, 30/min on refresh.

Password Recovery

  1. POST /api/v1/login/password-recovery with {"email": "…"}. The response is identical whether or not the account exists.
  2. The user receives a link to FRONTEND_HOST/reset-password?token=….
  3. The frontend calls POST /api/v1/login/reset-password with {"token": "…", "new_password": "…"}.

The reset token is single-use: resetting increments token_version, which also signs out every existing session.

Changing Roles

PATCH /api/v1/users/{id} with {"role": "super"} (SUPER only). For example, promote an admin:

PATCH /api/v1/users/{admin_user_id}
Authorization: Bearer <super token>
Content-Type: application/json

{"role": "super"}