diff --git a/apps/core/management/scripts/portal_crawler.py b/apps/core/management/scripts/portal_crawler.py index 3c565294..349a2643 100644 --- a/apps/core/management/scripts/portal_crawler.py +++ b/apps/core/management/scripts/portal_crawler.py @@ -9,7 +9,6 @@ from django.db import transaction from django.utils import timezone from django.utils.translation import gettext -from fake_useragent import UserAgent from tqdm import tqdm from apps.core.models import Article @@ -37,15 +36,8 @@ def _login_kaist_portal(): session = requests.Session() - user_agent = UserAgent() - login_req1 = session.post('https://portalsso.kaist.ac.kr/ssoProcess2.ps', data=LOGIN_INFO_SSO2, - headers={ - 'User-Agent': user_agent.random, - }) - login_req2 = session.post('https://portalsso.kaist.ac.kr/ssoProcess.ps', data=LOGIN_INFO_SSO, - headers={ - 'User-Agent': user_agent.random, - }) + login_req1 = session.post('https://portalsso.kaist.ac.kr/ssoProcess2.ps', data=LOGIN_INFO_SSO2,) + login_req2 = session.post('https://portalsso.kaist.ac.kr/ssoProcess.ps', data=LOGIN_INFO_SSO,) print(f'sso2: {login_req1.status_code} & sso: {login_req2.status_code}') diff --git a/apps/core/templates/invalid_sso_login.html b/apps/core/templates/invalid_sso_login.html new file mode 100644 index 00000000..b5a7d8c6 --- /dev/null +++ b/apps/core/templates/invalid_sso_login.html @@ -0,0 +1,62 @@ + + + + + + + + + + + Oops! + + + +
+
+
+ +
+ +
+
+ + + + diff --git a/apps/core/urls.py b/apps/core/urls.py index 750c020c..f76db251 100644 --- a/apps/core/urls.py +++ b/apps/core/urls.py @@ -1,9 +1,11 @@ from django.urls import path, include -from apps.core.views import HomeView, StatusView, router +from apps.core.views import HomeView, InvalidSsoLoginView, StatusView, router urlpatterns = [ path('api/', include(router.urls)), path('api/home/', view=HomeView.as_view(), name='HomeView'), path('api/status/', view=StatusView.as_view(), name='StatusView'), + + path('api/invalid_sso_login/', InvalidSsoLoginView.as_view(), name='InvalidSsoLoginView'), ] diff --git a/apps/core/views/__init__.py b/apps/core/views/__init__.py index c2f0729a..e3ee3260 100644 --- a/apps/core/views/__init__.py +++ b/apps/core/views/__init__.py @@ -1,3 +1,4 @@ from .home import * from .status import * from .router import * +from .invalid_sso_login import InvalidSsoLoginView diff --git a/apps/core/views/invalid_sso_login.py b/apps/core/views/invalid_sso_login.py new file mode 100644 index 00000000..a1bbdbdb --- /dev/null +++ b/apps/core/views/invalid_sso_login.py @@ -0,0 +1,20 @@ +from django.views.generic import TemplateView + + +class InvalidSsoLoginView(TemplateView): + template_name = 'invalid_sso_login.html' + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + + # TODO: code 와 status_code 에 따라 다른 해결 방법을 제시할 수 있으면 좋겠습니다. + # INVALID_METHOD, INVALID_CODE, TOKEN_SERVICE_MISMATCH, + # TOKEN_EXPIRED, INVALID_SERVICE, INALID_TIMESTAMP, INVALID_SIGN + # 등의 code 가 있을 수 있습니다. + # https://github.com/sparcs-kaist/sparcssso/blob/master/apps/api/views/v2.py + context.update({ + 'code': self.request.GET.get('code', ''), + 'status_code': self.request.GET.get('status_code', ''), + }) + + return context diff --git a/apps/core/views/viewsets/report.py b/apps/core/views/viewsets/report.py index 6ef2c2e4..963c6206 100644 --- a/apps/core/views/viewsets/report.py +++ b/apps/core/views/viewsets/report.py @@ -65,7 +65,7 @@ def create(self, request, *args, **kwargs): if article_id: parent_id = article_id article = Article.objects.get(id=parent_id) - title = f"[신고 (게시글)] '{request.user.id}: {request.user.profile}'님께서 Article {parent_id}을 신고하였습니다." + title = f"[신고 (게시글)] '{request.user.id}:: {request.user.profile}'님께서 Article {parent_id}을 신고하였습니다." message =\ f'''게시글 {parent_id}에 대하여 다음과 같은 신고가 접수되었습니다: 신고자: {request.user.id}:: {request.user.profile} @@ -81,7 +81,7 @@ def create(self, request, *args, **kwargs): parent_id = request.data.get('parent_comment') comment = Comment.objects.get(id=parent_id) article = comment.get_parent_article() - title = f"[신고 (댓글)] '{request.user.profile}'님께서 Comment {parent_id}을 신고하였습니다." + title = f"[신고 (댓글)] '{request.user.id}:: {request.user.profile}'님께서 Comment {parent_id}을 신고하였습니다." message =\ f'''댓글 {parent_id}에 대하여 다음과 같은 신고가 접수되었습니다: 신고자: {request.user.id}:: {request.user.profile} diff --git a/apps/user/admin.py b/apps/user/admin.py index cada888a..c8f45cf0 100644 --- a/apps/user/admin.py +++ b/apps/user/admin.py @@ -24,7 +24,7 @@ class UserProfileAdmin(MetaDataModelAdmin): 'uid', 'sid', 'nickname', - 'user', + 'user__id', ) diff --git a/apps/user/migrations/0015_fix_inactive_due_at_verbose_name.py b/apps/user/migrations/0015_fix_inactive_due_at_verbose_name.py new file mode 100644 index 00000000..c49c5611 --- /dev/null +++ b/apps/user/migrations/0015_fix_inactive_due_at_verbose_name.py @@ -0,0 +1,18 @@ +# Generated by Django 3.1 on 2021-02-25 12:23 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('user', '0014_userprofile_inactive_due_at'), + ] + + operations = [ + migrations.AlterField( + model_name='userprofile', + name='inactive_due_at', + field=models.DateTimeField(default=None, null=True, verbose_name='활동정지 마감 일시'), + ), + ] diff --git a/apps/user/models/user_profile.py b/apps/user/models/user_profile.py index 2be2ea0e..ebdb5eea 100644 --- a/apps/user/models/user_profile.py +++ b/apps/user/models/user_profile.py @@ -109,7 +109,7 @@ class UserGroup(models.IntegerChoices): inactive_due_at = models.DateTimeField( null=True, default=None, - verbose_name='약관 동의 일시', + verbose_name='활동정지 마감 일시', ) def __str__(self): diff --git a/apps/user/views/viewsets/user.py b/apps/user/views/viewsets/user.py index 7a82f93f..7bab0ac4 100644 --- a/apps/user/views/viewsets/user.py +++ b/apps/user/views/viewsets/user.py @@ -8,7 +8,7 @@ from django.conf import settings from django.contrib.auth import get_user_model, login, logout from django.db import transaction -from django.shortcuts import redirect +from django.shortcuts import redirect, reverse from django.utils import timezone from rest_framework import status, response, decorators, permissions @@ -99,18 +99,13 @@ def sso_login_callback(self, request, *args, **kwargs): user_info = self.sso_client.get_user_info(request.GET['code']) except requests.exceptions.HTTPError as http_error: - if http_error.response.status_code == 400: - message = '잘못된 요청입니다.' + try: + code = json.loads(http_error.response.content)['code'] - elif http_error.response.status_code == 403: - message = '권한이 부족합니다.' + except: + code = "json-loads-error" - else: - message = '알 수 없는 에러가 발생했습니다. 잠시 뒤에 다시 시도해주세요.' - - return response.Response( - data={'message': message}, status=http_error.response.status_code, - ) + return redirect(to=reverse('core:InvalidSsoLoginView') + f'?code={code}&status_code={http_error.response.status_code}') # Bypass SSO validation # if not request.GET.get('state'): diff --git a/ara/classes/middleware.py b/ara/classes/middleware.py index d81c600b..88382274 100644 --- a/ara/classes/middleware.py +++ b/ara/classes/middleware.py @@ -6,7 +6,8 @@ class CheckTermsOfServiceMiddleware: 'me', 'user-sso-login', 'user-sso-login-callback', - 'userprofile-agree-terms-of-service' + 'userprofile-agree-terms-of-service', + 'InvalidSsoLoginView', ] def __init__(self, get_response): diff --git a/ara/classes/sparcssso.py b/ara/classes/sparcssso.py index 9f014ad5..9a52eb9e 100644 --- a/ara/classes/sparcssso.py +++ b/ara/classes/sparcssso.py @@ -73,7 +73,8 @@ def _post_data(self, url, data): r.raise_for_status() except requests.exceptions.HTTPError as http_error: - capture_exception(http_error) + if int(http_error.response.status_code) >= 500: + capture_exception(http_error) raise http_error