-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.cursorrules
More file actions
executable file
·530 lines (441 loc) · 21.3 KB
/
Copy path.cursorrules
File metadata and controls
executable file
·530 lines (441 loc) · 21.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
## Naming Conventions and Code Style
### Naming Rules
- **NO marketing adjectives**: Never use "advanced", "enhanced", "improved", "better", "superior", "features", "optimized", "premium", or similar terms in code names
- **Be descriptive and direct**: Use specific, functional names that describe what the code does
- **Avoid redundancy**: Don't use "features" suffix (e.g., `useE2EE` not `useE2EEFeatures`)
- **Keep it simple**: Prefer shorter, clearer names over verbose ones
- **Examples**:
- ✅ `useE2EE`, `MessageScheduler`, `DisappearingMessages`
- ❌ `useAdvancedE2EE`, `EnhancedMessageScheduler`, `ImprovedFeatures`
### Integration over Creation
- Always integrate new functionality into existing hooks/components when possible
- Only create separate files when functionality is truly distinct
- Prefer extending existing interfaces over creating new ones
- Merge related functionality rather than creating parallel systems
## Architecture Overview
This is a Laravel 12 + React fullstack application using:
- **Backend**: Laravel 12 with Inertia.js for SPA functionality
- **Frontend**: React 19 with TypeScript, Vite for bundling
- **UI Library**: shadcn/ui components with Radix UI primitives and Tailwind CSS v4
- **Testing**: Pest (PHP) for backend, Playwright for E2E tests
- **Database**: PostgreSQL (default), migrations in `database/migrations/`
- **SSR**: Enabled via Inertia.js server-side rendering (port 13714)
- **Authentication**: Laravel Passport for OAuth 2.0/OIDC, Laravel Breeze-style UI flows, WebAuthn/Passkeys support
- **Extensions**: Multiple Spatie packages (permissions, activity logs, event sourcing)
- **Organization Management**: Hierarchical organization structure with units, positions, and memberships
- **Chat System**: End-to-end encrypted chat with multi-device and quantum-resistant cryptography support
- **File Storage**: MinIO S3-compatible storage with encrypted file handling
- **Containerization**: Docker Compose setup for local, dev, staging, and production environments
## Development Commands
### Start Development Server
```bash
# Runs Laravel server, queue worker, logs, and Vite in parallel
composer dev
# Development with SSR (builds SSR first, then runs with SSR server)
composer dev:ssr
```
### Frontend Only
```bash
npm run dev # Start Vite dev server
npm run build # Build for production
npm run build:ssr # Build with SSR
```
### Backend
```bash
php artisan serve # Start Laravel server
php artisan test # Run Pest tests
composer test # Same as artisan test
```
### Code Quality
```bash
npm run lint # ESLint with auto-fix
npm run types # TypeScript type checking
npm run format # Prettier formatting
npm run format:check # Check formatting
./vendor/bin/pint # Laravel Pint (PHP formatting)
```
### E2E and Testing
```bash
npm run test:e2e # Run all Playwright E2E tests
npm run test:e2e:ui # Run E2E tests with UI mode
npm run test:e2e:headed # Run E2E tests in headed mode
npm run test:e2e:debug # Debug E2E tests
./scripts/test-e2ee.sh # Comprehensive E2EE chat testing
./scripts/test-e2ee.sh unit # Run only unit tests
./scripts/test-e2ee.sh multidevice # Run only multi-device tests
./scripts/test-e2ee.sh quantum # Run quantum cryptography tests
php artisan test --filter=Chat # Run specific chat test group
```
### Docker Development
```bash
make help # Show all available commands
make install # Install and setup the project
make local # Start local development environment
make local-fresh # Start with fresh database
make test # Run complete test suite
make logs service=app # Show logs for specific service
make shell # Open shell in app container
make clean # Clean up Docker resources
```
## Project Structure
### Backend (Laravel)
- **Routes**:
- `routes/web.php` - Main web routes
- `routes/webs/auth.php` - Authentication routes
- `routes/webs/oauth.php` - OAuth/OIDC routes
- `routes/webs/settings.php` - Settings routes
- `routes/api.php` - API endpoints
- `routes/channels.php` - Broadcasting channels
- **Controllers**: `app/Http/Controllers/` organized by feature:
- `Api/Chat/` - Chat system controllers (Conversation, Message, Encryption, etc.)
- `Api/QuantumController.php` - Quantum cryptography API endpoints
- `Api/` - Organization, User, Permission, Role controllers
- `Auth/` - Authentication controllers
- `OAuth/` - OAuth 2.0 server controllers
- `Settings/` - Profile and password management
- **Models**: `app/Models/` including:
- Organization hierarchy: `Organization`, `OrganizationUnit`, `OrganizationPosition`, `OrganizationMembership`
- Chat system: `Chat/Conversation`, `Chat/Message`, `Chat/EncryptionKey`, `Chat/Participant`
- Authentication: `User`, `UserDevice`, `UserMfaSetting`
- OAuth: `Client`, `OAuthAuditLog`, `OAuthScope`
- **Services**: `app/Services/` - Business logic:
- `ChatEncryptionService.php` - Chat encryption with quantum algorithm support
- `QuantumCryptoService.php` - NIST-approved post-quantum cryptography (ML-KEM)
- `Crypto/MLKEMProviderInterface.php` - ML-KEM provider abstraction
- `Crypto/LibOQSMLKEMProvider.php` - Production LibOQS implementation
- `Crypto/FallbackMLKEMProvider.php` - Development/testing fallback
- `MultiDeviceEncryptionService.php` - Multi-device quantum support
- `ChatFileService.php` - File handling and other services
- **Middleware**: `app/Http/Middleware/` - Rate limiting, permissions, chat security, tenant context
### Frontend (React)
- **Entry Points**:
- `resources/js/app.tsx` - client-side entry
- `resources/js/ssr.tsx` - server-side rendering entry
- **Pages**: `resources/js/pages/` - Inertia.js pages:
- `chat.tsx` - Main chat interface
- `dashboard.tsx` - Dashboard
- `welcome.tsx` - Landing page
- **Components**: `resources/js/components/` - reusable components and shadcn/ui components
- **Layouts**: `resources/js/layouts/` - app layouts (app-layout, auth-layout)
- **Hooks**: `resources/js/hooks/` - custom React hooks:
- `useChat.ts` - Chat functionality
- `useE2EE.ts` - End-to-end encryption with quantum algorithm support
- `useQuantumE2EE.ts` - Quantum-resistant cryptography management
- `useChatPagination.ts` - Chat message pagination
- `usePermissions.tsx` - Permission checking
- **Services**: `resources/js/services/` - Frontend services:
- `ApiService.ts` - Centralized HTTP client with authentication and token management
- `MultiDeviceE2EEService.ts` - Multi-device encryption
- `OptimizedE2EEService.ts` - Performance optimized encryption with quantum support
- `QuantumE2EEService.ts` - Client-side quantum cryptography
- `SecurityMonitoringService.ts` - Security monitoring
- **Types**: `resources/js/types/` - TypeScript definitions (chat.ts, index.d.ts)
- **Utils**: `resources/js/utils/` - Utility functions and encryption helpers:
- `QuantumMigrationUtils.ts` - Migration utilities for quantum transition
- Standard encryption and chat utilities
### Configuration
- **TypeScript**: Path aliases configured (`@/*` maps to `resources/js/*`), strict mode enabled
- **shadcn/ui**: Configured in `components.json` with Tailwind CSS integration, Lucide icons
- **Vite**: Configured for Laravel with React, Tailwind CSS v4, and SSR support
- **Docker**: Multi-environment setup (local, dev, staging, prod) with Docker Compose
- **Storage**: MinIO S3-compatible storage configuration for encrypted file handling
## Key Integrations
### Inertia.js
- Server-side rendering enabled
- Pages are React components in `resources/js/pages/`
- Laravel routes return `Inertia::render()` calls
- Ziggy package provides named route helpers in frontend
### Authentication
- Laravel Breeze-style authentication with Inertia.js
- Complete auth flow: register, login, password reset, email verification
- Auth pages in `resources/js/pages/auth/`
### UI Components
- shadcn/ui components in `resources/js/components/ui/`:
- `quantum-status-badge.tsx` - Algorithm status indicators with tooltips
- `quantum-health-indicator.tsx` - System health and device readiness display
- `quantum-device-manager.tsx` - Device management interface with migration
- `quantum-admin-panel.tsx` - Complete admin dashboard for quantum systems
- Standard UI components (button, card, alert, etc.)
- Appearance/theme system with light/dark mode support
- Sidebar navigation with user management
## Testing
### Backend Tests (Pest)
```bash
php artisan test # Run all Pest tests
php artisan test --filter=Auth # Run specific test group
php artisan test tests/Feature/ # Run feature tests only
php artisan test --stop-on-failure # Stop on first failure
composer test # Run tests via composer (includes setup)
```
Tests are located in:
- `tests/Feature/` - Feature tests including auth flows, chat, E2EE, organization management
- `tests/Unit/` - Unit tests for services and utilities
- `tests/e2e/` - End-to-end tests using Playwright
### E2E Tests (Playwright)
```bash
npm run test:e2e # Run all E2E tests
npm run test:e2e:ui # Run with Playwright UI
npm run test:e2e:headed # Run in headed browser mode
npm run test:e2e:debug # Debug mode
```
E2E test categories:
- Chat functionality and messaging
- End-to-end encryption workflows
- Multi-device encryption scenarios
- Authentication and authorization flows
- Organization management features
### Specialized Testing Scripts
```bash
./scripts/test-e2ee.sh # Comprehensive E2EE testing suite
./scripts/test-e2ee.sh unit # Unit tests only
./scripts/test-e2ee.sh security # Security validation tests
./scripts/test-minio.sh # MinIO integration testing
```
### Database
- Uses PostgreSQL by default (configurable via Docker Compose)
- Extensive migration set including:
- OAuth 2.0/OIDC tables (Passport) with organization-scoped clients
- Organization hierarchy tables (organizations, units, positions, memberships)
- Chat system tables (conversations, messages, encryption keys, participants)
- Permission tables (Spatie), activity logs, event sourcing, multitenancy
- Passkeys/WebAuthn support tables
- User device management and trusted device tracking
- Comprehensive seeders in `database/seeders/`:
- Organizational structure and position hierarchies
- OAuth clients and scopes
- Permission system setup
- Industry-specific permissions
## Key Features
### Organization Management
- **Hierarchical Structure**: Organizations can have parent-child relationships with automatic path/level management
- **Organization Types**: holding_company, subsidiary, division, branch, department, unit
- **Organization Units**: Various unit types (boards, committees, divisions, departments, etc.)
- **Position Management**: Hierarchical position levels with salary ranges and qualifications
- **Membership System**: Users can have memberships across multiple organizations with different roles
### OAuth 2.0 & OIDC Provider
- **Full OAuth 2.0 Server**: Authorization code, client credentials, refresh token flows
- **OpenID Connect**: Complete OIDC implementation with discovery endpoints
- **Organization-Scoped Clients**: All OAuth clients must be associated with an organization
- **Google-Style Scopes**: Modern URL-based OAuth scopes (https://api.yourcompany.com/auth/organization.readonly, etc.)
- **Rate Limiting**: Separate rate limits for different OAuth endpoints
- **Audit Logging**: Complete audit trail for OAuth operations with tenant context
### End-to-End Encrypted Chat System
- **Multi-Device Support**: Seamless encryption across multiple user devices
- **Quantum-Resistant Cryptography**: NIST-approved post-quantum algorithms (ML-KEM-512/768/1024)
- **Hybrid Encryption**: Smooth transition from RSA to quantum-resistant algorithms
- **Key Management**: Sophisticated encryption key generation, rotation, and recovery
- **File Encryption**: Encrypted file uploads and downloads with thumbnail support
- **Performance Optimized**: Bulk encryption/decryption operations for better performance
- **Security Monitoring**: Real-time security anomaly detection and forensic audit trails
- **Cross-Platform Compatibility**: Support for different device types and encryption implementations
- **Algorithm Negotiation**: Automatic selection of best compatible encryption algorithms
- **Migration Tools**: Comprehensive utilities for upgrading to quantum-resistant encryption
### Quantum Cryptography Features
- **NIST-Approved Algorithms**: ML-KEM (CRYSTALS-KYBER) key encapsulation mechanism
- **Security Levels**: Support for ML-KEM-512, ML-KEM-768, and ML-KEM-1024
- **Hybrid Cryptography**: RSA+ML-KEM combinations for transition periods
- **Provider Architecture**: Pluggable ML-KEM implementations (LibOQS production, fallback testing)
- **Device Readiness Assessment**: Automatic evaluation of quantum migration readiness
- **Migration Strategies**: Immediate, gradual, and hybrid migration approaches
- **Real-time Health Monitoring**: Quantum system status and device capability tracking
- **Admin Dashboard**: Comprehensive quantum security management interface
- **Backward Compatibility**: Seamless interoperability with existing RSA-encrypted conversations
- **Performance Optimization**: Caching, batching, and background processing for quantum operations
### Security Features
- **WebAuthn/Passkeys**: Modern passwordless authentication
- **Two-Factor Authentication**: TOTP/Google Authenticator support
- **Rate Limiting**: Comprehensive rate limiting across OAuth, auth, and chat endpoints
- **Activity Logging**: Full activity logging with Spatie activity log package
- **Trusted Device Management**: Device registration and trust verification
- **Session Management**: Advanced session tracking and termination capabilities
- **Security Audit Trails**: Comprehensive logging for OAuth operations and chat encryption events
## Development Notes
### Key Development Patterns
- **Service Layer**: Business logic separated into dedicated service classes (`app/Services/`)
- **Repository Pattern**: Models use factories for testing with comprehensive relationship support
- **Event-Driven Architecture**: Laravel events for chat notifications, presence updates, and security alerts
- **Multi-Tenant Architecture**: Organization-scoped permissions and OAuth clients with tenant context
- **API Rate Limiting**: Different rate limits for various endpoint categories (auth, chat, OAuth)
- **Centralized HTTP Client**: All API calls use `ApiService` with automatic authentication, token refresh, and error handling
### Environment Setup
- **Concurrency Scripts**: `composer dev` and `composer dev:ssr` run multiple services in parallel
- **Docker Integration**: Full containerization with separate configs for local/dev/staging/prod
- **SSR Configuration**: Inertia.js server-side rendering on port 13714
- **File Storage**: MinIO S3-compatible storage with encryption support
- **Database Testing**: Separate test database with comprehensive seeding
### Code Quality Tools
- **Backend**: Laravel Pint for PHP formatting, Pest for testing
- **Frontend**: ESLint + Prettier with TypeScript strict mode
- **E2E Testing**: Playwright with comprehensive chat and encryption test suites
- **Specialized Testing**: Custom scripts for E2EE validation and performance benchmarking
### Data Manipulation Rules
- **NEVER use `php artisan tinker`**: Tinker should never be used for data manipulation, seeding, or any database operations
- **Use proper seeders**: For database seeding, always use `php artisan db:seed` with proper seeder classes in `database/seeders/`
- **Use migrations**: For schema changes, create and run migrations with `php artisan make:migration` and `php artisan migrate`
- **Use factories**: For test data generation, use model factories with `php artisan make:factory`
- **Use Artisan commands**: Create custom Artisan commands for one-time data operations with `php artisan make:command`
- **Production safety**: Tinker poses significant risks in production environments and should be avoided entirely
### Key Configuration Files
- `composer.json` - Development scripts and dependency management
- `docker-compose.yml` - Local development environment
- `vite.config.ts` - Frontend build configuration with SSR support
- `components.json` - shadcn/ui configuration
- `tsconfig.json` - TypeScript configuration with path aliases
### HTTP Client Usage
All frontend API requests must use the centralized `ApiService` instead of direct fetch calls:
```typescript
import apiService from '@/services/ApiService';
// GET request
const data = await apiService.get<ResponseType>('/api/endpoint');
// POST request
const result = await apiService.post<ResponseType>('/api/endpoint', payload);
// File upload
const formData = new FormData();
formData.append('file', file);
const response = await apiService.postFormData<ResponseType>('/api/upload', formData);
// Public endpoints (no authentication)
const publicData = await apiService.getPublic<ResponseType>('/api/public/endpoint');
```
**Benefits of using ApiService:**
- Automatic authentication token management
- Token refresh on expiration
- Consistent error handling across all API calls
- CSRF protection for web requests
- User session management across browser tabs
## Quantum Cryptography API
### Backend API Endpoints
All quantum endpoints are available under `/api/v1/quantum/` with authentication required:
```php
// Key generation
POST /api/v1/quantum/generate-keypair
{
"algorithm": "ML-KEM-768", // ML-KEM-512, ML-KEM-768, ML-KEM-1024
"security_level": 768
}
// Key encapsulation
POST /api/v1/quantum/encapsulate
{
"public_key": "base64_encoded_key",
"algorithm": "ML-KEM-768"
}
// Key decapsulation
POST /api/v1/quantum/decapsulate
{
"ciphertext": "base64_encoded_ciphertext",
"private_key": "base64_encoded_key",
"algorithm": "ML-KEM-768"
}
// Device registration
POST /api/v1/quantum/register-device
{
"device_name": "My Phone",
"device_type": "mobile",
"capabilities": ["ml-kem-768", "ml-kem-1024"]
}
// Algorithm negotiation
GET /api/v1/quantum/conversations/{id}/negotiate-algorithm
// Health check
GET /api/v1/quantum/health
```
### Frontend Usage Examples
#### Basic Quantum Encryption
```typescript
import { useQuantumE2EE } from '@/hooks/useQuantumE2EE';
import apiService from '@/services/ApiService';
const { encryptMessage, quantumStatus } = useQuantumE2EE();
// Generate quantum keypair using ApiService
const keypair = await apiService.post('/api/v1/quantum/generate-keypair', {
algorithm: 'ML-KEM-768',
security_level: 768
});
// Encrypt with quantum algorithm
const encrypted = await encryptMessage(
"Hello, quantum world!",
conversationId,
"ML-KEM-768"
);
```
#### Device Management
```tsx
import { QuantumDeviceManager } from '@/components/ui/quantum-device-manager';
<QuantumDeviceManager
showAddDevice={true}
className="max-w-4xl"
/>
```
#### Health Monitoring
```tsx
import { QuantumHealthIndicator } from '@/components/ui/quantum-health-indicator';
<QuantumHealthIndicator
showDetails={true}
autoRefresh={true}
refreshInterval={30000}
/>
```
#### Migration Management
```typescript
import { quantumMigrationUtils } from '@/utils/QuantumMigrationUtils';
import apiService from '@/services/ApiService';
// Register device with quantum capabilities using ApiService
const deviceRegistration = await apiService.post('/api/v1/quantum/register-device', {
device_name: 'My Device',
device_type: 'desktop',
capabilities: ['ml-kem-768', 'ml-kem-1024']
});
// Assess migration readiness
const assessment = await quantumMigrationUtils.assessMigrationReadiness();
// Start migration
const migrationId = await quantumMigrationUtils.startMigration('gradual');
// Monitor progress
const status = quantumMigrationUtils.getMigrationStatus();
```
#### Admin Dashboard
```tsx
import { QuantumAdminPanel } from '@/components/ui/quantum-admin-panel';
<QuantumAdminPanel className="container mx-auto" />
```
### Quantum Algorithm Support
#### Supported Algorithms
- **ML-KEM-512**: 128-bit security level, smaller keys (fastest)
- **ML-KEM-768**: 192-bit security level, balanced performance (recommended)
- **ML-KEM-1024**: 256-bit security level, largest keys (highest security)
- **HYBRID-RSA4096-MLKEM768**: Transition mode combining RSA and ML-KEM
#### Algorithm Selection Priority
1. **ML-KEM-768**: Default for new conversations
2. **HYBRID-RSA4096-MLKEM768**: For mixed classical/quantum device compatibility
3. **RSA-4096-OAEP**: Fallback for legacy devices
#### Encryption Version History
- **v1**: Legacy (deprecated)
- **v2**: RSA-based encryption (current classical)
- **v3**: Quantum-resistant encryption (ML-KEM based)
### Testing Quantum Features
#### Backend Testing
```bash
# Run quantum-specific tests
php artisan test --filter=Quantum
# Test ML-KEM provider implementations
php artisan test tests/Feature/QuantumCryptoServiceTest.php
# Test API endpoints
php artisan test tests/Feature/Api/QuantumControllerTest.php
```
#### Frontend Testing
```bash
# Run quantum E2EE tests
./scripts/test-e2ee.sh quantum
# Test device migration
npm run test:e2e -- --grep "quantum.*migration"
# Test algorithm negotiation
npm run test:e2e -- --grep "quantum.*negotiation"
```
#### Development Setup
```bash
# Install LibOQS (production ML-KEM provider)
# Ubuntu/Debian:
sudo apt-get install liboqs-dev
# macOS:
brew install liboqs
# For development/testing, fallback provider is used automatically
# when LibOQS is not available
```