Umbau broekr auf client-id
This commit is contained in:
1 parent
268e150f2b
commit
a95204b8d6
17 files changed
+649
-388
No files matched your search
+66
-29
@@ -3,8 +3,8 @@ WebSocket Routes
|
||||
|
||||
Ablauf einer Verbindung:
|
||||
1. Client verbindet sich mit /<tenant>/ws.
|
||||
2. Client sendet Code 0 mit dem zuvor über die API geholten Einmal-Token
|
||||
und seiner persistenten Sitzungs-ID.
|
||||
2. Client sendet Code 0 mit dem zuvor über die API geholten Einmal-Token und
|
||||
seiner Client-ID. Die Client-ID wird pro Webview erzeugt.
|
||||
3. Erst nach erfolgreicher Prüfung dürfen fachliche Nachrichten gesendet
|
||||
werden. Vorher wird die Verbindung geschlossen.
|
||||
"""
|
||||
@@ -17,9 +17,16 @@ from cmbnet.ws.protocol import WSMessage
|
||||
|
||||
|
||||
def _parse_auth_payload(value):
|
||||
"""Liefert (Token, Sitzungs-ID) aus dem Code-0-Payload."""
|
||||
"""Liefert (Token, Client-ID) aus dem Code-0-Payload.
|
||||
|
||||
Ältere Clients senden statt ``client_id`` noch ``session_id``. Der
|
||||
alte Schlüssel wird übergangsweise weiter akzeptiert.
|
||||
"""
|
||||
if isinstance(value, dict):
|
||||
return value.get('token'), value.get('session_id')
|
||||
return (
|
||||
value.get('token'),
|
||||
value.get('client_id') or value.get('session_id')
|
||||
)
|
||||
|
||||
if isinstance(value, str):
|
||||
text = value.strip()
|
||||
@@ -29,7 +36,10 @@ def _parse_auth_payload(value):
|
||||
except (TypeError, ValueError):
|
||||
return None, None
|
||||
if isinstance(data, dict):
|
||||
return data.get('token'), data.get('session_id')
|
||||
return (
|
||||
data.get('token'),
|
||||
data.get('client_id') or data.get('session_id')
|
||||
)
|
||||
return text, None
|
||||
|
||||
return None, None
|
||||
@@ -52,10 +62,9 @@ def ws(ws, tenant):
|
||||
ws.close()
|
||||
return
|
||||
|
||||
# Verbindung registrieren, aber noch nicht als authentifiziert markieren.
|
||||
if not broker.register_connection(tenant_id, account_id, ws):
|
||||
ws.close()
|
||||
return
|
||||
# Die Verbindung wird erst mit dem Code-0-Handshake registriert. Vorher
|
||||
# ist die Client-ID noch nicht bekannt.
|
||||
client_id = None
|
||||
|
||||
try:
|
||||
while True:
|
||||
@@ -74,43 +83,68 @@ def ws(ws, tenant):
|
||||
# Die Absender-ID stammt immer aus der Session und kann vom Client
|
||||
# nicht gefälscht werden.
|
||||
msg.sender_id = account_id
|
||||
if client_id:
|
||||
msg._sender_client_id = client_id
|
||||
|
||||
# Code 0: Authentifizierung über den Einmal-Token.
|
||||
# Code 0: Authentifizierung über den Einmal-Token und Registrierung
|
||||
# der Client-ID.
|
||||
if msg.code == 0:
|
||||
token, session_id = _parse_auth_payload(msg.msg)
|
||||
if not session_id:
|
||||
session_id = getattr(msg, 'session_id', None)
|
||||
if session_id:
|
||||
msg.session_id = session_id
|
||||
if not token or not broker.authenticate(
|
||||
tenant_id, account_id, token, session_id
|
||||
):
|
||||
if client_id:
|
||||
broker.log_incoming(
|
||||
tenant_id, msg, recipients=[],
|
||||
note='Code-0-Handshake fehlgeschlagen'
|
||||
)
|
||||
current_app.logger.warning(
|
||||
'WebSocket-Authentifizierung fehlgeschlagen: tenant=%s account=%s',
|
||||
tenant_id, account_id
|
||||
tenant_id, msg, recipients=[client_id],
|
||||
note='Code-0-Handshake nach bereits erfolgreicher Authentifizierung'
|
||||
)
|
||||
ws.close()
|
||||
break
|
||||
|
||||
token, requested_client_id = _parse_auth_payload(msg.msg)
|
||||
if not requested_client_id:
|
||||
requested_client_id = getattr(msg, 'client_id', None)
|
||||
client_id = broker.normalize_client_id(requested_client_id, account_id)
|
||||
msg.client_id = client_id
|
||||
|
||||
if not token or not broker.register_connection(
|
||||
tenant_id, account_id, client_id, ws
|
||||
):
|
||||
broker.log_incoming(
|
||||
tenant_id, msg, recipients=[],
|
||||
note='Code-0-Registrierung fehlgeschlagen'
|
||||
)
|
||||
current_app.logger.warning(
|
||||
'WebSocket-Registrierung fehlgeschlagen: tenant=%s account=%s client=%s',
|
||||
tenant_id, account_id, client_id
|
||||
)
|
||||
ws.close()
|
||||
break
|
||||
|
||||
if not broker.authenticate(tenant_id, account_id, client_id, token):
|
||||
broker.log_incoming(
|
||||
tenant_id, msg, recipients=[client_id],
|
||||
note='Code-0-Handshake fehlgeschlagen'
|
||||
)
|
||||
current_app.logger.warning(
|
||||
'WebSocket-Authentifizierung fehlgeschlagen: tenant=%s account=%s client=%s',
|
||||
tenant_id, account_id, client_id
|
||||
)
|
||||
ws.close()
|
||||
break
|
||||
|
||||
broker.log_incoming(
|
||||
tenant_id, msg, recipients=[account_id],
|
||||
tenant_id, msg, recipients=[client_id],
|
||||
note='Code-0-Handshake erfolgreich'
|
||||
)
|
||||
# Bestätigt dem Client den erfolgreichen Handshake. Erst jetzt
|
||||
# darf er gepufferte fachliche Nachrichten senden.
|
||||
broker.send_to_connection(
|
||||
tenant_id,
|
||||
account_id,
|
||||
client_id,
|
||||
WSMessage(sender_id=0, code=0, msg='authenticated').to_json()
|
||||
)
|
||||
continue
|
||||
|
||||
# Fachliche Nachrichten sind erst nach erfolgreicher
|
||||
# Authentifizierung erlaubt.
|
||||
if not broker.is_authenticated(tenant_id, account_id):
|
||||
if not client_id or not broker.is_authenticated(tenant_id, client_id):
|
||||
broker.log_incoming(
|
||||
tenant_id, msg, recipients=[],
|
||||
note='Nachricht vor Code-0-Authentifizierung'
|
||||
@@ -118,9 +152,12 @@ def ws(ws, tenant):
|
||||
ws.close()
|
||||
break
|
||||
|
||||
broker.route_message(tenant_id, msg, apply_business=True)
|
||||
broker.route_message(
|
||||
tenant_id, msg, apply_business=True, client_id=client_id
|
||||
)
|
||||
|
||||
except Exception as exc:
|
||||
print(f'WebSocket error: {exc}')
|
||||
finally:
|
||||
broker.remove_connection(tenant_id, account_id, ws)
|
||||
if client_id:
|
||||
broker.remove_connection(tenant_id, client_id, ws)
|
||||
Reference in new issue
Block a user