Skip to content

Commit 074a5ab

Browse files
unclesp1d3rgithub-advanced-security[bot]warp-agentCopilotCopilot
authored
implement authentication and authorization system mern migration task 3 (#40)
* feat(backend): Implement authentication and authorization system - Add cookie-parser middleware for session management - Implement user model and authentication service with JWT and session-based authentication - Create authentication and authorization middleware for role-based access control - Develop project and user models to manage user roles and permissions - Integrate Redis for session storage and improve database connection handling - Add comprehensive unit and integration tests for authentication flows - Update documentation to reflect new authentication implementation and usage This commit establishes a robust authentication and authorization framework, enhancing security and user management capabilities across the application. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io> * fix(backend): Update user ID handling in project service and improve integration test timeouts - Modify getUserProjects method to convert userId to ObjectId for accurate MongoDB queries. - Increase timeout for container startup in integration tests to 120 seconds to ensure reliable test execution. - Adjust cookie handling in authentication tests to support both single and array formats for improved robustness. These changes enhance the reliability of user project retrieval and improve the stability of integration tests. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io> * fix(backend): Enhance project model middleware and improve integration test cleanup - Update project model middleware to ensure `next()` is called only if it's a function, preventing potential errors. - Refactor integration tests to improve cleanup order for Redis and MongoDB containers, ensuring proper resource management and reducing test flakiness. These changes enhance the reliability of the project model and improve the stability of integration tests. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io> * chore(pre-commit): Update Prettier hook to use local npm script for consistent formatting - Refactor Prettier hook to utilize a local command that runs `npm run format`, ensuring the same Prettier version and configuration as used in npm scripts. - Adjust hook settings to always run and exclude passing filenames, improving integration with the project's formatting workflow. These changes enhance the reliability of code formatting across the project. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io> * feat(backend): Implement CSRF protection and enhance authentication tests - Introduced CSRF protection middleware using Lusca, ensuring secure handling of state-changing requests. - Updated authentication integration tests to include CSRF token handling for login requests, improving security during testing. - Added helper functions to facilitate CSRF token retrieval for both unauthenticated and authenticated requests. - Updated package dependencies to include Lusca and its type definitions. These changes enhance the security of the application by implementing CSRF protection and improving the robustness of authentication tests. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io> * feat(backend): Replace Lusca with CSRF protection and enhance middleware - Removed Lusca dependency and integrated CSRF protection using the csrf package. - Updated middleware to handle CSRF token generation and validation for state-changing requests. - Refactored related tests to accommodate the new CSRF implementation. - Updated package dependencies to reflect the removal of Lusca and addition of csrf. These changes improve the security posture of the application by implementing a more flexible CSRF protection mechanism. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io> * Potential fix for code scanning alert no. 1: Missing rate limiting Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> * fix: build shared package before type-check in CI The type-check step was failing because backend imports from @hashhive/shared, but the shared package's dist output wasn't built yet. Added build-shared as a dependency before type-check in the ci-check recipe. Co-Authored-By: Warp <agent@warp.dev> * fix: extract magic numbers in rate limiter to named constants Resolve ESLint @typescript-eslint/no-magic-numbers violations by extracting rate limiter configuration values into descriptive constants. Co-Authored-By: Warp <agent@warp.dev> * feat(backend): Enhance rate limiting and database connection settings - Added express-rate-limit package to implement rate limiting on sensitive authentication endpoints. - Updated Jest configuration to improve coverage thresholds for various files. - Enhanced MongoDB connection settings with server selection and socket timeouts for better reliability. - Updated .prettierignore to exclude Next.js generated files. These changes improve the security and performance of the application by enforcing request limits and optimizing database connectivity. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io> * feat(backend): Refactor CSRF handling and enhance project service with transactions - Updated CSRF token handling in the middleware to reuse existing secrets, improving token validity across requests. - Enhanced project creation in ProjectService to use MongoDB transactions, ensuring atomicity when creating projects and associated users. - Improved error logging in optional authentication middleware to capture and log authentication errors. - Refactored CSRF token retrieval in tests to align with the new header-based approach. These changes enhance security and reliability in CSRF handling and project management processes. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io> * fix(ci): use npx to run eslint in workspace packages The lint command in backend, frontend, and shared packages was failing because eslint was not found. The eslint package is installed at the root level as a devDependency, but workspace packages need to use 'npx' to resolve binaries from the workspace tree. This change updates all workspace package.json files to use 'npx eslint' instead of 'eslint' directly. Co-Authored-By: Warp <agent@warp.dev> * feat(workflow): add GitHub Copilot setup workflow Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io> * Address PR #40 review feedback: CORS, CSRF middleware, and role aggregation (#41) * Initial plan * fix: Apply PR review feedback - CORS, CSRF, and role aggregation improvements Co-authored-by: unclesp1d3r <251112+unclesp1d3r@users.noreply.github.com> * refactor: Simplify CSRF middleware logic and remove redundant await Co-authored-by: unclesp1d3r <251112+unclesp1d3r@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: unclesp1d3r <251112+unclesp1d3r@users.noreply.github.com> * Update backend/src/routes/auth.routes.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * chore(ci): update node version file references and add .nvmrc Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io> * chore(models): remove redundant index declarations to avoid duplication Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io> * feat(auth): enhance password handling and upgrade flag logic - Added password_requires_upgrade field to IUser model. - Updated login logic to flag users with weak passwords for upgrade. - Improved error messages for Redis client status. - Enhanced tests for login functionality and session management. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io> * feat(csrf): implement encryption for CSRF secrets in cookies - Added functions to encrypt and decrypt CSRF secrets using AES-256-GCM. - Updated CSRF cookie handling to store encrypted secrets, enhancing security. - Improved error handling for encryption and decryption processes. These changes strengthen the protection of CSRF tokens by ensuring sensitive information is not stored in plaintext. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io> * feat(auth): improve user mapping and enhance CSRF key handling - Added a utility function to map Mongoose IUser documents to a User object for request context, improving code readability and maintainability. - Cached the CSRF encryption key at application startup to optimize performance by avoiding repeated PBKDF2 computations. - Updated project model to change the pre-save hook to a pre-validate hook for slug generation, ensuring validation requirements are met. These changes enhance the efficiency and clarity of user authentication and CSRF handling processes. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io> * Fix failing checks for run 19625498172 (#47) Co-authored-by: Warp Agent <agent@warp.dev> Co-authored-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io> * docs: add ci-check reminder before committing changes in AGENTS.md and copilot-instructions.md - Included a note to run `just ci-check` to ensure all checks and tests pass before committing changes, emphasizing the importance of task completion. This update enhances the development workflow documentation for better adherence to quality checks. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io> --------- Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io> Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> Co-authored-by: Warp Agent <agent@warp.dev> Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent bddd2e3 commit 074a5ab

59 files changed

Lines changed: 4923 additions & 208 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/copilot-instructions.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ HashHive is a greenfield MERN (MongoDB, Express, React, Node.js) platform orches
3434

3535
## Development Workflow
3636

37+
> Before committing any changes, always run `just ci-check` and ensure all checks and tests pass; a task is not complete until `just ci-check` succeeds.
38+
3739
```bash
3840
# Quick start
3941
npm install # Install all workspace deps

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818

1919
- uses: actions/setup-node@v5
2020
with:
21-
node-version-file: 'package.json'
21+
node-version-file: '.node-version'
2222
cache: npm
2323

2424
- name: Install just
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: Copilot Setup
2+
'on':
3+
workflow_dispatch:
4+
jobs:
5+
setup:
6+
runs-on: ubuntu-latest
7+
env:
8+
# Testcontainers configuration for GitHub Copilot
9+
# Ryuk is a container that cleans up resources; enable it for proper cleanup
10+
TESTCONTAINERS_RYUK_DISABLED: false
11+
# Use the local Docker daemon
12+
DOCKER_HOST: unix:///var/run/docker.sock
13+
steps:
14+
- uses: actions/checkout@v5
15+
16+
- uses: actions/setup-node@v5
17+
with:
18+
node-version-file: '.node-version'
19+
cache: npm
20+
21+
- name: Install just
22+
uses: extractions/setup-just@v1
23+
24+
- name: Install root dependencies
25+
run: npm install
26+
27+
- name: Install workspace dependencies
28+
run: npm install --workspaces
29+
30+
- name: Build shared package
31+
run: npm run build -w shared
32+
33+
- name: Run type checking
34+
run: npm run type-check --workspaces
35+
36+
- name: Verify Docker is available for Testcontainers
37+
run: |
38+
docker --version
39+
docker info
40+
echo "Docker socket permissions:"
41+
ls -la /var/run/docker.sock
42+
43+
- name: Pre-pull Docker images for Testcontainers
44+
run: |
45+
echo "Pulling Docker images used by integration tests..."
46+
docker pull mongo:7 &
47+
docker pull redis:7-alpine &
48+
docker pull minio/minio:latest &
49+
wait
50+
echo "All images pulled successfully"
51+
52+
- name: Validate setup
53+
run: just validate
54+
55+
- name: Display environment info
56+
run: just info

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,4 @@ temp/
4343

4444
# Test results
4545
frontend/test-results/
46-
frontend/playwright-report/index.html
46+
frontend/playwright-report/

.kiro/specs/mern-migration/tasks.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,29 @@
3838
- Add presigned URL generation for secure downloads
3939
- _Requirements: 1.4, 7.1_
4040

41-
- [ ] 3. Authentication and authorization system
42-
- [ ] 3.1 Implement user model and authentication service
41+
- [x] 3. Authentication and authorization system
42+
- [x] 3.1 Implement user model and authentication service
4343
- Create User Mongoose model with password hashing
4444
- Implement AuthService with login, logout, and token generation
4545
- Add JWT token generation and validation
4646
- Implement session management with HttpOnly cookies
4747
- _Requirements: 2.1, 2.2, 2.5_
4848

49-
- [ ] 3.2 Create authentication middleware
49+
- [x] 3.2 Create authentication middleware
5050
- Implement JWT validation middleware for API routes
5151
- Create session validation middleware for web routes
5252
- Add request user context injection
5353
- Implement token refresh logic
5454
- _Requirements: 2.1, 2.2_
5555

56-
- [ ] 3.3 Implement role-based access control
56+
- [x] 3.3 Implement role-based access control
5757
- Create Role and ProjectUser models
5858
- Implement authorization middleware with role checking
5959
- Add project-scoped permission validation
6060
- Create permission helper utilities
6161
- _Requirements: 2.3, 2.4_
6262

63-
- [ ] 3.4 Write authentication tests
63+
- [x] 3.4 Write authentication tests
6464
- Test login flow with valid and invalid credentials
6565
- Test JWT token generation and validation
6666
- Test session cookie management

.node-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
20

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
20

.pre-commit-config.yaml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,20 @@ repos:
3232
- id: check-xml
3333
- id: check-yaml
3434

35-
# Format code with Prettier
36-
- repo: https://github.com/pre-commit/mirrors-prettier
37-
rev: v4.0.0-alpha.8
35+
# Local hooks for npm scripts
36+
- repo: local
3837
hooks:
38+
# Format code with Prettier
39+
# Use local hook to ensure same Prettier version and config as npm scripts
3940
- id: prettier
40-
# Only run Prettier on code/config files, not Markdown or JSONC
41+
name: Prettier
42+
entry: bash -c 'npm run format'
43+
language: system
44+
types: [file]
4145
types_or: [javascript, jsx, ts, tsx, json, yaml]
42-
exclude: ^(node_modules/|dist/|\.next/|coverage/|\.git/|package-lock\.json|.*\.md$|.*\.jsonc$)
46+
pass_filenames: false
47+
always_run: true
4348

44-
# Local hooks for npm scripts
45-
- repo: local
46-
hooks:
4749
# ESLint linting
4850
# Note: Using local hook to respect project's ESLint config
4951
# Alternative: https://github.com/pre-commit/mirrors-eslint (requires additional setup)

.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ test-results
1010
# Ignore markdown docs from Prettier checks (per project preference)
1111
*.md
1212
**/*.md
13+
# Ignore Next.js generated files
14+
frontend/next-env.d.ts

AGENTS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ Updates are generally non-destructive, relying on explicit status fields and met
148148

149149
## Development and runtime commands (planned)
150150

151+
> Before committing any changes, always run `just ci-check` and ensure all checks and tests pass; a task is not complete until `just ci-check` succeeds.
152+
151153
At the time of writing, this repository primarily contains design documents; actual `package.json` scripts and Docker configurations may not yet exist. The commands below are taken from `.kiro/steering/tech.md` and represent the intended interface once backend and frontend packages are in place.
152154

153155
### Backend (inside `backend/`)

0 commit comments

Comments
 (0)