def permission_denied(self, request, message=None): """ If request is not permitted, determine what kind of exception to raise. """ if not request.user: raise exceptions.NotAuthenticated() raise exceptions.PermissionDenied(detail=message)
def get_user_for_token(token, scope): """ Given a selfcontained token and a scope try to parse and unsign it. If max_age is specified it checks token expiration. If token passes a validation, returns a user instance corresponding with user_id stored in the incoming token. """ try: data = jwt.decode(token, settings.SECRET_KEY) except jwt.DecodeError: raise exc.NotAuthenticated("Invalid token") model_cls = apps.get_model("users", "User") try: user = model_cls.objects.get(pk=data["user_%s_id" % (scope)]) except (model_cls.DoesNotExist, KeyError): raise exc.NotAuthenticated("Invalid token") else: return user
def _wrapped(*args, **kwargs): if not identity.authenticated: msg = "Authentication required before calling '%s'." % fnc.__name__ raise exc.NotAuthenticated(msg) return fnc(*args, **kwargs)