Files
Ninjaserver/README.md
2025-09-23 14:13:24 +02:00

223 lines
5.1 KiB
Markdown

# 🔐 Lizenzgenerator mit PostgreSQL Integration
Ein sicherer Lizenzgenerator mit PostgreSQL-Datenbank, interaktiver Karte und API-Key Authentifizierung.
## ✨ Features
- **🔑 Sichere Lizenzgenerierung** mit HMAC-SHA256
- **🗄️ PostgreSQL Integration** für lokale Datenspeicherung
- **🗺️ Interaktive Karte** mit Leaflet.js und OpenStreetMap
- **🔍 Standortsuche** über Nominatim API
- **🔐 API-Key Authentifizierung** für alle API-Endpunkte
- **🌐 Web-Interface** mit Login-Schutz
- **📱 Responsive Design** für alle Geräte
## 🚀 Installation
### Voraussetzungen
- Node.js (v16 oder höher)
- PostgreSQL Datenbank
- npm oder yarn
### Setup
```bash
# Repository klonen
git clone <repository-url>
cd ninjaserver
# Abhängigkeiten installieren
npm install
# Umgebungsvariablen konfigurieren
cp .env.example .env
# .env-Datei mit Ihren Datenbankdaten bearbeiten
# Datenbank initialisieren
npm run init-db
# Server starten
npm start
```
## 🔐 Authentifizierung
### Web-Interface
- **Standardanmeldung**: `admin` / `admin123`
- **Benutzer erstellen**: `npm run create-user`
### API-Key Authentifizierung
Alle API-Endpunkte erfordern einen gültigen API-Key im `Authorization` Header:
```bash
Authorization: Bearer YOUR_API_KEY_HERE
```
#### API-Key generieren
```bash
curl -X POST http://localhost:3000/api/generate-api-key \
-H "Content-Type: application/json" \
-d '{"description": "Mein API Key", "standorte": "München, Berlin"}'
```
## 📡 API-Endpunkte
### Geschützte Endpunkte (API-Key erforderlich)
#### Standorte abrufen
```bash
curl -X GET http://localhost:3000/api/locations \
-H "Authorization: Bearer YOUR_API_KEY"
```
#### Neuen Standort erstellen
```bash
curl -X POST http://localhost:3000/api/create-location \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{"name": "München", "lat": 48.1351, "lon": 11.5820}'
```
#### API-Tokens abrufen
```bash
curl -X GET http://localhost:3000/api/tokens \
-H "Authorization: Bearer YOUR_API_KEY"
```
#### Token validieren
```bash
curl -X POST http://localhost:3000/api/validate-token \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{"token": "TOKEN_TO_VALIDATE"}'
```
#### Token speichern
```bash
curl -X POST http://localhost:3000/api/save-token \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{"token": "GENERATED_TOKEN", "description": "Beschreibung", "standorte": "Standorte"}'
```
### Öffentliche Endpunkte (nur Web-Interface)
#### Login
```bash
curl -X POST http://localhost:3000/api/login \
-H "Content-Type: application/json" \
-d '{"username": "admin", "password": "admin123"}'
```
#### Logout
```bash
curl -X POST http://localhost:3000/api/logout
```
## 🗄️ Datenbankstruktur
### `adminusers` Tabelle
```sql
CREATE TABLE adminusers (
id SERIAL PRIMARY KEY,
username VARCHAR(50) UNIQUE NOT NULL,
password_hash VARCHAR(255) NOT NULL,
is_active BOOLEAN DEFAULT true,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
last_login TIMESTAMP
);
```
### `api_tokens` Tabelle
```sql
CREATE TABLE api_tokens (
id SERIAL PRIMARY KEY,
token VARCHAR(255) UNIQUE NOT NULL,
description TEXT,
standorte TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
expires_at TIMESTAMP,
is_active BOOLEAN DEFAULT true
);
```
### `locations` Tabelle
```sql
CREATE TABLE locations (
id SERIAL PRIMARY KEY,
name VARCHAR(255) UNIQUE NOT NULL,
latitude DECIMAL(10, 8) NOT NULL,
longitude DECIMAL(11, 8) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
```
## 🧪 API testen
### Test-Skript verwenden
```bash
# test-api.js bearbeiten und API_KEY setzen
node test-api.js
```
### Manueller Test
```bash
# 1. API-Key generieren
curl -X POST http://localhost:3000/api/generate-api-key \
-H "Content-Type: application/json" \
-d '{"description": "Test Key"}'
# 2. API mit generiertem Key testen
curl -X GET http://localhost:3000/api/locations \
-H "Authorization: Bearer GENERATED_API_KEY"
```
## 📁 Projektstruktur
```
ninjaserver/
├── server.js # Hauptserver-Datei
├── routes/
│ └── api.js # API-Routen mit Bearer Token Auth
├── scripts/
│ ├── init-db.js # Datenbankinitialisierung
│ └── create-user.js # Benutzer-Erstellung
├── public/
│ ├── index.html # Hauptanwendung
│ └── login.html # Login-Seite
├── test-api.js # API-Test-Skript
└── package.json
```
## 🚀 Deployment
### Lokale Entwicklung
```bash
npm run dev # Mit Nodemon für automatisches Neuladen
```
### Produktion
```bash
npm start # Produktionsserver
```
## 🔒 Sicherheit
- **API-Key Authentifizierung** für alle API-Endpunkte
- **Session-basierte Authentifizierung** für Web-Interface
- **Passwort-Hashing** mit bcrypt
- **HTTPS empfohlen** für Produktionsumgebung
- **Regelmäßige API-Key Rotation** empfohlen
## 📝 Lizenz
Proprietär - Alle Rechte vorbehalten
## 👨‍💻 Autor
Carsten Graf
---
**⚠️ Wichtig**: Ändern Sie die Standardpasswörter in der Produktionsumgebung!