Skip to content

Commit

Permalink
feat(account): remove oauth code
Browse files Browse the repository at this point in the history
  • Loading branch information
OrenZhang committed Dec 30, 2024
1 parent 6dff01d commit ce8dc56
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 78 deletions.
2 changes: 0 additions & 2 deletions apps/account/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
PHONE_VERIFY_CODE_TIMEOUT = 60 * 60 # second
PHONE_VERIFY_CODE_KEY = "phone_verify_code:{phone_number}"

LOGIN_CODE_KEY = "login_code:{code}"

WECHAT_LOGIN_STATE_KEY = "wechat_login_state:{state}"
WECHAT_USER_INFO_KEY = "wechat_user_info:{code}"

Expand Down
27 changes: 1 addition & 26 deletions apps/account/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@
SHORT_CHAR_LENGTH,
)
from ovinc_client.core.models import SoftDeletedManager, SoftDeletedModel
from ovinc_client.core.utils import num_code, uniq_id
from ovinc_client.core.utils import num_code

from apps.account.constants import (
LOGIN_CODE_KEY,
PHONE_VERIFY_CODE_KEY,
PHONE_VERIFY_CODE_LENGTH,
PHONE_VERIFY_CODE_TIMEOUT,
Expand Down Expand Up @@ -99,30 +98,6 @@ class Meta:
verbose_name_plural = verbose_name
ordering = ["username"]

def generate_oauth_code(self) -> str:
"""
Generate OAuth User Code
"""

code = uniq_id()
cache_key = LOGIN_CODE_KEY.format(code=code)
cache.set(cache_key, self.username, timeout=settings.OAUTH_CODE_TIMEOUT)
return code

@classmethod
def check_oauth_code(cls, code: str) -> (bool, Union[models.Model, None]):
"""
Check OAuth User Code
"""

cache_key = LOGIN_CODE_KEY.format(code=code)
username = cache.get(cache_key)
cache.delete(cache_key)
try:
return True, cls.objects.get(username=username)
except cls.DoesNotExist: # pylint: disable=E1101
return False, None

@classmethod
def check_ticket(cls, ticket: str) -> (bool, Union[models.Model, None]):
"""
Expand Down
12 changes: 0 additions & 12 deletions apps/account/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class SignInSerializer(Serializer):

username = serializers.CharField(label=gettext_lazy("Username"))
password = serializers.CharField(label=gettext_lazy("Password"))
is_oauth = serializers.BooleanField(label=gettext_lazy("Is OAuth"), default=False)
wechat_code = serializers.CharField(label=gettext_lazy("WeChat Code"), required=False)
tcaptcha = serializers.JSONField(label=gettext_lazy("Tencent Captcha"), required=False, default=dict)

Expand All @@ -57,7 +56,6 @@ class UserRegistrySerializer(ModelSerializer):
"""

username = serializers.RegexField(label=gettext_lazy("Username"), regex=USERNAME_REGEX)
is_oauth = serializers.BooleanField(label=gettext_lazy("Is OAuth"), default=False)
wechat_code = serializers.CharField(label=gettext_lazy("WeChat Code"), required=False)
phone_area = serializers.ChoiceField(label=gettext_lazy("Phone Area"), choices=PhoneNumberAreas.choices)
phone_number = serializers.CharField(label=gettext_lazy("Phone Number"))
Expand All @@ -74,7 +72,6 @@ class Meta:
"username",
"nick_name",
"password",
"is_oauth",
"wechat_code",
"phone_area",
"phone_number",
Expand Down Expand Up @@ -108,14 +105,6 @@ def validate_phone_number(self, phone_number: str) -> str:
return phone_number


class VerifyCodeRequestSerializer(Serializer):
"""
Verify Code
"""

code = serializers.CharField(label=gettext_lazy("Code"))


class VerifyTicketRequestSerializer(Serializer):
"""
Verify Ticket
Expand All @@ -131,7 +120,6 @@ class WeChatLoginReqSerializer(Serializer):

code = serializers.CharField(label=gettext_lazy("Code"))
state = serializers.CharField(label=gettext_lazy("State"))
is_oauth = serializers.BooleanField(label=gettext_lazy("Is OAuth"), default=False)


class ResetPasswordRequestSerializer(Serializer):
Expand Down
36 changes: 1 addition & 35 deletions apps/account/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
SignInSerializer,
UserInfoSerializer,
UserRegistrySerializer,
VerifyCodeRequestSerializer,
VerifyTicketRequestSerializer,
WeChatLoginReqSerializer,
)
Expand Down Expand Up @@ -93,10 +92,6 @@ async def sign_in(self, request, *args, **kwargs):
# auth session
await database_sync_to_async(auth.login)(request, user)

# oauth
if request_data["is_oauth"]:
return Response({"code": user.generate_oauth_code()})

return Response()

@action(methods=["GET"], detail=False)
Expand Down Expand Up @@ -135,10 +130,6 @@ async def sign_up(self, request, *args, **kwargs):
# login session
await database_sync_to_async(auth.login)(request, user)

# oauth
if request_data["is_oauth"]:
return Response({"code": user.generate_oauth_code()})

# response
return Response()

Expand All @@ -164,31 +155,6 @@ async def phone_verify_code(self, request, *args, **kwargs):
# response
return Response()

@action(methods=["GET"], detail=False)
async def oauth_code(self, request, *args, **kwargs):
"""
oauth code
"""

return Response({"code": request.user.generate_oauth_code()})

@action(methods=["POST"], detail=False, authentication_classes=[ApplicationAuthenticate])
async def verify_code(self, request, *args, **kwargs):
"""
verify oauth code
"""

# validate request
request_serializer = VerifyCodeRequestSerializer(data=request.data)
request_serializer.is_valid(raise_exception=True)
request_data = request_serializer.validated_data

# load user
is_success, user = await database_sync_to_async(USER_MODEL.check_oauth_code)(request_data["code"])
if is_success:
return Response(await UserInfoSerializer(instance=user).adata)
raise WrongToken()

@action(methods=["POST"], detail=False, authentication_classes=[ApplicationAuthenticate])
async def verify_ticket(self, request, *args, **kwargs):
"""
Expand Down Expand Up @@ -296,7 +262,7 @@ async def wechat_login(self, request, *args, **kwargs):
if user:
await self.update_user_by_wechat(user, code)
await database_sync_to_async(auth.login)(request, user)
return Response({"code": user.generate_oauth_code() if request_data["is_oauth"] else ""})
return Response()

# need registry
return Response({"wechat_code": code})
Expand Down
3 changes: 0 additions & 3 deletions entry/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,6 @@
# TCI Callback
TCI_AUDIT_CALLBACK_PREFIX = os.getenv("TCI_AUDIT_CALLBACK_PREFIX", "")

# OAuth
OAUTH_CODE_TIMEOUT = int(os.getenv("OAUTH_CODE_TIMEOUT", str(60 * 5)))

# WeChat
WECHAT_APP_ID = os.getenv("WECHAT_APP_ID")
WECHAT_APP_KEY = os.getenv("WECHAT_APP_KEY")
Expand Down

0 comments on commit ce8dc56

Please sign in to comment.