Skip to content
This repository was archived by the owner on Oct 1, 2020. It is now read-only.
Open
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
24 changes: 24 additions & 0 deletions apps/ksso/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from django.contrib.auth.models import User

from rest_framework import serializers

from apps.ksso.models import PortalInfo


class PortalInfoSerializer(serializers.ModelSerializer):
class Meta:
model = PortalInfo
fields = '__all__'


class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = (
'id',
'username',

'portal_info',
)

portal_info = PortalInfoSerializer()
4 changes: 3 additions & 1 deletion apps/ksso/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

from django.conf.urls import url

from .views import AgreeView, DisagreeView, LoginView, LogoutView, SignUpView
from .views import AgreeView, DisagreeView, LoginView, LogoutView, SignUpView, UserInfoView

urlpatterns = [
url(r'^login/$', LoginView.as_view(), name='login'),
url(r'^logout/$', LogoutView.as_view(), name='logout'),
url(r'^signup/$', SignUpView.as_view(), name='signup'),
url(r'^signup/agree/$', AgreeView.as_view()),
url(r'^signup/disagree/$', DisagreeView.as_view()),

url(r'^info/$', UserInfoView.as_view()),
]
39 changes: 38 additions & 1 deletion apps/ksso/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@
KAIST 단일인증서비스 뷰.
"""

from django.contrib.auth import login, logout
from django.conf import settings as project_settings
from django.contrib.auth import login, logout, load_backend, SESSION_KEY, BACKEND_SESSION_KEY
from django.contrib.auth.views import redirect_to_login
from django.contrib.sessions.models import Session
from django.shortcuts import redirect
from django.views.generic import TemplateView, View

from rest_framework import views, status, response

from apps.manager.views import PageView

from . import settings
from .classes import PortalController
from .serializers import UserSerializer


class LoginView(TemplateView):
Expand Down Expand Up @@ -166,3 +171,35 @@ def dispatch(self, request, *args, **kwargs):
except:
pass
return redirect('main')


class UserInfoView(views.APIView):
def get(self, request, format=None):
if request.META['REMOTE_ADDR'] not in project_settings.ALLOWED_REMOTE_ADDRS:
return response.Response(status=status.HTTP_400_BAD_REQUEST)

session_key = request.GET.get('session_key')

if not session_key:
return response.Response(status=status.HTTP_400_BAD_REQUEST)

try:
session = Session.objects.get(session_key=session_key)

try:
user_id = session.get_decoded()[SESSION_KEY]
backend_path = session.get_decoded()[BACKEND_SESSION_KEY]

except KeyError:
pass

else:
if backend_path in project_settings.AUTHENTICATION_BACKENDS:
backend = load_backend(backend_path)

return response.Response(UserSerializer(backend.get_user(user_id)).data)

except Session.DoesNotExist:
pass

return response.Response(status=status.HTTP_404_NOT_FOUND)
6 changes: 6 additions & 0 deletions kaistusc/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@
}
}

# UserInfoView ALLOWED REMOTE_ADDRS

ALLOWED_REMOTE_ADDRS = (
'127.0.0.1',
)

try:
from .local_settings import *
except ImportError:
Expand Down