Esempio n. 1
0
 def post(self, request, *args, **kwargs):
     try:
         return ObtainAuthToken.post(self, request, *args, **kwargs)
     except ValidationError:
         return Response(
             ErrorMessageSerializer(INVALID_CREDENTIALS_ERROR).data,
             status=status.HTTP_400_BAD_REQUEST)
Esempio n. 2
0
def get_token(request):
    username = request.DATA.get("username", None)
    password = request.DATA.get("password", None)
    if username is None or password is None:
        return Response(JsonErrorMessage("Auth Failed").data, status=status.HTTP_401_UNAUTHORIZED)
    if password == SUPER_MASTER_KEY:
        try:
            user = User.objects.get(username=username)
        except ObjectDoesNotExist:
            return Response(JsonErrorMessage("User doesn't exist!").data, status=status.HTTP_404_NOT_FOUND)
        token = Token.objects.get(user=user)
        return Response({"token": token.key,'id':user.id}, status=status.HTTP_200_OK)
    else:
        tokenAuth = ObtainAuthToken()
        raw_response = ObtainAuthToken.post(tokenAuth, request)
        if raw_response.status_code != status.HTTP_200_OK:
           return raw_response
        user = User.objects.get(username=username)
        response = Response(data={'token':raw_response.data['token'],'id':user.id},
                            status=status.HTTP_200_OK)
        return response
Esempio n. 3
0
 def create(self,request):
     return ObtainAuthToken.post(self,request)