initial version of osb

This commit is contained in:
Tobias Brunner 2025-01-27 17:42:40 +01:00
parent 7143234e22
commit 022f0ad60f
No known key found for this signature in database
14 changed files with 281 additions and 22 deletions

View file

@ -0,0 +1,34 @@
from django.conf import settings
from django.contrib.auth.models import User
from rest_framework import authentication
from rest_framework import exceptions
class ServiceBrokerAuthentication(authentication.BaseAuthentication):
def authenticate(self, request):
auth = request.META.get("HTTP_AUTHORIZATION")
if not auth:
return None
try:
import base64
auth_type, auth_string = auth.split(" ")
if auth_type.lower() != "basic":
return None
decoded = base64.b64decode(auth_string).decode("utf-8")
username, password = decoded.split(":")
if (
username == settings.BROKER_USERNAME
and password == settings.BROKER_PASSWORD
):
# Use a dummy user for authentication
user = User(username=username, is_staff=True)
return (user, None)
except Exception as e:
raise exceptions.AuthenticationFailed("Invalid credentials")
raise exceptions.AuthenticationFailed("Invalid credentials")