def authenticate_credentials(self, key): hashed_token_key = make_password(key) try: token = self.model.objects.get(key=hashed_token_key) if timezone.now() > token.creation_date + timezone.timedelta(days=5): raise OldTokenException except (self.model.DoesNotExist, OldTokenException): status, info = check_auth_token(key) if status: uuid = info['access']['user']['id'] if User.objects.filter(uuid=uuid).count() == 0: user = User.objects.create(uuid=uuid) else: user = User.objects.get(uuid=uuid) if Token.objects.filter(user=user).count() == 0: token = Token.objects.create(user=user, key=hashed_token_key, creation_date=timezone.now()) else: token = Token.objects.get(user=user) token.key = hashed_token_key token.save() else: raise exceptions.AuthenticationFailed('Invalid token') return token.user, token
def authenticate(request): """ Checks the validity of the authentication token of the user .. deprecated:: Use authenticate_user.KamakiTokenAuthentication """ # request.META contains all the headers of the request auth_token = request.META.get("HTTP_AUTHORIZATION").split()[-1] status, info = check_auth_token(auth_token) if status: return JsonResponse({"result": "success"}, status=200) else: error_info = json.loads(info)['unauthorized'] error_info['details'] = error_info.get('details') + 'unauthorized' return JsonResponse({"errors": [error_info]}, status=401)