Esempio n. 1
0
    def validate(self, attrs):
        credentials = {
            self.username_field: attrs[self.username_field],
            "password": attrs["password"],
        }

        if attrs[self.username_field] and attrs["password"]:
            user = authenticate(**credentials)

            if user:
                if not user.is_active:
                    raise serializers.ValidationError(
                        _("User account is disabled."))

                payload = jwt_payload_handler(user)
                refresh_expires_in = (
                    payload["origIat"] +
                    jwt_settings.JWT_REFRESH_EXPIRATION_DELTA.total_seconds())

                return {
                    "token": jwt_encode_handler(payload),
                    "payload": payload,
                    "refresh_expires_in": refresh_expires_in,
                }
            raise serializers.ValidationError(
                _("Please enter valid credentials"))
        else:
            raise serializers.ValidationError(
                _(f'Must include "{self.username_field}" and "password".'))
Esempio n. 2
0
    def validate(self, attrs):
        # Get and check payload
        try:
            payload = get_payload(attrs["token"])
        except (JSONWebTokenExpired, JSONWebTokenError) as e:
            raise serializers.ValidationError(str(e))

        # Get and check user by payload
        try:
            user = get_user_by_payload(payload)
        except JSONWebTokenError as e:
            raise serializers.ValidationError(str(e))
        # Get and check "origIat"
        orig_iat = payload.get("origIat")

        if not orig_iat:
            raise serializers.ValidationError(_("origIat field is required"))

        if jwt_refresh_expired_handler(orig_iat):
            raise serializers.ValidationError(_("Refresh has expired"))

        new_payload = jwt_payload_handler(user)
        new_payload["origIat"] = orig_iat
        refresh_expires_in = (
            orig_iat +
            jwt_settings.JWT_REFRESH_EXPIRATION_DELTA.total_seconds())
        token = jwt_encode_handler(new_payload)

        return {
            "token": token,
            "payload": new_payload,
            "refresh_expires_in": refresh_expires_in,
        }
Esempio n. 3
0
    def validate(self, validate_data):
        reason = validate_data.get('reason', None)
        confirm = validate_data.get('confirm', None)

        if not confirm and reason is None:
            raise serializers.ValidationError({'reason': 'This field is required!'})

        return validate_data
Esempio n. 4
0
    def validate(self, attrs):
        request = self.context["request"]
        strategy = load_strategy(request)

        try:
            backend = load_backend(strategy,
                                   attrs["provider"],
                                   redirect_uri=None)
        except MissingBackend:
            raise serializers.ValidationError(_("Provider not found"))

        if request.user.is_authenticated:
            authenticated_user = request.user
        else:
            authenticated_user = None

        try:
            user = backend.do_auth(attrs["access_token"],
                                   user=authenticated_user)
        except AuthException as e:
            raise serializers.ValidationError(_(str(e)))

        if user is None:
            raise serializers.ValidationError(_("Invalid token"))

        user_model = strategy.storage.user.user_model()

        if not isinstance(user, user_model):
            raise serializers.ValidationError(
                _("`{}` is not a user instance").format(type(user).__name__))

        _do_login(backend, user, user.social_user)

        return {
            "token": get_token(user),
            "social": {
                "id": user.social_user.id,
                "provider": user.social_user.provider,
                "uid": user.social_user.uid,
                "extraData": user.social_user.extra_data,
                "created": user.social_user.created,
                "modified": user.social_user.modified,
            },
        }
Esempio n. 5
0
    def validate(self, validate_data):
        email = validate_data.get('email', None)

        if email:
            try:
                user = UserModel.objects.get(email=email)
                validate_data['user_id'] = user.id
            except UserModel.DoesNotExist:
                raise serializers.ValidationError({'email': 'No user for such email'})

        return validate_data
Esempio n. 6
0
    def validate(self, attrs):
        user = (self.context and self.context["request"]
                and self.context["request"].user) or self.user
        # why assert? There are ValidationError / fail everywhere
        assert user is not None

        try:
            validate_password(attrs["new_password"], user)
        except django_exceptions.ValidationError as e:
            raise serializers.ValidationError(
                {"new_password": list(e.messages)})
        return super().validate(attrs)
Esempio n. 7
0
    def validate(self, attrs):
        user = User(**attrs)
        password = attrs.get("password")

        try:
            validate_password(password, user)
        except django_exceptions.ValidationError as e:
            serializer_error = serializers.as_serializer_error(e)
            raise serializers.ValidationError(
                {"password": serializer_error["non_field_errors"]})

        return attrs
Esempio n. 8
0
 def validate(self, attrs):
     try:
         payload = get_payload(attrs["token"])
     except (JSONWebTokenExpired, JSONWebTokenError) as e:
         raise serializers.ValidationError(str(e))
     return {"payload": payload}