OnlineJudge/account/views/oj.py

95 lines
3.3 KiB
Python
Raw Normal View History

2016-09-25 06:07:45 +00:00
from django.contrib import auth
from django.core.exceptions import MultipleObjectsReturned
from django.utils.translation import ugettext as _
2017-01-23 08:01:56 +00:00
from otpauth import OtpAuth
2016-11-19 04:32:23 +00:00
from utils.api import APIView, validate_serializer
2016-09-25 06:07:45 +00:00
from utils.captcha import Captcha
2017-01-23 08:48:04 +00:00
2016-09-25 06:07:45 +00:00
from ..decorators import login_required
from ..models import User, UserProfile
2017-01-23 08:48:04 +00:00
from ..serializers import (UserChangePasswordSerializer, UserLoginSerializer,
UserRegisterSerializer)
2016-09-25 06:07:45 +00:00
2016-11-19 04:37:27 +00:00
class UserLoginAPI(APIView):
2016-11-19 04:32:23 +00:00
@validate_serializer(UserLoginSerializer)
2016-09-25 06:07:45 +00:00
def post(self, request):
"""
User login api
"""
2016-11-19 04:32:23 +00:00
data = request.data
user = auth.authenticate(username=data["username"], password=data["password"])
# None is returned if username or password is wrong
if user:
if not user.two_factor_auth:
auth.login(request, user)
return self.success(_("Succeeded"))
2016-09-25 06:07:45 +00:00
2016-11-19 04:32:23 +00:00
# `tfa_code` not in post data
if user.two_factor_auth and "tfa_code" not in data:
return self.success("tfa_required")
2016-09-25 06:07:45 +00:00
2016-11-19 04:32:23 +00:00
if OtpAuth(user.tfa_token).valid_totp(data["tfa_code"]):
auth.login(request, user)
return self.success(_("Succeeded"))
2016-09-25 06:07:45 +00:00
else:
2016-11-19 04:32:23 +00:00
return self.error(_("Invalid two factor verification code"))
2016-09-25 06:07:45 +00:00
else:
2016-11-19 04:32:23 +00:00
return self.error(_("Invalid username or password"))
2016-09-25 06:07:45 +00:00
# todo remove this, only for debug use
def get(self, request):
auth.login(request, auth.authenticate(username=request.GET["username"], password=request.GET["password"]))
2016-10-29 18:17:35 +00:00
return self.success({})
2016-09-25 06:07:45 +00:00
2016-11-19 04:37:27 +00:00
class UserRegisterAPI(APIView):
2016-11-19 04:32:23 +00:00
@validate_serializer(UserRegisterSerializer)
2016-09-25 06:07:45 +00:00
def post(self, request):
"""
User register api
"""
2016-11-19 04:32:23 +00:00
data = request.data
captcha = Captcha(request)
if not captcha.check(data["captcha"]):
return self.error(_("Invalid captcha"))
try:
User.objects.get(username=data["username"])
return self.error(_("Username already exists"))
except User.DoesNotExist:
pass
try:
User.objects.get(email=data["email"])
return self.error(_("Email already exists"))
# Some old data has duplicate email
except MultipleObjectsReturned:
return self.error(_("Email already exists"))
except User.DoesNotExist:
user = User.objects.create(username=data["username"], email=data["email"])
user.set_password(data["password"])
user.save()
UserProfile.objects.create(user=user)
return self.success(_("Succeeded"))
2016-09-25 06:07:45 +00:00
2016-11-19 04:37:27 +00:00
class UserChangePasswordAPI(APIView):
2016-11-19 04:32:23 +00:00
@validate_serializer(UserChangePasswordSerializer)
2016-09-25 06:07:45 +00:00
@login_required
def post(self, request):
"""
User change password api
"""
2016-11-19 04:32:23 +00:00
data = request.data
captcha = Captcha(request)
if not captcha.check(data["captcha"]):
return self.error(_("Invalid captcha"))
username = request.user.username
user = auth.authenticate(username=username, password=data["old_password"])
if user:
user.set_password(data["new_password"])
user.save()
return self.success(_("Succeeded"))
2016-09-25 06:07:45 +00:00
else:
2016-11-19 04:32:23 +00:00
return self.error(_("Invalid old password"))