# Client-Server Architecture — Ninja Nexus

## Model

Ninja Nexus følger en **hybrid client-server** arkitektur med stærk lokal kapabilitet:

- **Client (PC):** Shell + Agent — autonom drift mulig offline
- **Server (Cloud):** Control Center + moduler — autoritativ kilde for politikker, identitet og administration

---

## Client-komponenter

### Nexus Shell (User-mode app)

| Egenskab | Værdi |
|----------|-------|
| Processtype | Interaktiv brugerproces |
| Kører som | Logged-in bruger |
| Starter | Ved login (erstatter explorer.exe eller kører fullscreen) |
| Netværk | HTTPS til cloud; IPC til Agent |
| Offline | Begrænset (cached profil, launcher, seneste apps) |

### Nexus Agent (Windows Service)

| Egenskab | Værdi |
|----------|-------|
| Processtype | Windows Service (auto-start) |
| Kører som | NETWORK SERVICE eller custom service account |
| Starter | Ved boot (før bruger-login) |
| Netværk | mTLS til Control Center |
| Offline | Fuld politik-cache, inventar-kø, install-kø |

---

## Server-komponenter

### API Gateway

- Single entry point: `https://api.nexus.{domain}/v1/`
- Rate limiting, request validation
- JWT validation (fra Identity)
- Device certificate validation (for Agent)
- Routing til backend services

### Backend Services

| Service | Endpoints (eksempler) | Auth |
|---------|----------------------|------|
| Identity | `/auth/*`, `/users/*`, `/roles/*` | Public (login) + Admin JWT |
| Device Mgmt | `/devices/*`, `/policies/*` | Admin JWT + Agent mTLS |
| App Center | `/apps/*`, `/catalog/*` | User JWT + Admin JWT |
| Update | `/packages/*`, `/deployments/*` | Agent mTLS + Admin JWT |
| Remote Support | `/sessions/*` | Admin JWT + User consent |

---

## Forbindelsesmønstre

### Agent → Server (primær)

```
Frekvens:
  - Heartbeat:     hvert 60 sekund
  - Policy sync:   hvert 5. minut (eller push)
  - Inventory:     hvert 15. minut (delta)
  - Install check: on-demand + hvert 30. minut

Protokol:
  - REST over HTTPS (mTLS)
  - WebSocket for push events (policy change, remote session invite)

Retry:
  - Exponential backoff: 1s, 2s, 4s, 8s, max 5 min
  - Offline queue: max 10.000 events, FIFO
```

### Shell → Server (sekundær)

```
Frekvens:
  - Login:         on-demand
  - Profile sync:  ved login + hvert 30. minut
  - App catalog:   ved login + cache 24 timer

Protokol:
  - HTTPS (OIDC for auth, REST for data)
  - Primær data via Agent IPC (hurtigere, offline-capable)
```

### Admin → Server

```
Frekvens:  On-demand (browser)
Protokol:  HTTPS (SPA → API)
Auth:      OIDC + MFA required
```

---

## Enhedsregistrering

```
1. Agent installeres (MSI/Intune) med bootstrap token
2. Agent → Control Center: POST /devices/register
   { bootstrap_token, hardware_id, os_info }
3. Control Center: validerer token, opretter device record, udsteder device certificate
4. Agent: gemmer certificate i TPM/CNG (preferred) eller encrypted store
5. Alle fremtidige requests: mTLS med device certificate
6. Ved første bruger-login: binding device ↔ user
```

---

## Multi-site og edge (fremtid)

```
                    ┌──────────────┐
                    │  Global API   │
                    │  (Primary)    │
                    └──────┬───────┘
                           │
              ┌────────────┼────────────┐
              ▼            ▼            ▼
        ┌──────────┐ ┌──────────┐ ┌──────────┐
        │ Edge EU  │ │ Edge US  │ │ Edge APAC│
        │ (cache)  │ │ (cache)  │ │ (cache)  │
        └────┬─────┘ └────┬─────┘ └────┬─────┘
             │            │            │
          Agents       Agents       Agents
```

Edge-noder cacher politikker og app-pakker. Identity forbliver central (eller federeret).

---

## Latens-krav

| Operation | Maks latens | Offline OK? |
|-----------|-------------|-------------|
| Login | < 3s | Nej |
| Desktop load | < 2s | Ja (cache) |
| Launcher søgning | < 200ms | Ja (lokal index) |
| Policy apply | < 30s efter modtagelse | Ja (cached policy) |
| App install | Varierer | Kø offline |
| Remote support connect | < 5s | Nej |

---

*Se også: [OFFLINE_STRATEGY.md](./OFFLINE_STRATEGY.md) · [DATA_FLOW.md](./DATA_FLOW.md)*
