feat: add cmshopee authorization shadow routes
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import logging
|
||||
|
||||
from django.http import HttpResponse
|
||||
from django.conf import settings
|
||||
from django.utils import timezone
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
@@ -70,6 +71,7 @@ from apps.licensing.services import (
|
||||
DeviceRegistrationError,
|
||||
DeviceSessionValidationError,
|
||||
create_migration_request,
|
||||
evaluate_device_authorization,
|
||||
record_device_heartbeat,
|
||||
register_device,
|
||||
resolve_optional_device_session,
|
||||
@@ -77,6 +79,7 @@ from apps.licensing.services import (
|
||||
from apps.licensing.models import MigrationRequest
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
authorization_logger = logging.getLogger("cmhub.licensing.authorization")
|
||||
|
||||
|
||||
class ExternalApiView(APIView):
|
||||
@@ -379,6 +382,56 @@ class GenerateImageTaskDetailView(ExternalApiView):
|
||||
return Response(task_detail_response(task), status=status.HTTP_200_OK)
|
||||
|
||||
|
||||
class CmshopeeShadowAuthorizationMixin:
|
||||
product_code = "cmshopee"
|
||||
|
||||
def optional_client_device(self, request):
|
||||
device = super().optional_client_device(request)
|
||||
decision = evaluate_device_authorization(
|
||||
user=request.user,
|
||||
product_code=self.product_code,
|
||||
device=device,
|
||||
raw_credential_token=request.headers.get("X-Device-Credential", ""),
|
||||
)
|
||||
event = {
|
||||
"event": "cmshopee_authorization_shadow",
|
||||
"product_code": self.product_code,
|
||||
"user_id": request.user.id,
|
||||
"client_device_id": getattr(device, "id", None),
|
||||
"credential_id": decision.credential_id,
|
||||
"would_reject": decision.would_reject,
|
||||
"would_reject_code": decision.code,
|
||||
"shadow_mode": settings.CMSHOPEE_AUTHORIZATION_SHADOW_MODE,
|
||||
}
|
||||
authorization_logger.info(
|
||||
"%s %s",
|
||||
event["event"],
|
||||
event,
|
||||
extra={"cmshopee_authorization": event},
|
||||
)
|
||||
request.cmshopee_authorization = decision
|
||||
return device
|
||||
|
||||
|
||||
class CmshopeeGenerateTitleView(CmshopeeShadowAuthorizationMixin, GenerateTitleView):
|
||||
pass
|
||||
|
||||
|
||||
class CmshopeeAnalyzeImagesView(CmshopeeShadowAuthorizationMixin, AnalyzeImagesView):
|
||||
pass
|
||||
|
||||
|
||||
class CmshopeeGenerateImageTaskSubmitView(
|
||||
CmshopeeShadowAuthorizationMixin,
|
||||
GenerateImageTaskSubmitView,
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
class CmshopeeGenerateImageTaskDetailView(GenerateImageTaskDetailView):
|
||||
pass
|
||||
|
||||
|
||||
class BalanceView(ExternalApiView):
|
||||
def get(self, request):
|
||||
balance = get_balance_snapshot(request.user)
|
||||
|
||||
Reference in New Issue
Block a user