Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions apiserver/plane/app/views/user/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Python imports
# import uuid
import uuid

# Django imports
from django.db.models import Case, Count, IntegerField, Q, When
Expand Down Expand Up @@ -183,8 +183,8 @@
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)

Check warning on line 187 in apiserver/plane/app/views/user/base.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

apiserver/plane/app/views/user/base.py#L187

The password on 'user' is being set without validating the password.

# Deactivate the user
user.is_active = False
Expand Down
50 changes: 38 additions & 12 deletions apiserver/plane/authentication/adapter/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
from plane.db.models import Account

from .base import Adapter
from plane.authentication.adapter.error import (
AuthenticationException,
AUTHENTICATION_ERROR_CODES,
)


class OauthAdapter(Adapter):
Expand Down Expand Up @@ -50,20 +54,42 @@ def authenticate(self):
return self.complete_login_or_signup()

def get_user_token(self, data, headers=None):
headers = headers or {}
response = requests.post(
self.get_token_url(), data=data, headers=headers
)
response.raise_for_status()
return response.json()
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):
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()
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
Expand Down
28 changes: 20 additions & 8 deletions apiserver/plane/authentication/provider/oauth/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,26 @@ def set_token_data(self):
)

def __get_email(self, headers):
# Github does not provide email in user response
emails_url = "https://github.com/gitapi/user/emails"
emails_response = requests.get(emails_url, headers=headers).json()
email = next(
(email["email"] for email in emails_response if email["primary"]),
None,
)
return email
try:
# Github does not provide email in user response
emails_url = "https://github.com/gitapi/user/emails"
emails_response = requests.get(emails_url, headers=headers).json()
email = next(
(
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()
Expand Down
Loading