12345678910111213141516171819202122232425 |
- from rest_framework.authentication import BaseAuthentication
- from rest_framework.exceptions import AuthenticationFailed
- from background.Object import TokenObject
- from background.models import WechatUserInfo
- class MyAuthentication(BaseAuthentication):
- def authenticate(self, request):
- if request.method in ["GET", "POST", "PUT", "DELETE"]:
- token_obj = request.META.get('HTTP_AUTHORIZATION')
- if not token_obj:
- raise AuthenticationFailed('缺少token')
- print(token_obj)
- token = TokenObject(token_obj)
- if token.code != 0:
- raise AuthenticationFailed('token无效')
- user_id = token.userID
- user_name = token.user
- wechat_user = WechatUserInfo.objects.filter(user_id=user_id, name=user_name, status=1)
- if not wechat_user.exists():
- raise AuthenticationFailed('token无效')
- return user_id, user_name
- else:
- return None, None
|