[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
# import uuid
import uuid
# Django imports
from django.db.models import Case, Count, IntegerField, Q, When
@ -183,8 +183,8 @@ class UserEndpoint(BaseViewSet):
profile.save()
# Reset password
# user.is_password_autoset = True
# user.set_password(uuid.uuid4().hex)
user.is_password_autoset = True
user.set_password(uuid.uuid4().hex)
# Deactivate the user
user.is_active = False

View File

@ -8,6 +8,10 @@ from django.utils import timezone
from plane.db.models import Account
from .base import Adapter
from plane.authentication.adapter.error import (
AuthenticationException,
AUTHENTICATION_ERROR_CODES,
)
class OauthAdapter(Adapter):
@ -50,20 +54,42 @@ class OauthAdapter(Adapter):
return self.complete_login_or_signup()
def get_user_token(self, data, headers=None):
try:
headers = headers or {}
response = requests.post(
self.get_token_url(), data=data, headers=headers
)
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):
try:
headers = {
"Authorization": f"Bearer {self.token_data.get('access_token')}"
}
response = requests.get(self.get_user_info_url(), headers=headers)
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):
self.user_data = data

View File

@ -105,14 +105,26 @@ class GitHubOAuthProvider(OauthAdapter):
)
def __get_email(self, headers):
try:
# Github does not provide email in user response
emails_url = "https://api.github.com/user/emails"
emails_response = requests.get(emails_url, headers=headers).json()
email = next(
(email["email"] for email in emails_response if email["primary"]),
(
email["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):
user_info_response = self.get_user_response()