Reafctor app

This commit is contained in:
Oliver Traber 2024-04-13 22:43:03 +02:00
parent b8216f6ade
commit 7740be8bb5
Signed by: Bluemedia
GPG key ID: C0674B105057136C
36 changed files with 389 additions and 208 deletions

0
app/util/__init__.py Normal file
View file

View file

@ -0,0 +1,38 @@
import base64
import binascii
from uuid import UUID
from starlette.authentication import (
AuthCredentials, AuthenticationBackend, AuthenticationError, SimpleUser
)
from app.database import SessionLocal
from app.models.chargepoint import ChargePoint
class BasicAuthBackend(AuthenticationBackend):
async def authenticate(self, conn):
if "Authorization" not in conn.headers:
raise AuthenticationError('No Authorization header provided')
auth = conn.headers["Authorization"]
try:
scheme, credentials = auth.split()
if scheme.lower() != 'basic':
raise AuthenticationError('Invalid authorization scheme')
decoded = base64.b64decode(credentials).decode("ascii")
except (ValueError, UnicodeDecodeError, binascii.Error) as exc:
raise AuthenticationError('Invalid basic auth credentials')
username, _, password = decoded.partition(":")
try:
id = UUID(username)
except (ValueError) as exc:
raise AuthenticationError('Invalid basic auth credentials')
with SessionLocal() as db:
chargepoint = db.query(ChargePoint).filter(ChargePoint.id == id).first()
if chargepoint is None:
raise AuthenticationError('Invalid basic auth credentials')
if chargepoint.password != password:
raise AuthenticationError('Invalid basic auth credentials')
return AuthCredentials(["authenticated"]), chargepoint

View file

@ -0,0 +1,19 @@
from fastapi import WebSocket, WebSocketDisconnect
from websockets import ConnectionClosed
# Wrapper to transform a FastAPI websocket to a standard websocket
class WebSocketWrapper():
def __init__(self, websocket: WebSocket):
self._websocket = websocket
async def recv(self) -> str:
try:
return await self._websocket.receive_text()
except WebSocketDisconnect as e:
raise ConnectionClosed(e.code, 'WebSocketWrapper')
async def send(self, msg: str) -> None:
await self._websocket.send_text(msg)
async def close(self, code: int, reason: str) -> None:
await self._websocket.close(code)