Skip to content

Commit

Permalink
Merge pull request #1919 from st1020/feat/add-token-login
Browse files Browse the repository at this point in the history
feat: add token login
  • Loading branch information
st1020 authored Dec 12, 2023
2 parents b6c7f7a + d62e9cf commit 638f671
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions dongtai_conf/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1109,3 +1109,6 @@ def set_asyncio_policy():

# log service timeout
LOG_SERVICE_TIMEOUT = config.getint("log_service", "port", fallback=10)

# enable token login
TOKEN_LOGIN = config.getboolean("other", "token_login", fallback=False)
16 changes: 16 additions & 0 deletions dongtai_web/views/user_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@

from captcha.models import CaptchaStore
from django.contrib.auth import authenticate, login
from django.http import HttpResponseRedirect
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from drf_spectacular.utils import extend_schema
from rest_framework.authtoken.models import Token

from dongtai_common.endpoint import R, UserEndPoint
from dongtai_common.models.user import User
from dongtai_common.utils.request_type import Request
from dongtai_conf.patch import patch_point, to_patch
from dongtai_conf.settings import TOKEN_LOGIN

logger = logging.getLogger("dongtai-webapi")

Expand Down Expand Up @@ -84,3 +87,16 @@ def post(self, request: Request):
except Exception as e:
logger.exception("uncatched exception: ", exc_info=e)
return R.failure(status=202, msg=_("Login failed"))

if TOKEN_LOGIN:

def get(self, request: Request):
url = request.GET.get("url", "/")
token = request.GET.get("token")
token_obj = Token.objects.filter(key=token).first()
if not url.startswith("/"):
url = "/"
if token_obj is not None:
login(request, token_obj.user)
return HttpResponseRedirect(url)
return HttpResponseRedirect("/login")

0 comments on commit 638f671

Please sign in to comment.