forked from github/plane
[WEB - 1438] dev: oauth exception handling (#4602)
* dev: oauth exception handling * dev: reset password on deactivation
This commit is contained in:
parent
ff03c0b718
commit
36b82a7776
@ -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
|
||||||
|
@ -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
|
||||||
|
@ -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()
|
||||||
|
Loading…
Reference in New Issue
Block a user