feat(users): add google oauth login flow
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.contrib.auth import get_user_model
|
||||
from django_filters.rest_framework import DjangoFilterBackend
|
||||
from drf_spectacular.utils import extend_schema, inline_serializer
|
||||
@@ -19,6 +20,9 @@ from apps.users.api.serializers import (
|
||||
ChangePasswordSerializer,
|
||||
LoginOtpSerializer,
|
||||
LoginSerializer,
|
||||
GoogleOAuthClaimVerifySerializer,
|
||||
GoogleOAuthCompleteSerializer,
|
||||
GoogleOAuthFlowSerializer,
|
||||
RegisterSerializer,
|
||||
ResetPasswordSerializer,
|
||||
SendOTPSerializer,
|
||||
@@ -35,6 +39,9 @@ from apps.users.api.throttles import (
|
||||
OTPSendBurstThrottle,
|
||||
OTPSendSustainedThrottle,
|
||||
PasswordLoginThrottle,
|
||||
GoogleClaimSendBurstThrottle,
|
||||
GoogleClaimSendSustainedThrottle,
|
||||
GoogleClaimVerifyThrottle,
|
||||
)
|
||||
from apps.users.services.auth import (
|
||||
register_user_with_password,
|
||||
@@ -46,6 +53,20 @@ from apps.users.services.auth import (
|
||||
change_password,
|
||||
logout_user
|
||||
)
|
||||
from apps.users.services.google_oauth import (
|
||||
build_authenticated_flow_payload,
|
||||
build_google_authorization_url,
|
||||
build_google_callback_redirect_url,
|
||||
build_pending_google_flow_payload,
|
||||
complete_google_signup,
|
||||
consume_google_state,
|
||||
create_google_flow,
|
||||
exchange_code_for_google_profile,
|
||||
find_social_account_for_profile,
|
||||
get_google_flow,
|
||||
send_google_claim_otp,
|
||||
verify_google_claim,
|
||||
)
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
@@ -146,6 +167,88 @@ class LoginOTPView(APIView):
|
||||
return Response(tokens, status=status.HTTP_200_OK)
|
||||
|
||||
|
||||
class GoogleOAuthStartView(APIView):
|
||||
permission_classes = (AllowAny,)
|
||||
|
||||
@extend_schema(responses=None)
|
||||
def get(self, request):
|
||||
return HttpResponseRedirect(build_google_authorization_url())
|
||||
|
||||
|
||||
class GoogleOAuthCallbackView(APIView):
|
||||
permission_classes = (AllowAny,)
|
||||
|
||||
@extend_schema(responses=None)
|
||||
def get(self, request):
|
||||
if request.query_params.get("error"):
|
||||
raise serializers.ValidationError(
|
||||
{"detail": request.query_params.get("error_description") or "Google sign-in was cancelled."}
|
||||
)
|
||||
|
||||
consume_google_state(request.query_params.get("state"))
|
||||
profile = exchange_code_for_google_profile(request.query_params.get("code"))
|
||||
social_account = find_social_account_for_profile(profile)
|
||||
|
||||
if social_account:
|
||||
flow_payload = build_authenticated_flow_payload(social_account.user)
|
||||
else:
|
||||
flow_payload = build_pending_google_flow_payload(profile)
|
||||
|
||||
flow = create_google_flow(flow_payload)
|
||||
return HttpResponseRedirect(build_google_callback_redirect_url(flow))
|
||||
|
||||
|
||||
class GoogleOAuthFlowView(APIView):
|
||||
permission_classes = (AllowAny,)
|
||||
|
||||
@extend_schema(responses=None)
|
||||
def get(self, request):
|
||||
serializer = GoogleOAuthFlowSerializer(data=request.query_params)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
return Response(get_google_flow(serializer.validated_data["flow"]), status=status.HTTP_200_OK)
|
||||
|
||||
|
||||
class GoogleOAuthCompleteView(APIView):
|
||||
permission_classes = (AllowAny,)
|
||||
|
||||
@extend_schema(request=GoogleOAuthCompleteSerializer, responses=None)
|
||||
def post(self, request):
|
||||
serializer = GoogleOAuthCompleteSerializer(data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
payload = complete_google_signup(
|
||||
flow=serializer.validated_data["flow"],
|
||||
mobile=serializer.validated_data["mobile"],
|
||||
)
|
||||
return Response(payload, status=status.HTTP_200_OK)
|
||||
|
||||
|
||||
class GoogleOAuthClaimSendOtpView(APIView):
|
||||
permission_classes = (AllowAny,)
|
||||
throttle_classes = [GoogleClaimSendBurstThrottle, GoogleClaimSendSustainedThrottle]
|
||||
|
||||
@extend_schema(request=GoogleOAuthFlowSerializer, responses=None)
|
||||
def post(self, request):
|
||||
serializer = GoogleOAuthFlowSerializer(data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
payload = send_google_claim_otp(serializer.validated_data["flow"])
|
||||
return Response(payload, status=status.HTTP_200_OK)
|
||||
|
||||
|
||||
class GoogleOAuthClaimVerifyView(APIView):
|
||||
permission_classes = (AllowAny,)
|
||||
throttle_classes = [GoogleClaimVerifyThrottle]
|
||||
|
||||
@extend_schema(request=GoogleOAuthClaimVerifySerializer, responses=None)
|
||||
def post(self, request):
|
||||
serializer = GoogleOAuthClaimVerifySerializer(data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
payload = verify_google_claim(
|
||||
flow=serializer.validated_data["flow"],
|
||||
code=serializer.validated_data["code"],
|
||||
)
|
||||
return Response(payload, status=status.HTTP_200_OK)
|
||||
|
||||
|
||||
class ResetPasswordView(APIView):
|
||||
permission_classes = (AllowAny,)
|
||||
serializer_class = ResetPasswordSerializer
|
||||
|
||||
Reference in New Issue
Block a user