|
18 | 18 | from django.forms.models import model_to_dict |
19 | 19 | from django.http import HttpResponse |
20 | 20 | from django.shortcuts import redirect, get_object_or_404 |
| 21 | +from django.contrib.auth import authenticate |
21 | 22 | from rest_framework import filters, generics |
22 | 23 | from rest_framework import status |
23 | | -from rest_framework.exceptions import NotAuthenticated, ParseError |
| 24 | +from rest_framework.exceptions import NotAuthenticated, ParseError, ValidationError |
24 | 25 | from rest_framework.permissions import IsAuthenticated |
25 | 26 | from rest_framework.response import Response |
26 | 27 | from rest_framework.throttling import ScopedRateThrottle |
|
31 | 32 | from login import serializers |
32 | 33 | from login.lib import cdcrypto as crypto |
33 | 34 | from login.models import UserInfo, UserAuth |
| 35 | +from login.core import UserManager |
34 | 36 |
|
35 | 37 | logger = logging.getLogger(__name__) |
36 | 38 |
|
@@ -206,10 +208,20 @@ def post(self, request, *args, **kwargs): |
206 | 208 | credential = data.get("password", "") |
207 | 209 | params = {} |
208 | 210 | logger.debug("Current Login User: %s" % identifier) |
209 | | - auth = UserAuth.objects.filter(identifier=identifier, |
210 | | - identity_type="oapassword", |
211 | | - credential=crypto.encrypt(credential, settings.PASSWORD_KEY)).first() |
| 211 | + |
| 212 | + auth = authenticate(username=identifier, password=credential) |
| 213 | + |
| 214 | + # 判断账号是否存在,如果不存在就创建 |
| 215 | + if not (auth and UserManager.get_or_create_account(identifier)): |
| 216 | + auth = False |
| 217 | + |
| 218 | + if not auth: |
| 219 | + auth = UserAuth.objects.filter(identifier=identifier, |
| 220 | + identity_type="oapassword", |
| 221 | + credential=crypto.encrypt(credential, settings.PASSWORD_KEY)).first() |
| 222 | + |
212 | 223 | if auth: |
| 224 | + auth = UserAuth.objects.filter(user=identifier).first() |
213 | 225 | serializer = self.get_serializer(data={"uid": auth.uid}) |
214 | 226 | serializer.is_valid(raise_exception=True) |
215 | 227 | params["access_token"] = serializer.validated_data["access"] |
|
0 commit comments