From 284941444519e44b2d4322e0af2c9edd408d2b7a Mon Sep 17 00:00:00 2001 From: Michael Shamoon <4887959+shamoon@users.noreply.github.com> Date: Fri, 18 Nov 2022 14:21:31 -0800 Subject: [PATCH] one-way imap password setting via API, ObfuscatedPasswordField --- src/documents/serialisers.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py index d2fa10af9..cede116fe 100644 --- a/src/documents/serialisers.py +++ b/src/documents/serialisers.py @@ -692,7 +692,21 @@ class AcknowledgeTasksViewSerializer(serializers.Serializer): return tasks +class ObfuscatedPasswordField(serializers.Field): + """ + Sends *** string instead of password in the clear + """ + + def to_representation(self, value): + return re.sub(".", "*", value) + + def to_internal_value(self, data): + return data + + class MailAccountSerializer(serializers.ModelSerializer): + password = ObfuscatedPasswordField() + class Meta: model = MailAccount depth = 1 @@ -708,6 +722,9 @@ class MailAccountSerializer(serializers.ModelSerializer): ] def update(self, instance, validated_data): + if "password" in validated_data: + if len(validated_data.get("password").replace("*", "")) == 0: + validated_data.pop("password") super().update(instance, validated_data) return instance