[WEB - 1438] dev: oauth exception handling (#4602)

* dev: oauth exception handling

* dev: reset password on deactivation
This commit is contained in:
Nikhil 2024-05-28 13:39:27 +05:30 committed by GitHub
parent ff03c0b718
commit 36b82a7776
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 61 additions and 23 deletions

View File

@ -1,5 +1,5 @@
# Python imports # Python imports
# import uuid import uuid
# Django imports # Django imports
from django.db.models import Case, Count, IntegerField, Q, When from django.db.models import Case, Count, IntegerField, Q, When
@ -183,8 +183,8 @@ class UserEndpoint(BaseViewSet):
profile.save() profile.save()
# Reset password # Reset password
# user.is_password_autoset = True user.is_password_autoset = True
# user.set_password(uuid.uuid4().hex) user.set_password(uuid.uuid4().hex)
# Deactivate the user # Deactivate the user
user.is_active = False user.is_active = False

View File

@ -8,6 +8,10 @@ from django.utils import timezone
from plane.db.models import Account from plane.db.models import Account
from .base import Adapter from .base import Adapter
from plane.authentication.adapter.error import (
AuthenticationException,
AUTHENTICATION_ERROR_CODES,
)
class OauthAdapter(Adapter): class OauthAdapter(Adapter):
@ -50,20 +54,42 @@ class OauthAdapter(Adapter):
return self.complete_login_or_signup() return self.complete_login_or_signup()
def get_user_token(self, data, headers=None): def get_user_token(self, data, headers=None):
headers = headers or {} try:
response = requests.post( headers = headers or {}
self.get_token_url(), data=data, headers=headers response = requests.post(
) self.get_token_url(), data=data, headers=headers
response.raise_for_status() )
return response.json() response.raise_for_status()
return response.json()
except requests.RequestException:
code = (
"GOOGLE_OAUTH_PROVIDER_ERROR"
if self.provider == "google"
else "GITHUB_OAUTH_PROVIDER_ERROR"
)
raise AuthenticationException(
error_code=AUTHENTICATION_ERROR_CODES[code],
error_message=str(code),
)
def get_user_response(self): def get_user_response(self):
headers = { try:
"Authorization": f"Bearer {self.token_data.get('access_token')}" headers = {
} "Authorization": f"Bearer {self.token_data.get('access_token')}"
response = requests.get(self.get_user_info_url(), headers=headers) }
response.raise_for_status() response = requests.get(self.get_user_info_url(), headers=headers)
return response.json() response.raise_for_status()
return response.json()
except requests.RequestException:
code = (
"GOOGLE_OAUTH_PROVIDER_ERROR"
if self.provider == "google"
else "GITHUB_OAUTH_PROVIDER_ERROR"
)
raise AuthenticationException(
error_code=AUTHENTICATION_ERROR_CODES[code],
error_message=str(code),
)
def set_user_data(self, data): def set_user_data(self, data):
self.user_data = data self.user_data = data

View File

@ -105,14 +105,26 @@ class GitHubOAuthProvider(OauthAdapter):
) )
def __get_email(self, headers): def __get_email(self, headers):
# Github does not provide email in user response try:
emails_url = "https://api.github.com/user/emails" # Github does not provide email in user response
emails_response = requests.get(emails_url, headers=headers).json() emails_url = "https://api.github.com/user/emails"
email = next( emails_response = requests.get(emails_url, headers=headers).json()
(email["email"] for email in emails_response if email["primary"]), email = next(
None, (
) email["email"]
return email for email in emails_response
if email["primary"]
),
None,
)
return email
except requests.RequestException:
raise AuthenticationException(
error_code=AUTHENTICATION_ERROR_CODES[
"GITHUB_OAUTH_PROVIDER_ERROR"
],
error_message="GITHUB_OAUTH_PROVIDER_ERROR",
)
def set_user_data(self): def set_user_data(self):
user_info_response = self.get_user_response() user_info_response = self.get_user_response()