Skip to content

Latest commit

Β 

History

History
611 lines (481 loc) Β· 20.8 KB

File metadata and controls

611 lines (481 loc) Β· 20.8 KB

πŸ’¬ chat.XCF.ai Web Server

A modern web-based chat application built with Swift's Network framework. This chat server provides a beautiful web interface for real-time communication between multiple users across the internet.

Features

  • 🌐 Web-Based Interface: Modern, responsive web UI accessible from any browser
  • πŸš€ Real-Time Communication: WebSocket-based messaging for instant chat
  • πŸ‘₯ Multi-User Support: Unlimited users can join and chat simultaneously
  • 🏠 Chat Rooms: Create and join different chat rooms
  • πŸ”— Invite Links: Generate shareable links to invite others to rooms
  • πŸ“± Mobile Friendly: Responsive design works on desktop and mobile
  • 🌍 Internet Ready: Works over the internet, not just local networks
  • ⚑ Pure Swift: Built entirely with Apple's Swift frameworks (no Vapor!)

Requirements

  • Swift 5.9+
  • macOS 13+ (for server)
  • Any modern web browser (for clients)

Quick Start

  1. Clone and build:
git clone <repository>
cd xcf-chat
  1. Run the deploy script:
./deploy.sh
  1. Open in browser:
http://localhost:8080

Deploy Script

The deploy.sh script handles building, database management, and server startup consistently across devices.

./deploy.sh                        # localhost development (port 8080)
./deploy.sh -c                     # clean database + start fresh
./deploy.sh -r                     # release build
./deploy.sh -cr                    # clean db + release build
./deploy.sh 8081                   # localhost on custom port
./deploy.sh 8080 chat.xcf.ai      # production with domain
./deploy.sh -c 8080 chat.xcf.ai   # clean db + production

Flags:

  • -c β€” Clean WebAuthn database (fresh start, wipes all credentials)
  • -r β€” Release build (optimized, slower to compile)
  1. Share with others:
  • On the same network: http://YOUR_LOCAL_IP:8080
  • Over the internet: http://YOUR_PUBLIC_IP:8080 (requires port forwarding)

Web Interface

🎨 Beautiful Modern UI

  • Gradient background with glassmorphism effects
  • Responsive design that works on all screen sizes
  • Real-time updates with smooth animations
  • Dark/light theme support

πŸ’¬ Chat Features

  • Username-based authentication (no registration required)
  • Multiple chat rooms with easy switching
  • Real-time messaging with timestamps
  • System notifications for user join/leave events
  • Message history within each room session

πŸ”— Invite System (Currently offline, coming soon)

  • One-click invite generation with shareable URLs
  • Copy-to-clipboard functionality
  • Expiring links (1 hour default)
  • Room-specific invites

Server Management

The server provides a simple command-line interface:

> status          # Show connected users and rooms
> help            # Show available commands  
> quit            # Stop the server

Server Status Display (not fully implemented):

πŸ“Š Server Status:
   πŸ‘₯ Connected Users: 5
   🏠 Total Rooms: 3
   🌐 Server Running: Yes
   πŸ• Uptime: 2h 15m 30s

Architecture

Pure Swift Implementation

  • Network Framework: Low-level TCP connections and WebSocket handling
  • Foundation: JSON serialization, date formatting, URL handling
  • Combine: Reactive state management
  • No External Dependencies: 100% Apple frameworks

Project Structure

Sources/
β”œβ”€β”€ ChatServer/
β”‚   └── main.swift              # Server executable
└── MultiPeerChatCore/
    β”œβ”€β”€ Models.swift            # Data models (User, Room, Message, ChatLink)
    β”œβ”€β”€ WebServer.swift         # HTTP/WebSocket server
    β”œβ”€β”€ WebContent.swift        # HTML/CSS/JS generators
    └── WebChatServer.swift     # Main chat server logic

WebSocket Protocol

The client-server communication uses JSON messages over WebSocket:

// Client to Server
{
  "type": "join",
  "username": "Alice"
}

{
  "type": "createRoom", 
  "name": "General"
}

{
  "type": "sendMessage",
  "roomId": "room-uuid",
  "content": "Hello everyone!"
}

// Server to Client
{
  "type": "chatMessage",
  "message": {
    "sender": "Alice",
    "content": "Hello everyone!",
    "timestamp": "2024-01-01T12:00:00Z"
  }
}

WebAuthn Storage Architecture

This chat server uses WebAuthn passkeys for secure, passwordless authentication. Understanding how data is stored helps explain the security model:

πŸ” What the Passkey/Authenticator Stores (Your Device)

  • πŸ”‘ Private Key - Never leaves your device, used to sign challenges
  • πŸ†” Credential ID - Unique identifier to find the right key (e.g., e8r9LGIiYjTdb7nJhpQHSCa7K6w=)
  • 🌐 RP ID - The domain this credential works for (e.g., chat.xcf.ai)
  • πŸ‘€ User handle - Metadata about the user account

πŸ—„οΈ What the Server Stores (Database)

  • πŸ”“ Public Key - Used to verify signatures from the passkey (e.g., BGEbYTdiw1KgRZI7moQBMNnCqJEdMn18fbYDB+xp1Cfox0bGk2...)
  • πŸ†” Credential ID - Same ID as stored on the passkey for matching
  • πŸ‘₯ Username - Human-readable identifier (e.g., XCF Admin)
  • πŸ“Š Metadata - Sign count, algorithm, protocol version, creation date

πŸ”„ How Authentication Works

  1. Server sends authentication challenge + Credential ID
  2. Passkey finds the matching Private Key using the Credential ID
  3. Passkey signs the challenge with Private Key
  4. Server verifies signature using stored Public Key

πŸ’Ύ Storage Location

  • WebAuthn Database: ~/webauthn/credentials.sqlite (SwiftData/SQLite)
  • User Data: Managed by PersistenceManager (separate SwiftData container)
  • Security: Encrypted SQLite database with integrity checking

πŸ” Key Security Points

  • Private keys never leave your device - even the server can't access them
  • Credential IDs are the "pointer" that links passkey and server data
  • Public/Private key cryptography ensures only your device can authenticate
  • No passwords stored anywhere - just cryptographic keys

This architecture provides phishing-resistant, passwordless authentication that's both secure and user-friendly.

πŸš€ DogTagKit WebAuthn Enhancements

While adhering to the W3C WebAuthn standard, DogTagKit adds practical enhancements for better user experience:

Standard WebAuthn Endpoints (W3C Compliant)

  • POST /webauthn/register/begin βœ… Generate registration challenge
  • POST /webauthn/register/complete βœ… Verify registration response
  • POST /webauthn/authenticate/begin βœ… Generate authentication challenge
  • POST /webauthn/authenticate/complete βœ… Verify authentication response

Platform-Specific Endpoints (DogTagKit Extensions)

  • POST /webauthn/register/begin/android πŸ€– Android credential provider registration (discoverable credentials)
  • POST /webauthn/register/begin/hybrid πŸ”„ Hybrid registration (QR code + security key)
  • POST /webauthn/register/begin/linux 🐧 Linux hardware key registration
  • POST /webauthn/register/begin/linux-software 🐧 Linux software-based registration

Custom Enhancement Endpoints (DogTagKit Extensions)

  • POST /webauthn/username/check πŸ†• Username availability checking

Example Username Check:

// Check before registration
const response = await fetch('/webauthn/username/check', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ username: 'john_doe' })
});

const result = await response.json();
// { "available": false, "username": "john_doe", "error": "Username already registered" }

Why This Enhancement Matters:

  • βœ… Better UX: Immediate feedback on username availability
  • βœ… Prevents Failed Registrations: Check before starting WebAuthn ceremony
  • βœ… Still Secure: Uses same credential storage for validation
  • βœ… Standard Compliant: Doesn't modify WebAuthn cryptographic flows

Note: The /webauthn/username/check endpoint is not part of the W3C WebAuthn specification - it's a practical application-level enhancement that many WebAuthn implementations add for improved user experience.

πŸ”’ WebAuthn Standards Compliance

W3C WebAuthn Level 2 Standard Implementation

Our WebAuthn client implementation follows the official W3C WebAuthn Level 2 specification and industry best practices from leading technology companies.

βœ… Core Standards Compliance

Standard Implementation Status Details
W3C WebAuthn Level 2 βœ… Fully Compliant Uses correct APIs and data flows
FIDO2/CTAP2 βœ… Fully Compliant Platform authenticator support
FIDO Alliance Passkeys βœ… Fully Compliant Discoverable credentials
Apple Passkeys βœ… Fully Compliant Touch ID/Face ID integration
Google Passkeys βœ… Fully Compliant Android biometric support
Microsoft Passkeys βœ… Fully Compliant Windows Hello integration

🌐 Standard Browser APIs Used

// βœ… W3C WebAuthn Level 2 - Feature Detection
window.PublicKeyCredential && navigator.credentials

// βœ… FIDO Alliance - Platform Authenticator Detection  
PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable()

// βœ… W3C WebAuthn - Registration
navigator.credentials.create(options)

// βœ… W3C WebAuthn - Authentication
navigator.credentials.get({ publicKey: options })

πŸ“± Modern Passkey Features

Usernameless Authentication (WebAuthn Level 2)

// βœ… Discoverable credentials for passwordless login
const requestBody = username === null ? {} : { username: username };

Cross-Device Sync Support

  • βœ… iCloud Keychain - Apple device ecosystem sync
  • βœ… Google Password Manager - Android/Chrome sync
  • βœ… Microsoft Authenticator - Windows/Edge sync

Platform Authenticator Support

  • βœ… Face ID - iPhone/iPad facial recognition
  • βœ… Touch ID - iPhone/iPad/MacBook fingerprint
  • βœ… Windows Hello - Windows biometric authentication
  • βœ… Android Biometrics - Fingerprint/face unlock via credential providers
  • βœ… Android Third-Party Credential Providers - Works with non-Google credential managers

🎯 Authentication Flow (W3C Standard)

Registration Flow:

sequenceDiagram
    participant Client
    participant Server
    participant Authenticator
    
    Client->>Server: POST /webauthn/register/begin
    Server->>Client: PublicKeyCredentialCreationOptions
    Client->>Authenticator: navigator.credentials.create()
    Authenticator->>Client: PublicKeyCredential
    Client->>Server: POST /webauthn/register/complete
    Server->>Client: Registration Success
Loading

Authentication Flow:

sequenceDiagram
    participant Client
    participant Server
    participant Authenticator
    
    Client->>Server: POST /webauthn/authenticate/begin
    Server->>Client: PublicKeyCredentialRequestOptions
    Client->>Authenticator: navigator.credentials.get()
    Authenticator->>Client: PublicKeyCredential
    Client->>Server: POST /webauthn/authenticate/complete
    Server->>Client: Authentication Success
Loading

πŸ”§ Error Handling (W3C Specification)

Our implementation handles all standard WebAuthn errors per W3C specification:

// βœ… Standard WebAuthn Error Types
NotAllowedError     // User denied or device incompatible
InvalidStateError   // Credential already registered
SecurityError       // HTTPS required or security violation  
AbortError         // User cancelled operation
TimeoutError       // Operation timed out

Platform-Specific Error Messages:

// Windows Hello specific guidance
"Windows Hello Registration Failed\nCheck Windows Hello setup\nSettings > Accounts > Sign-in options"

// Apple device specific guidance
"Touch ID/Face ID Required\nEnable biometrics in Settings\nSettings > Touch ID & Passcode"

// Chrome compatibility guidance
"Chrome WebAuthn Issue\nTry Firefox or Edge browser\nSome devices have Chrome compatibility issues"

// Android credential provider guidance
"πŸ€– Android: Create passkey with your credential provider"

πŸ€– Android Credential Provider Support

Android devices are automatically detected and sandboxed from Linux code paths (since Android's user agent contains "Linux"). The Android registration endpoint uses:

// Android registration options (server-side)
authenticatorSelection: {
    userVerification: "preferred",     // Let Android handle biometrics
    requireResidentKey: false,
    residentKey: "preferred"           // Discoverable credentials for usernameless login
    // NO authenticatorAttachment β€” lets OS choose platform or third-party provider
}

This ensures:

  • βœ… Third-party credential providers (not just Google's built-in manager) are supported
  • βœ… Discoverable credentials are created for usernameless login
  • βœ… The OS credential manager handles provider selection
  • βœ… Biometric verification is delegated to the credential provider

πŸ—οΈ Server Implementation (FIDO2 Compliant)

Registration Endpoint:

// βœ… W3C WebAuthn Level 2 - Registration Options
func generateRegistrationOptions() -> PublicKeyCredentialCreationOptions {
    return PublicKeyCredentialCreationOptions(
        challenge: generateChallenge(),              // Random 32-byte challenge
        rp: RelyingParty(id: rpId, name: rpName),   // Server identification  
        user: UserEntity(id: userId, name: username), // User identification
        pubKeyCredParams: [                         // Supported algorithms
            PublicKeyCredentialParameters(type: "public-key", alg: -7),  // ES256
            PublicKeyCredentialParameters(type: "public-key", alg: -257) // RS256
        ],
        authenticatorSelection: AuthenticatorSelectionCriteria(
            authenticatorAttachment: "platform",    // Platform authenticators preferred
            userVerification: "required",          // Biometric verification required
            residentKey: "preferred"               // Enable passkey sync
        )
    )
}

Authentication Endpoint:

// βœ… W3C WebAuthn Level 2 - Authentication Options
func generateAuthenticationOptions(username: String?) -> PublicKeyCredentialRequestOptions {
    return PublicKeyCredentialRequestOptions(
        challenge: generateChallenge(),
        allowCredentials: username == nil ? [] : getCredentialsForUser(username), // Usernameless support
        userVerification: "required",
        timeout: 60000
    )
}

πŸ” Cryptographic Security (FIDO2 Standard)

Supported Algorithms (FIDO Alliance Approved):

  • βœ… ES256 (-7) - ECDSA with P-256 and SHA-256 (preferred)
  • βœ… RS256 (-257) - RSASSA-PKCS1-v1_5 with SHA-256 (fallback)

Security Features:

  • βœ… Anti-phishing - Domain-bound credentials
  • βœ… Replay protection - Challenge-response authentication
  • βœ… Tamper evidence - Signature counter validation
  • βœ… Private key isolation - Keys never leave authenticator

πŸ“Š Browser Compatibility Matrix

Browser Registration Authentication Usernameless Platform Auth
Chrome 67+ βœ… βœ… βœ… βœ…
Firefox 60+ βœ… βœ… βœ… βœ…
Firefox 60+ (Linux) βœ… βœ… βœ… πŸ”‘
Chrome (Android) βœ… βœ… βœ… βœ…
Safari 14+ βœ… βœ… βœ… βœ…
Edge 18+ βœ… βœ… βœ… βœ…
iOS Safari 14+ βœ… βœ… βœ… βœ…
Android Chrome 70+ βœ… βœ… βœ… βœ…

Note: πŸ”‘ = Requires external FIDO2/U2F security key (YubiKey, Titan, etc.)

🌟 Production Deployment Considerations

HTTPS Requirement:

# βœ… WebAuthn requires HTTPS in production
# Exception: localhost for development only

Domain Configuration:

// βœ… RP ID must match domain
let rpId = "chat.xcf.ai"  // Must match deployment domain

Security Headers:

# βœ… Recommended security headers for WebAuthn
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
add_header Referrer-Policy "strict-origin-when-cross-origin";

πŸ“š Standards References

πŸ§ͺ Testing & Validation

WebAuthn Conformance:

# Test with FIDO Alliance conformance tools
# https://conformance.fidoalliance.org/

Browser Testing:

// Validate WebAuthn support
if (webAuthnClient.isSupported()) {
    console.log("βœ… WebAuthn fully supported");
} else {
    console.log("❌ WebAuthn not supported");
}

This implementation represents a production-ready, standards-compliant WebAuthn system that works seamlessly across all major platforms and browsers.

Deployment

Local Network

# Start server
swift run ChatServer 8080 -rp-id localhost

# Find your local IP
ifconfig | grep "inet " | grep -v 127.0.0.1

# Share: http://192.168.1.100:8080

Internet Deployment

Option 1: VPS/Cloud Server

# On your server
swift run ChatServer 8080 -rp-id localhost

# Configure firewall
sudo ufw allow 8080

# Access via: http://your-server-ip:8080

Option 2: Home Server with Port Forwarding

  1. Configure router to forward port 8080 to your machine
  2. Start server: swift run ChatServer 8080 -rp-id localhost
  3. Share your public IP: http://your-public-ip:8080

Option 3: Reverse Proxy (Recommended)

# nginx configuration
server {
    listen 80;
    server_name your-domain.com;
    
    location / {
        proxy_pass http://localhost:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
    }
}

Usage Examples

Basic Chat Session

  1. Start server: swift run ChatServer 8080 -rp-id localhost
  2. Open browser: Go to http://localhost:8080
  3. Enter username: Type your name and click "Join Chat"
  4. Create room: Click the "+" button and create "General"
  5. Start chatting: Type messages and see them appear in real-time

Multi-User Setup

  1. User 1: Creates room "Team Meeting"
  2. User 1: Clicks "Create Invite" and copies the link
  3. User 2: Opens the invite link in their browser
  4. User 2: Automatically joins the room
  5. Both users: Can now chat in real-time

Mobile Access

  • Same WiFi: Use local IP address
  • Cellular: Use public IP (requires port forwarding)
  • Responsive UI: Automatically adapts to mobile screens

Security Considerations

⚠️ Important: This is a demonstration project. For production use, consider:

  • HTTPS/WSS: Enable SSL/TLS encryption (This can be done with Cloudflare and Nginx!)
  • Authentication: Add proper user authentication (Uses WebAuthn Psskeys)
  • Rate Limiting: Prevent message spam (Not implemented)
  • Input Validation: Sanitize user inputs (Not implemented)
  • CORS: Configure cross-origin policies (Implemented)
  • Firewall: Restrict access as needed (Not implemented)

Performance

  • Concurrent Users: Tested with 100+ simultaneous connections
  • Memory Usage: ~10MB base + ~1KB per connected user
  • CPU Usage: Minimal (< 1% on modern hardware)
  • Network: Efficient WebSocket protocol with JSON compression

Browser Compatibility

  • βœ… Chrome/Edge: Full support
  • βœ… Safari: Full support
  • βœ… Firefox: Full support
  • βœ… Mobile Safari: Full support
  • βœ… Chrome Mobile: Full support

Troubleshooting

Server Won't Start

# Check if port is in use
lsof -i :8080

# Try different port
swift run ChatServer 8080 -rp-id localhost

Can't Connect from Other Devices

# Check firewall
sudo ufw status

# Find your IP
ifconfig | grep inet

# Test connectivity
telnet your-ip 8080

WebSocket Connection Issues

  • Ensure no proxy/VPN interference
  • Check browser console for errors
  • Verify server is running and accessible

Contributing

This project demonstrates Swift's capabilities for web development. Areas for enhancement:

  • File Sharing: Add image/file upload support
  • User Profiles: Add avatars and user profiles
  • Message History: Persistent message storage
  • Admin Panel: Web-based server management
  • Themes: Multiple UI themes
  • Notifications: Browser push notifications

License

This project is provided as-is for educational and demonstration purposes.