automatisch auf INFLUX schalten, wenn es Chi-ID gibt zuzsätzlich option db=m zum erzwingen von Moing Anzeige Mongo/Influx im Datenstrom
187 lines
3.6 KiB
Markdown
187 lines
3.6 KiB
Markdown
# API-Key-Authentifizierung Setup
|
|
|
|
## Schnellstart
|
|
|
|
### 1. API-Keys generieren
|
|
|
|
```bash
|
|
node generate-apikey.js 3
|
|
```
|
|
|
|
Dies generiert 3 zufällige API-Keys wie:
|
|
```
|
|
1. a1b2c3d4e5f6...
|
|
2. f6e5d4c3b2a1...
|
|
3. 1a2b3c4d5e6f...
|
|
```
|
|
|
|
### 2. Konfiguration in .env
|
|
|
|
Kopiere `.env.example` nach `.env` und füge deine Keys hinzu:
|
|
|
|
```bash
|
|
cp .env.example .env
|
|
```
|
|
|
|
Bearbeite `.env`:
|
|
```bash
|
|
# Aktiviere Authentifizierung
|
|
API_AUTH_REQUIRED=true
|
|
|
|
# Füge generierte Keys hinzu (komma-separiert)
|
|
API_KEYS=a1b2c3d4e5f6...,f6e5d4c3b2a1...,1a2b3c4d5e6f...
|
|
```
|
|
|
|
### 3. Server starten/neu starten
|
|
|
|
```bash
|
|
npm start
|
|
```
|
|
|
|
## Verwendung
|
|
|
|
### Mit curl
|
|
|
|
```bash
|
|
# Header-Methode (empfohlen)
|
|
curl -H "X-API-Key: your-key-here" \
|
|
"http://localhost:3000/api/getactdata?sensorid=12345"
|
|
|
|
# Query-Parameter-Methode
|
|
curl "http://localhost:3000/api/getactdata?sensorid=12345&apikey=your-key-here"
|
|
```
|
|
|
|
### Mit JavaScript/Node.js
|
|
|
|
```javascript
|
|
import axios from 'axios'
|
|
|
|
const API_KEY = process.env.SENSOR_API_KEY
|
|
const client = axios.create({
|
|
baseURL: 'http://localhost:3000/api',
|
|
headers: { 'X-API-Key': API_KEY }
|
|
})
|
|
|
|
const data = await client.get('/getactdata', {
|
|
params: { sensorid: 12345 }
|
|
})
|
|
```
|
|
|
|
### Mit Python
|
|
|
|
```python
|
|
import os
|
|
import requests
|
|
|
|
API_KEY = os.getenv('SENSOR_API_KEY')
|
|
headers = {'X-API-Key': API_KEY}
|
|
|
|
response = requests.get(
|
|
'http://localhost:3000/api/getactdata',
|
|
params={'sensorid': 12345},
|
|
headers=headers
|
|
)
|
|
```
|
|
|
|
## Verwaltung
|
|
|
|
### Neuen Key hinzufügen
|
|
|
|
1. Generiere neuen Key: `node generate-apikey.js 1`
|
|
2. Füge zur `API_KEYS` Liste in `.env` hinzu
|
|
3. Restart Server
|
|
|
|
### Key entfernen
|
|
|
|
1. Entferne Key aus `API_KEYS` Liste in `.env`
|
|
2. Restart Server
|
|
|
|
### Authentifizierung deaktivieren
|
|
|
|
```bash
|
|
# In .env
|
|
API_AUTH_REQUIRED=false
|
|
# oder
|
|
API_KEYS=
|
|
```
|
|
|
|
## Sicherheit
|
|
|
|
### ✅ Do's
|
|
|
|
- ✅ Keys in Umgebungsvariablen speichern
|
|
- ✅ Unterschiedliche Keys für verschiedene Clients
|
|
- ✅ HTTPS in Produktion verwenden
|
|
- ✅ Keys regelmäßig rotieren
|
|
- ✅ Zugriffe loggen und überwachen
|
|
|
|
### ❌ Don'ts
|
|
|
|
- ❌ Keys in Git committen
|
|
- ❌ Keys im Frontend-Code verwenden
|
|
- ❌ Denselben Key für alle Clients
|
|
- ❌ Keys über unverschlüsselte Verbindungen senden
|
|
- ❌ Keys in URLs verwenden (bevorzuge Header)
|
|
|
|
## Troubleshooting
|
|
|
|
### "API key required" Fehler
|
|
|
|
- Stelle sicher, dass der Key im Header oder Query-Parameter übergeben wird
|
|
- Prüfe Schreibweise: `X-API-Key` (Header) oder `apikey` (Query)
|
|
|
|
### "Invalid API key" Fehler
|
|
|
|
- Prüfe, ob der Key in der `API_KEYS` Liste vorhanden ist
|
|
- Stelle sicher, dass keine Leerzeichen oder Zeilenumbrüche im Key sind
|
|
- Prüfe, ob `.env` korrekt geladen wird
|
|
|
|
### Keys werden nicht erkannt
|
|
|
|
- Server nach `.env` Änderungen neu starten
|
|
- Prüfe, ob `dotenv` korrekt konfiguriert ist
|
|
- Teste mit: `node -e "console.log(process.env.API_KEYS)"`
|
|
|
|
## Integration in bestehende Systeme
|
|
|
|
### Nginx Proxy
|
|
|
|
```nginx
|
|
location /api/ {
|
|
proxy_pass http://localhost:3000/api/;
|
|
proxy_set_header X-API-Key $http_x_api_key;
|
|
}
|
|
```
|
|
|
|
### Apache Proxy
|
|
|
|
```apache
|
|
<Location /api/>
|
|
ProxyPass http://localhost:3000/api/
|
|
ProxyPassReverse http://localhost:3000/api/
|
|
RequestHeader set X-API-Key %{HTTP:X-API-Key}e
|
|
</Location>
|
|
```
|
|
|
|
## Erweiterte Konfiguration
|
|
|
|
### Programmatische Key-Verwaltung
|
|
|
|
```javascript
|
|
import { addApiKey, removeApiKey, generateApiKey } from './utilities/apiauth.js'
|
|
|
|
// Neuen Key zur Laufzeit hinzufügen
|
|
const newKey = generateApiKey()
|
|
addApiKey(newKey)
|
|
|
|
// Key zur Laufzeit entfernen
|
|
removeApiKey('old-key-here')
|
|
```
|
|
|
|
### Logging
|
|
|
|
Authentifizierungs-Ereignisse werden automatisch geloggt:
|
|
- Erfolgreiche Authentifizierung
|
|
- Fehlgeschlagene Versuche mit IP-Adresse
|
|
- Key-Verwaltungsoperationen
|