Corp/backend/api/auth/base.py

55 lines
1.6 KiB
Python

from __future__ import annotations
import logging
from django.conf import settings
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.views import APIView
from api.schema import UnstructuredResponseSerializer
from api.v1.employees.views._helpers import Employee
from employees.utils import _normalize_phone
from .services import (
SESSION_ID_CLAIM,
)
logger = logging.getLogger(__name__)
def _resolve_employee_by_login(login: str) -> Employee | None:
normalized_login = (login or "").strip()
if not normalized_login:
return None
if "@" in normalized_login:
return Employee.objects.filter(email__iexact=normalized_login).first()
normalized_phone = _normalize_phone(normalized_login)
if not normalized_phone:
return None
return Employee.objects.filter(phone_number=normalized_phone).first()
def _build_frontend_password_reset_link(*, uid: str, token: str) -> str:
site_url = getattr(settings, "SITE_URL", "https://corp.robotail.pro").rstrip("/")
return f"{site_url}/reset-password?uid={uid}&token={token}"
class AnonymousAPIView(APIView):
authentication_classes = []
throttle_scope = "auth_sensitive"
permission_classes = [AllowAny]
serializer_class = UnstructuredResponseSerializer
class SessionBaseAPIView(APIView):
permission_classes = [IsAuthenticated]
serializer_class = UnstructuredResponseSerializer
def get_current_session_id(self, request) -> str | None:
token = getattr(request, "auth", None)
if token is None:
return None
return token.get(SESSION_ID_CLAIM)