feat(auth): implement RBAC, user lifecycle, staff administration, and modular architecture - #1
Merged
Conversation
…and rotation gate
…h_logout.py, and test_auth_pin.py
…mline router imports
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR expands the backend authentication and user management subsystems for TutorBox (Week 1 Milestone).
It introduces full Role-Based Access Control (RBAC), student self-service onboarding, staff account administration, forced PIN rotation, append-only audit logging, and a modular architecture mirroring across
src/andtests/.Key Changes
1. Database Migrations (
migrations/004–006)004_add_must_change_pin.sql: Addsmust_change_pincolumn to enforce temporary-PIN rotation.005_add_users_deleted_at.sql: Addsdeleted_atandformer_usernamecolumns for soft-deletion withusername freeing and telemetry preservation.
006_add_audit_logs.sql: Creates append-onlyaudit_logstable with indexed actor and target lookups.2. Security & RBAC Infrastructure (
src/security/)require_roles("student", "teacher", "admin")).AuthContextwith active session checking (is_active = 1).must_change_pin = 1until PIN rotation is completed.InMemoryRateLimiter: Thread-safe, bounded capacity, exponential backoff lockout for credential attempts.SlidingWindowLimiter: Event-queue rate limiter for global signup flood prevention.UsernameField,PinField,RoleField) enforcing boundaries and log-injection safety.3. API Surface (
src/api/)src/api/auth/):POST /login: Authenticate username + PIN, return session token andmust_change_pinstatus.POST /logout: Invalidate caller's session token in SQLite.src/api/users/):POST /signup: Self-registration for student accounts (rate-limited).GET /users/me: Profile retrieval for authenticated caller.PATCH /users/me/pin: Change PIN (clears rotation flag, verifies current PIN, deactivates old session).PATCH /users/me/username: Rename account (frees old username immediately).src/api/staff/):GET /users: Roster view of active users, or soft-deleted accounts (?include_deleted=true).POST /users: Staff user creation (Teachers create students/teachers; Admins create any role).POST /users/{id}/reset-pin: Issues 6-digit temp PIN, flagsmust_change_pin = 1, and invalidates all target sessions.DELETE /users/{id}: Soft-deletes user, anonymizes username, preserves telemetry turn logs, and enforces last-admin guard.POST /users/{id}/recover: Restores soft-deleted account under a new username with temporary PIN.GET /audit-logs: Admin-only reverse chronological view of up to 500 audit entries.4. Modular Refactor & Test Breakdown (
tests/)tests/into 1:1 subpackages mirroringsrc/(api/auth,api/users,api/staff,db,security).auth_headers,get_user_id) intests/conftest.py.Verification & Quality Gates
--cov-fail-under=80)ruff check .andruff format --check .return 0 errors)Branch Details
week1_expandedmain