Skip to content

Feat/secure wallet endpoints#156

Open
davedumto wants to merge 7 commits intoHarmonia-Development:mainfrom
davedumto:feat/secure-wallet-endpoints
Open

Feat/secure wallet endpoints#156
davedumto wants to merge 7 commits intoHarmonia-Development:mainfrom
davedumto:feat/secure-wallet-endpoints

Conversation

@davedumto
Copy link
Contributor

closes #136

Summary

This PR implements JWT authentication middleware to secure all wallet-related API endpoints in the Stellar wallet service. The implementation ensures only users with valid JWTs can access sensitive wallet operations like account creation and management.

Changes Made

🔧 Core Implementation

  • JWT Middleware: Created comprehensive JWT authentication middleware in src/auth/jwt.ts

    • Validates Authorization: Bearer <token> header format
    • Verifies JWT signature and expiration
    • Extracts user information from JWT payload
    • Implements proper error handling with defensive programming
  • User Authorization: Added requireMatchingUserId middleware to prevent cross-user access

    • Ensures JWT user_id matches request body user_id
    • Returns appropriate HTTP status codes (401/403)

🛡️ Security Implementation

  • Route Protection: Applied JWT middleware to all /wallet/* endpoints

    • Global middleware in src/index.ts for consistent protection
    • Specific middleware on wallet creation endpoint for additional security
  • Error Handling: Implemented proper HTTP status codes and JSON error responses

    • 401 Unauthorized - Missing or invalid JWT tokens
    • 403 Forbidden - Valid JWT but mismatched user_id
    • 500 Internal Server Error - Missing JWT secret configuration

🧪 Testing

  • Comprehensive Test Suite: Created full test coverage with bun compatibility

    • JWT middleware tests (tests/auth/jwt.test.ts) - 8 test cases
    • Wallet integration tests (tests/routes/wallet.test.ts) - 6 test cases
    • All authentication scenarios covered (401, 403, 201 responses)
    • Proper mocking of external dependencies
  • Test Scenarios:

    • ✅ Valid JWT with matching user_id → Success (201)
    • ✅ Missing JWT token → Unauthorized (401)
    • ✅ Invalid JWT token → Unauthorized (401)
    • ✅ Expired JWT token → Unauthorized (401)
    • ✅ Valid JWT with mismatched user_id → Forbidden (403)
    • ✅ Business logic errors properly handled

📚 Documentation

  • Integration Guide: Added JWT_INTEGRATION.md with practical examples
    • Setup instructions
    • Request format examples
    • Error handling guidance
    • Frontend integration examples

Technical Details

Authentication Flow

  1. Client sends request with Authorization: Bearer <jwt_token> header
  2. jwtMiddleware validates token format and signature
  3. requireMatchingUserId ensures user can only access their own resources
  4. Request proceeds to business logic if authenticated and authorized

Security Features

  • Token Validation: Full JWT signature verification with configurable secret
  • User Isolation: Prevents users from accessing other users' wallet data
  • Graceful Degradation: Proper error responses maintain API consistency
  • Environment Security: JWT secret validation with fallback error handling

Code Quality

  • ESLint/Prettier Compliant: All code follows project formatting standards
  • TypeScript: Full type safety with proper interfaces
  • Error Handling: Defensive programming with null checks and validation
  • Performance: Efficient middleware with minimal overhead
  • Maintainability: Clean, readable code with helpful comments

Testing Results

✅ 14/14 tests passing
✅ JWT Middleware: 8/8 tests pass
✅ Wallet Integration: 6/6 tests pass
✅ All authentication scenarios covered
✅ No linting errors

Breaking Changes

None - this is purely additive security enhancement.

Migration

Existing API consumers need to:

  1. Obtain JWT tokens from the authentication service
  2. Include Authorization: Bearer <token> header in wallet API requests
  3. Ensure user_id in JWT matches user_id in request body

See JWT_INTEGRATION.md for detailed integration examples.

Files Changed

  • src/auth/jwt.ts - JWT middleware implementation
  • src/index.ts - Applied middleware to all wallet routes
  • src/routes/wallet.ts - Added user authorization to wallet endpoints
  • tests/auth/jwt.test.ts - JWT middleware test suite
  • tests/routes/wallet.test.ts - Wallet integration tests
  • JWT_INTEGRATION.md - Integration documentation

🔒 Security Impact: This PR significantly enhances the security posture of the wallet service by implementing industry-standard JWT authentication with proper user authorization controls.

@salazarsebas
Copy link
Contributor

Hey @davedumto , please resolve the workflow errors

@davedumto
Copy link
Contributor Author

Okay on it!

Merge branch 'feat/secure-wallet-endpoints' of https://github.com/davedumto/harmonia-dappOD into feat/secure-wallet-endpoints
@davedumto
Copy link
Contributor Author

some other tests are failing and causing the last ci to fail, and the failing tests are not from my code

@salazarsebas
Copy link
Contributor

some other tests are failing and causing the last ci to fail, and the failing tests are not from my code

All merged pull requests pass the workflows. So if the changes you made are causing the workflow to fail, perhaps merging from main is where the workflow failed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Secure Wallet Endpoints with JWT Authentication [wallet - service]

2 participants