author.py 1017 B

12345678910111213141516171819202122232425
  1. from rest_framework.authentication import BaseAuthentication
  2. from rest_framework.exceptions import AuthenticationFailed
  3. from background.Object import TokenObject
  4. from background.models import WechatUserInfo
  5. class MyAuthentication(BaseAuthentication):
  6. def authenticate(self, request):
  7. if request.method in ["GET", "POST", "PUT", "DELETE"]:
  8. token_obj = request.META.get('HTTP_AUTHORIZATION')
  9. if not token_obj:
  10. raise AuthenticationFailed('缺少token')
  11. print(token_obj)
  12. token = TokenObject(token_obj)
  13. if token.code != 0:
  14. raise AuthenticationFailed('token无效')
  15. user_id = token.userID
  16. user_name = token.user
  17. wechat_user = WechatUserInfo.objects.filter(user_id=user_id, name=user_name, status=1)
  18. if not wechat_user.exists():
  19. raise AuthenticationFailed('token无效')
  20. return user_id, user_name
  21. else:
  22. return None, None