From 17f18209559f8e1236e10458a18f56f436a784d8 Mon Sep 17 00:00:00 2001 From: virusdefender <1670873886@qq.com> Date: Tue, 20 Oct 2015 20:09:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E8=AE=A8=E8=AE=BA?= =?UTF-8?q?=E5=8C=BA=E7=9A=84=20SSO=20=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- account/serializers.py | 6 +++++- account/views.py | 34 ++++++++++++++++++++++---------- oj/local_settings.py | 15 ++++++-------- oj/server_settings.py | 2 ++ oj/urls.py | 4 ++-- template/src/oj/account/sso.html | 12 ++++------- 6 files changed, 43 insertions(+), 30 deletions(-) diff --git a/account/serializers.py b/account/serializers.py index 13f3e24b..5004ee1d 100644 --- a/account/serializers.py +++ b/account/serializers.py @@ -57,4 +57,8 @@ class ApplyResetPasswordSerializer(serializers.Serializer): class ResetPasswordSerializer(serializers.Serializer): token = serializers.CharField(min_length=1, max_length=40) password = serializers.CharField(min_length=6, max_length=30) - captcha = serializers.CharField(max_length=4, min_length=4) \ No newline at end of file + captcha = serializers.CharField(max_length=4, min_length=4) + + +class SSOSerializer(serializers.Serializer): + token = serializers.CharField(max_length=40) \ No newline at end of file diff --git a/account/views.py b/account/views.py index b50e0a3a..5a43e9cb 100644 --- a/account/views.py +++ b/account/views.py @@ -5,6 +5,7 @@ from django.contrib import auth from django.shortcuts import render from django.db.models import Q from django.conf import settings +from django.http import HttpResponseRedirect from django.core.exceptions import MultipleObjectsReturned from django.utils.timezone import now @@ -20,7 +21,8 @@ from .models import User from .serializers import (UserLoginSerializer, UsernameCheckSerializer, UserRegisterSerializer, UserChangePasswordSerializer, EmailCheckSerializer, UserSerializer, EditUserSerializer, - ApplyResetPasswordSerializer, ResetPasswordSerializer) + ApplyResetPasswordSerializer, ResetPasswordSerializer, + SSOSerializer) from .decorators import super_admin_required @@ -287,12 +289,24 @@ def user_index_page(request, username): return render(request, "oj/account/user_index.html") -def auth_page(request): - if not request.user.is_authenticated(): - return render(request, "oj/account/oauth.html") - callback = request.GET.get("callback", None) - if not callback: - return error_page(request, u"参数错误") - token = rand_str() - request.user.auth_token = token - return render(request, "oj/account/oauth.html", {"callback": callback, "token": token}) +class SSOAPIView(APIView): + def post(self, request): + serializer = SSOSerializer(data=request.data) + if serializer.is_valid(): + try: + user = User.objects.get(auth_token=serializer.data["token"]) + return success_response({"username": user.username}) + except User.DoesNotExist: + return error_response(u"用户不存在") + else: + return serializer_invalid_response(serializer) + + @login_required + def get(self, request): + callback = request.GET.get("callback", None) + if not callback or callback != settings.SSO["callback"]: + return error_page(request, u"参数错误") + token = rand_str() + request.user.auth_token = token + request.user.save() + return render(request, "oj/account/sso.html", {"redirect_url": callback + "?token=" + token, "callback": callback}) \ No newline at end of file diff --git a/oj/local_settings.py b/oj/local_settings.py index 074b1b01..a08ae7e2 100644 --- a/oj/local_settings.py +++ b/oj/local_settings.py @@ -11,18 +11,13 @@ DATABASES = { }, # submission 的 name 和 engine 请勿修改,其他代码会用到 'submission': { - 'NAME': 'oj_submission', - 'ENGINE': 'django.db.backends.mysql', - 'CONN_MAX_AGE': 0.1, - 'HOST': "127.0.0.1", - 'PORT': 3306, - 'USER': 'root', - 'PASSWORD': 'root', + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(BASE_DIR, 'db1.sqlite3'), } } REDIS_CACHE = { - "host": "121.42.32.129", + "host": "127.0.0.1", "port": 6379, "db": 1 } @@ -36,4 +31,6 @@ ALLOWED_HOSTS = [] STATICFILES_DIRS = [os.path.join(BASE_DIR, "static/src/"), BASE_DIR] # 模板文件夹 -TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'template/src/')] \ No newline at end of file +TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'template/src/')] + +SSO = {"callback": "http://localhost:8765/login"} \ No newline at end of file diff --git a/oj/server_settings.py b/oj/server_settings.py index 9f8f084e..bba27c6f 100644 --- a/oj/server_settings.py +++ b/oj/server_settings.py @@ -43,3 +43,5 @@ STATICFILES_DIRS = [os.path.join(BASE_DIR, "static/release/"), os.path.join(BASE TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'template/release/')] SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') + +SSO = {"callback": "https://discuss.acmer.site/login"} diff --git a/oj/urls.py b/oj/urls.py index f95fa2bb..2b6047d5 100644 --- a/oj/urls.py +++ b/oj/urls.py @@ -6,7 +6,7 @@ from django.views.generic import TemplateView from account.views import (UserLoginAPIView, UsernameCheckAPIView, UserRegisterAPIView, UserChangePasswordAPIView, EmailCheckAPIView, UserAdminAPIView, UserInfoAPIView, - ApplyResetPasswordAPIView) + ApplyResetPasswordAPIView, SSOAPIView) from announcement.views import AnnouncementAdminAPIView @@ -127,7 +127,7 @@ urlpatterns = [ url(r'^account/settings/$', TemplateView.as_view(template_name="oj/account/settings.html"), name="account_setting_page"), url(r'^account/settings/avatar/$', TemplateView.as_view(template_name="oj/account/avatar.html"), name="avatar_settings_page"), - url(r'^account/auth/$', "account.views.auth_page", name="auth_login_page"), + url(r'^account/sso/$', SSOAPIView.as_view(), name="sso_api"), ] diff --git a/template/src/oj/account/sso.html b/template/src/oj/account/sso.html index 2713d19c..23f04455 100644 --- a/template/src/oj/account/sso.html +++ b/template/src/oj/account/sso.html @@ -5,17 +5,13 @@ {% block body %}
- {% if request.user.is_authenticated %} -

3秒钟后将跳转到{{ callback }}

+

3秒钟后将使用账号{{ request.user.username }}登录{{ callback }}

+ + - {% else %} - - {% endif %} - -
{% endblock %}