2017-04-18 11:57:57 +08:00
|
|
|
from datetime import timedelta
|
|
|
|
|
2017-04-18 14:34:23 +08:00
|
|
|
from django.conf import settings
|
2017-04-19 01:37:10 +08:00
|
|
|
from django.contrib import auth
|
2016-09-25 14:07:45 +08:00
|
|
|
from django.core.exceptions import MultipleObjectsReturned
|
2017-04-18 11:57:57 +08:00
|
|
|
from django.utils.timezone import now
|
2017-04-19 01:37:10 +08:00
|
|
|
from otpauth import OtpAuth
|
2017-01-23 16:01:56 +08:00
|
|
|
|
2017-04-18 14:34:23 +08:00
|
|
|
from conf.models import WebsiteConfig
|
2016-11-19 12:32:23 +08:00
|
|
|
from utils.api import APIView, validate_serializer
|
2016-09-25 14:07:45 +08:00
|
|
|
from utils.captcha import Captcha
|
2017-04-18 11:57:57 +08:00
|
|
|
from utils.shortcuts import rand_str
|
2017-01-23 16:48:04 +08:00
|
|
|
|
2016-09-25 14:07:45 +08:00
|
|
|
from ..decorators import login_required
|
|
|
|
from ..models import User, UserProfile
|
2017-04-19 01:37:10 +08:00
|
|
|
from ..serializers import (ApplyResetPasswordSerializer,
|
|
|
|
ResetPasswordSerializer,
|
|
|
|
UserChangePasswordSerializer, UserLoginSerializer,
|
|
|
|
UserRegisterSerializer)
|
2017-04-19 02:03:48 +08:00
|
|
|
from ..tasks import send_email_async
|
2016-09-25 14:07:45 +08:00
|
|
|
|
|
|
|
|
2016-11-19 12:37:27 +08:00
|
|
|
class UserLoginAPI(APIView):
|
2016-11-19 12:32:23 +08:00
|
|
|
@validate_serializer(UserLoginSerializer)
|
2016-09-25 14:07:45 +08:00
|
|
|
def post(self, request):
|
|
|
|
"""
|
|
|
|
User login api
|
|
|
|
"""
|
2016-11-19 12:32:23 +08: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)
|
2017-04-19 02:03:48 +08:00
|
|
|
return self.success("Succeeded")
|
2016-09-25 14:07:45 +08:00
|
|
|
|
2016-11-19 12:32:23 +08: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 14:07:45 +08:00
|
|
|
|
2016-11-19 12:32:23 +08:00
|
|
|
if OtpAuth(user.tfa_token).valid_totp(data["tfa_code"]):
|
|
|
|
auth.login(request, user)
|
2017-04-19 02:03:48 +08:00
|
|
|
return self.success("Succeeded")
|
2016-09-25 14:07:45 +08:00
|
|
|
else:
|
2017-04-19 02:03:48 +08:00
|
|
|
return self.error("Invalid two factor verification code")
|
2016-09-25 14:07:45 +08:00
|
|
|
else:
|
2017-04-19 02:03:48 +08:00
|
|
|
return self.error("Invalid username or password")
|
2016-09-25 14:07:45 +08: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-30 02:17:35 +08:00
|
|
|
return self.success({})
|
2016-09-25 14:07:45 +08:00
|
|
|
|
|
|
|
|
2016-11-19 12:37:27 +08:00
|
|
|
class UserRegisterAPI(APIView):
|
2016-11-19 12:32:23 +08:00
|
|
|
@validate_serializer(UserRegisterSerializer)
|
2016-09-25 14:07:45 +08:00
|
|
|
def post(self, request):
|
|
|
|
"""
|
|
|
|
User register api
|
|
|
|
"""
|
2016-11-19 12:32:23 +08:00
|
|
|
data = request.data
|
|
|
|
captcha = Captcha(request)
|
2017-04-30 21:58:34 +08:00
|
|
|
# if not captcha.check(data["captcha"]):
|
|
|
|
# return self.error("Invalid captcha")
|
2016-11-19 12:32:23 +08:00
|
|
|
try:
|
|
|
|
User.objects.get(username=data["username"])
|
2017-04-19 02:03:48 +08:00
|
|
|
return self.error("Username already exists")
|
2016-11-19 12:32:23 +08:00
|
|
|
except User.DoesNotExist:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
User.objects.get(email=data["email"])
|
2017-04-19 02:03:48 +08:00
|
|
|
return self.error("Email already exists")
|
2016-11-19 12:32:23 +08:00
|
|
|
# Some old data has duplicate email
|
|
|
|
except MultipleObjectsReturned:
|
2017-04-19 02:03:48 +08:00
|
|
|
return self.error("Email already exists")
|
2016-11-19 12:32:23 +08:00
|
|
|
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)
|
2017-04-19 02:03:48 +08:00
|
|
|
return self.success("Succeeded")
|
2016-09-25 14:07:45 +08:00
|
|
|
|
|
|
|
|
2016-11-19 12:37:27 +08:00
|
|
|
class UserChangePasswordAPI(APIView):
|
2016-11-19 12:32:23 +08:00
|
|
|
@validate_serializer(UserChangePasswordSerializer)
|
2016-09-25 14:07:45 +08:00
|
|
|
@login_required
|
|
|
|
def post(self, request):
|
|
|
|
"""
|
|
|
|
User change password api
|
|
|
|
"""
|
2016-11-19 12:32:23 +08:00
|
|
|
data = request.data
|
|
|
|
captcha = Captcha(request)
|
|
|
|
if not captcha.check(data["captcha"]):
|
2017-04-19 02:03:48 +08:00
|
|
|
return self.error("Invalid captcha")
|
2016-11-19 12:32:23 +08:00
|
|
|
username = request.user.username
|
|
|
|
user = auth.authenticate(username=username, password=data["old_password"])
|
|
|
|
if user:
|
|
|
|
user.set_password(data["new_password"])
|
|
|
|
user.save()
|
2017-04-19 02:03:48 +08:00
|
|
|
return self.success("Succeeded")
|
2016-09-25 14:07:45 +08:00
|
|
|
else:
|
2017-04-19 02:03:48 +08:00
|
|
|
return self.error("Invalid old password")
|
2017-04-18 11:57:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ApplyResetPasswordAPI(APIView):
|
|
|
|
@validate_serializer(ApplyResetPasswordSerializer)
|
|
|
|
def post(self, request):
|
|
|
|
data = request.data
|
|
|
|
captcha = Captcha(request)
|
2017-04-18 15:19:26 +08:00
|
|
|
config = WebsiteConfig.objects.first()
|
2017-04-18 11:57:57 +08:00
|
|
|
if not captcha.check(data["captcha"]):
|
2017-04-19 02:03:48 +08:00
|
|
|
return self.error("Invalid captcha")
|
2017-04-18 11:57:57 +08:00
|
|
|
try:
|
|
|
|
user = User.objects.get(email=data["email"])
|
|
|
|
except User.DoesNotExist:
|
2017-04-19 02:03:48 +08:00
|
|
|
return self.error("User does not exist")
|
2017-04-18 11:57:57 +08:00
|
|
|
if user.reset_password_token_expire_time and 0 < (
|
2017-04-18 14:34:23 +08:00
|
|
|
user.reset_password_token_expire_time - now()).total_seconds() < 20 * 60:
|
2017-04-19 02:03:48 +08:00
|
|
|
return self.error("You can only reset password once per 20 minutes")
|
2017-04-18 11:57:57 +08:00
|
|
|
user.reset_password_token = rand_str()
|
|
|
|
|
|
|
|
user.reset_password_token_expire_time = now() + timedelta(minutes=20)
|
|
|
|
user.save()
|
2017-04-18 14:34:23 +08:00
|
|
|
email_template = open("reset_password_email.html", "w",
|
|
|
|
encoding="utf-8").read()
|
|
|
|
email_template = email_template.replace("{{ username }}", user.username). \
|
|
|
|
replace("{{ website_name }}", settings.WEBSITE_INFO["website_name"]). \
|
|
|
|
replace("{{ link }}", settings.WEBSITE_INFO["url"] + "/reset_password/t/" +
|
|
|
|
user.reset_password_token)
|
2017-04-19 02:03:48 +08:00
|
|
|
send_email_async.delay(config.name,
|
|
|
|
user.email,
|
|
|
|
user.username,
|
|
|
|
config.name + " 登录信息找回邮件",
|
|
|
|
email_template)
|
|
|
|
return self.success("Succeeded")
|
2017-04-18 11:57:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ResetPasswordAPI(APIView):
|
2017-04-18 15:19:26 +08:00
|
|
|
@validate_serializer(ResetPasswordSerializer)
|
2017-04-18 11:57:57 +08:00
|
|
|
def post(self, request):
|
|
|
|
data = request.data
|
|
|
|
captcha = Captcha(request)
|
|
|
|
if not captcha.check(data["captcha"]):
|
2017-04-19 02:03:48 +08:00
|
|
|
return self.error("Invalid captcha")
|
2017-04-18 11:57:57 +08:00
|
|
|
try:
|
|
|
|
user = User.objects.get(reset_password_token=data["token"])
|
|
|
|
except User.DoesNotExist:
|
2017-04-19 02:03:48 +08:00
|
|
|
return self.error("Token dose not exist")
|
2017-04-18 11:57:57 +08:00
|
|
|
if 0 < (user.reset_password_token_expire_time - now()).total_seconds() < 30 * 60:
|
2017-04-19 02:03:48 +08:00
|
|
|
return self.error("Token expired")
|
2017-04-18 11:57:57 +08:00
|
|
|
user.reset_password_token = None
|
|
|
|
user.set_password(data["password"])
|
|
|
|
user.save()
|
2017-04-19 02:03:48 +08:00
|
|
|
return self.success("Succeeded")
|