opencode ergebnis V0.1
This commit is contained in:
1 parent
61d86a3d83
commit
7e498ebc3f
71 files changed
+28982
-46
No files matched your search
@@ -0,0 +1,70 @@
|
||||
"""
|
||||
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.
|
||||
3. Erst nach erfolgreicher Prüfung dürfen fachliche Nachrichten gesendet
|
||||
werden. Vorher wird die Verbindung geschlossen.
|
||||
"""
|
||||
from flask import session
|
||||
from cmbnet.extensions import sock
|
||||
from cmbnet.ws import broker
|
||||
from cmbnet.ws.protocol import WSMessage
|
||||
|
||||
|
||||
@sock.route('/<tenant>/ws')
|
||||
def ws(ws, tenant):
|
||||
"""WebSocket Endpoint"""
|
||||
from cmbnet.db import get_db
|
||||
|
||||
db = get_db()
|
||||
tenant_row = db.execute('SELECT * FROM tenants WHERE code = ?', (tenant,)).fetchone()
|
||||
if not tenant_row:
|
||||
ws.close()
|
||||
return
|
||||
|
||||
tenant_id = tenant_row['id']
|
||||
account_id = session.get('account_id')
|
||||
if not account_id:
|
||||
ws.close()
|
||||
return
|
||||
|
||||
# Verbindung registrieren, aber noch nicht als authentifiziert markieren.
|
||||
if not broker.register_connection(tenant_id, account_id, ws):
|
||||
ws.close()
|
||||
return
|
||||
|
||||
try:
|
||||
while True:
|
||||
data = ws.receive()
|
||||
if data is None:
|
||||
break
|
||||
|
||||
msg = WSMessage.from_json(data)
|
||||
if not msg:
|
||||
continue
|
||||
|
||||
# Die Absender-ID stammt immer aus der Session und kann vom Client
|
||||
# nicht gefälscht werden.
|
||||
msg.sender_id = account_id
|
||||
|
||||
# Code 0: Authentifizierung über den Einmal-Token.
|
||||
if msg.code == 0:
|
||||
if not broker.authenticate(tenant_id, account_id, msg.msg):
|
||||
ws.close()
|
||||
break
|
||||
continue
|
||||
|
||||
# Fachliche Nachrichten sind erst nach erfolgreicher
|
||||
# Authentifizierung erlaubt.
|
||||
if not broker.is_authenticated(tenant_id, account_id):
|
||||
ws.close()
|
||||
break
|
||||
|
||||
broker.route_message(tenant_id, msg, apply_business=True)
|
||||
|
||||
except Exception as exc:
|
||||
print(f'WebSocket error: {exc}')
|
||||
finally:
|
||||
broker.remove_connection(tenant_id, account_id, ws)
|
||||
Reference in new issue
Block a user