Skip to content

The platform for continuously improving AI agents.

8.2 KB
Raw Blame
import hashlib
import uuid

from django.db import transaction
from drf_spectacular.utils import OpenApiResponse, extend_schema, extend_schema_field
from rest_framework import serializers, status
from rest_framework.exceptions import APIException, PermissionDenied
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from rest_framework.throttling import SimpleRateThrottle
from rest_framework.views import APIView
from rest_framework_simplejwt.tokens import RefreshToken

from overbae.api.serializers import UserOnboardingSerializer
from overbae.auth import clerk_enabled
from overbae.models import SignOnMethod, User, UserOnboarding
from overbae.services.feedback import set_agent_feedback


class ClerkRequired(PermissionDenied):
    default_detail = "Local sign-in is only available when Clerk is disabled."
    default_code = "clerk_required"


class InvalidCredentials(APIException):
    # Not AuthenticationFailed: with authentication_classes=[] DRF rewrites that to 403.
    status_code = status.HTTP_401_UNAUTHORIZED
    default_detail = "Invalid email or password."
    default_code = "authentication_failed"


class LocalSessionThrottle(SimpleRateThrottle):
    # Each attempt checks a password: the limit is what stops guessing one. It counts per
    # address tried, which a client cannot vary the way it can a forwarded-for header.
    rate = "10/minute"
    scope = "local_session"

    def get_cache_key(self, request, view):
        email = str(request.data.get("email") or "").strip().lower()
        ident = hashlib.sha256(email.encode()).hexdigest() if email else self.get_ident(request)
        return self.cache_format % {"scope": self.scope, "ident": ident}


class LocalSessionSerializer(serializers.Serializer):
    email = serializers.EmailField(max_length=254)
    password = serializers.CharField(write_only=True, min_length=8, max_length=128)

    def validate_email(self, value):
        return value.strip().lower()


class UserMeSerializer(serializers.ModelSerializer):
    has_completed_onboarding = serializers.SerializerMethodField()
    billing_enabled = serializers.SerializerMethodField()
    credits_usd = serializers.SerializerMethodField()

    class Meta:
        model = User
        fields = [
            "id",
            "clerk_user_id",
            "email",
            "email_verified",
            "has_completed_onboarding",
            "is_guest",
            "billing_enabled",
            "credits_usd",
            "agent_feedback",
        ]
        extra_kwargs = {
            "is_guest": {"read_only": True},
            "agent_feedback": {"read_only": True},
            # Opaque Clerk id — same distinct_id the Console uses in PostHog.
            "clerk_user_id": {"read_only": True},
        }

    def get_has_completed_onboarding(self, user: User) -> bool:
        onboarding = getattr(user, "onboarding", None)
        if onboarding and onboarding.status == "completed":
            return True
        # The wizard sets "in_progress" on mount and creates a project in step
        # one, so the membership shortcut below would flag it complete mid-wizard.
        if onboarding and onboarding.status == "in_progress":
            return False
        # Membership (invited, provisioned, seeded) IS the completed state —
        # otherwise an account that can already see workspaces is sent through
        # the wizard.
        return user.project_memberships.exists()

    def get_billing_enabled(self, user: User) -> bool:
        from overbae.services.billing_provider import get_billing

        return get_billing().enabled

    @extend_schema_field(serializers.DecimalField(max_digits=12, decimal_places=4))
    def get_credits_usd(self, user: User):
        from overbae.services.billing_ledger import balance_usd

        return balance_usd(user)


class AuthTokensResponseSerializer(serializers.Serializer):
    access = serializers.CharField()
    refresh = serializers.CharField()
    user = UserMeSerializer()


def tokens_for(user: User) -> dict:
    refresh = RefreshToken.for_user(user)
    return AuthTokensResponseSerializer(
        {
            "access": str(refresh.access_token),
            "refresh": str(refresh),
            "user": user,
        }
    ).data


def local_session(*, email: str, password: str) -> tuple[User, bool]:
    """Sign in an existing password account, or create one on first use.

    Returns ``(user, created)``. Refuses when Clerk is configured so the hosted
    product cannot be bypassed through this path.
    """
    if clerk_enabled():
        raise ClerkRequired()

    # Lazy import: overbae.models is not ready when some callers load this module.
    from overbae.services.project_invites import claim_pending_invites

    user = User.objects.filter(email__iexact=email).first()
    if user is not None:
        if user.is_guest or not user.is_active or not user.has_usable_password():
            raise InvalidCredentials()
        if not user.check_password(password):
            raise InvalidCredentials()
        return user, False

    with transaction.atomic():
        user = User.objects.create_user(
            email=email,
            password=password,
            email_verified=True,
            is_active=True,
            sign_on_method=SignOnMethod.PASSWORD,
            clerk_user_id=f"local_{uuid.uuid4().hex}",
        )
    claim_pending_invites(user)
    return user, True


@extend_schema(
    summary="Local sign-in",
    description=(
        "Email and password session for self-hosted deployments without Clerk. "
        "Creates the account on first use; no email verification."
    ),
    request=LocalSessionSerializer,
    responses={
        200: OpenApiResponse(
            response=AuthTokensResponseSerializer, description="JWT pair and user profile"
        ),
        201: OpenApiResponse(
            response=AuthTokensResponseSerializer, description="Account created and signed in"
        ),
    },
)
class LocalSessionView(APIView):
    authentication_classes: list = []
    permission_classes = [AllowAny]
    throttle_classes = [LocalSessionThrottle]

    def post(self, request):
        serializer = LocalSessionSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user, created = local_session(
            email=serializer.validated_data["email"],
            password=serializer.validated_data["password"],
        )
        return Response(
            tokens_for(user),
            status=status.HTTP_201_CREATED if created else status.HTTP_200_OK,
        )


class UserPreferencesSerializer(serializers.Serializer):
    agent_feedback = serializers.BooleanField()


class UserMeView(APIView):
    @extend_schema(
        summary="Current user profile",
        description="Returns the authenticated user's profile.",
        responses={200: UserMeSerializer},
    )
    def get(self, request):
        return Response(UserMeSerializer(request.user).data)

    @extend_schema(
        summary="Update account preferences",
        description="Records whether the user's coding agents may send friction reports.",
        request=UserPreferencesSerializer,
        responses={200: UserMeSerializer},
    )
    def patch(self, request):
        if request.user.is_guest:
            raise PermissionDenied("Sign up to choose account preferences.")
        serializer = UserPreferencesSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        set_agent_feedback(request.user, serializer.validated_data["agent_feedback"])
        return Response(UserMeSerializer(request.user).data)


@extend_schema(
    summary="User onboarding",
    description="Retrieve or update the authenticated user's onboarding record.",
    request=UserOnboardingSerializer,
    responses={200: UserOnboardingSerializer},
)
class OnboardingView(APIView):
    def get(self, request) -> Response:
        onboarding, _ = UserOnboarding.objects.get_or_create(user=request.user)
        return Response(UserOnboardingSerializer(onboarding).data)

    def patch(self, request) -> Response:
        onboarding, _ = UserOnboarding.objects.get_or_create(user=request.user)
        serializer = UserOnboardingSerializer(onboarding, data=request.data, partial=True)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data)