|
| 1 | +import decouple |
| 2 | +import requests |
| 3 | +from rest_framework.views import APIView |
| 4 | + |
| 5 | +from utils.response import CustomResponse |
| 6 | + |
| 7 | + |
| 8 | +AUTH_DOMAIN = decouple.config("AUTH_DOMAIN") |
| 9 | + |
| 10 | + |
| 11 | +class GoogleMobileAuthProxyAPI(APIView): |
| 12 | + """ |
| 13 | + Proxy endpoint for Google mobile authentication. |
| 14 | + Forwards requests to the auth server and returns the response. |
| 15 | + """ |
| 16 | + |
| 17 | + def post(self, request): |
| 18 | + id_token = request.data.get("id_token") or request.data.get("idToken") |
| 19 | + |
| 20 | + if not id_token: |
| 21 | + return CustomResponse( |
| 22 | + general_message="ID token is required" |
| 23 | + ).get_failure_response() |
| 24 | + |
| 25 | + try: |
| 26 | + response = requests.post( |
| 27 | + f"{AUTH_DOMAIN}/api/v1/auth/google-mobile/", |
| 28 | + json={"id_token": id_token}, |
| 29 | + headers={"Content-Type": "application/json"}, |
| 30 | + timeout=30, |
| 31 | + ) |
| 32 | + |
| 33 | + data = response.json() |
| 34 | + |
| 35 | + if data.get("hasError"): |
| 36 | + return CustomResponse( |
| 37 | + general_message=data.get("message", {}).get("general", ["Authentication failed"])[0] |
| 38 | + ).get_failure_response() |
| 39 | + |
| 40 | + return CustomResponse( |
| 41 | + general_message="Access Granted", |
| 42 | + response=data.get("response"), |
| 43 | + ).get_success_response() |
| 44 | + |
| 45 | + except requests.exceptions.Timeout: |
| 46 | + return CustomResponse( |
| 47 | + general_message="Authentication server timeout" |
| 48 | + ).get_failure_response() |
| 49 | + except requests.exceptions.RequestException as e: |
| 50 | + return CustomResponse( |
| 51 | + general_message=f"Authentication server error: {str(e)}" |
| 52 | + ).get_failure_response() |
| 53 | + |
| 54 | + |
| 55 | +class AppleMobileAuthProxyAPI(APIView): |
| 56 | + """ |
| 57 | + Proxy endpoint for Apple mobile authentication. |
| 58 | + Forwards requests to the auth server and returns the response. |
| 59 | + """ |
| 60 | + |
| 61 | + def post(self, request): |
| 62 | + identity_token = request.data.get("identity_token") or request.data.get("identityToken") |
| 63 | + email = request.data.get("email") |
| 64 | + |
| 65 | + if not identity_token: |
| 66 | + return CustomResponse( |
| 67 | + general_message="Identity token is required" |
| 68 | + ).get_failure_response() |
| 69 | + |
| 70 | + try: |
| 71 | + payload = {"identity_token": identity_token} |
| 72 | + if email: |
| 73 | + payload["email"] = email |
| 74 | + |
| 75 | + response = requests.post( |
| 76 | + f"{AUTH_DOMAIN}/api/v1/auth/apple-mobile/", |
| 77 | + json=payload, |
| 78 | + headers={"Content-Type": "application/json"}, |
| 79 | + timeout=30, |
| 80 | + ) |
| 81 | + |
| 82 | + data = response.json() |
| 83 | + |
| 84 | + if data.get("hasError"): |
| 85 | + return CustomResponse( |
| 86 | + general_message=data.get("message", {}).get("general", ["Authentication failed"])[0] |
| 87 | + ).get_failure_response() |
| 88 | + |
| 89 | + return CustomResponse( |
| 90 | + general_message="Access Granted", |
| 91 | + response=data.get("response"), |
| 92 | + ).get_success_response() |
| 93 | + |
| 94 | + except requests.exceptions.Timeout: |
| 95 | + return CustomResponse( |
| 96 | + general_message="Authentication server timeout" |
| 97 | + ).get_failure_response() |
| 98 | + except requests.exceptions.RequestException as e: |
| 99 | + return CustomResponse( |
| 100 | + general_message=f"Authentication server error: {str(e)}" |
| 101 | + ).get_failure_response() |
| 102 | + |
| 103 | + |
| 104 | +class RefreshTokenProxyAPI(APIView): |
| 105 | + """ |
| 106 | + Proxy endpoint for token refresh. |
| 107 | + Forwards requests to the auth server and returns fresh tokens. |
| 108 | + """ |
| 109 | + |
| 110 | + def post(self, request): |
| 111 | + refresh_token = request.data.get("refreshToken") or request.data.get("refresh_token") |
| 112 | + |
| 113 | + if not refresh_token: |
| 114 | + return CustomResponse( |
| 115 | + general_message="Refresh token is required" |
| 116 | + ).get_failure_response() |
| 117 | + |
| 118 | + try: |
| 119 | + response = requests.post( |
| 120 | + f"{AUTH_DOMAIN}/api/v1/auth/get-access-token/", |
| 121 | + json={"refreshToken": refresh_token}, |
| 122 | + headers={"Content-Type": "application/json"}, |
| 123 | + timeout=30, |
| 124 | + ) |
| 125 | + |
| 126 | + data = response.json() |
| 127 | + |
| 128 | + if data.get("hasError"): |
| 129 | + return CustomResponse( |
| 130 | + general_message=data.get("message", {}).get("general", ["Token refresh failed"])[0] |
| 131 | + ).get_failure_response() |
| 132 | + |
| 133 | + return CustomResponse( |
| 134 | + response=data.get("response"), |
| 135 | + ).get_success_response() |
| 136 | + |
| 137 | + except requests.exceptions.Timeout: |
| 138 | + return CustomResponse( |
| 139 | + general_message="Authentication server timeout" |
| 140 | + ).get_failure_response() |
| 141 | + except requests.exceptions.RequestException as e: |
| 142 | + return CustomResponse( |
| 143 | + general_message=f"Authentication server error: {str(e)}" |
| 144 | + ).get_failure_response() |
0 commit comments