From 79eebe68e2f30b89facf510483e829d174bf5222 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 14 Feb 2023 10:20:40 +0100 Subject: [PATCH] add registration manager (change password + delete account) --- .../conversations/android/xmpp/Managers.java | 2 + .../xmpp/manager/RegistrationManager.java | 41 +++++++++++++++++++ .../android/xmpp/model/register/Password.java | 10 +++++ .../android/xmpp/model/register/Register.java | 8 ++++ .../android/xmpp/model/register/Remove.java | 10 +++++ .../android/xmpp/model/register/Username.java | 12 ++++++ 6 files changed, 83 insertions(+) create mode 100644 src/main/java/im/conversations/android/xmpp/manager/RegistrationManager.java create mode 100644 src/main/java/im/conversations/android/xmpp/model/register/Password.java create mode 100644 src/main/java/im/conversations/android/xmpp/model/register/Remove.java create mode 100644 src/main/java/im/conversations/android/xmpp/model/register/Username.java diff --git a/src/main/java/im/conversations/android/xmpp/Managers.java b/src/main/java/im/conversations/android/xmpp/Managers.java index 042a84287..c720f963d 100644 --- a/src/main/java/im/conversations/android/xmpp/Managers.java +++ b/src/main/java/im/conversations/android/xmpp/Managers.java @@ -17,6 +17,7 @@ import im.conversations.android.xmpp.manager.PepManager; import im.conversations.android.xmpp.manager.PresenceManager; import im.conversations.android.xmpp.manager.PubSubManager; import im.conversations.android.xmpp.manager.ReceiptManager; +import im.conversations.android.xmpp.manager.RegistrationManager; import im.conversations.android.xmpp.manager.RosterManager; import im.conversations.android.xmpp.manager.StanzaIdManager; @@ -40,6 +41,7 @@ public final class Managers { .put(PresenceManager.class, new PresenceManager(context, connection)) .put(PubSubManager.class, new PubSubManager(context, connection)) .put(ReceiptManager.class, new ReceiptManager(context, connection)) + .put(RegistrationManager.class, new RegistrationManager(context, connection)) .put(RosterManager.class, new RosterManager(context, connection)) .put(StanzaIdManager.class, new StanzaIdManager(context, connection)) .build(); diff --git a/src/main/java/im/conversations/android/xmpp/manager/RegistrationManager.java b/src/main/java/im/conversations/android/xmpp/manager/RegistrationManager.java new file mode 100644 index 000000000..8b88c898c --- /dev/null +++ b/src/main/java/im/conversations/android/xmpp/manager/RegistrationManager.java @@ -0,0 +1,41 @@ +package im.conversations.android.xmpp.manager; + +import android.content.Context; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.MoreExecutors; +import im.conversations.android.xmpp.XmppConnection; +import im.conversations.android.xmpp.model.register.Register; +import im.conversations.android.xmpp.model.register.Remove; +import im.conversations.android.xmpp.model.stanza.Iq; + +public class RegistrationManager extends AbstractManager { + + public RegistrationManager(Context context, XmppConnection connection) { + super(context, connection); + } + + public ListenableFuture setPassword(final String password) { + final var account = getAccount(); + final var iq = new Iq(Iq.Type.SET); + final var register = iq.addExtension(new Register()); + register.addUsername(account.address.getEscapedLocal()); + register.addPassword(password); + return Futures.transform( + connection.sendIqPacket(iq), r -> null, MoreExecutors.directExecutor()); + } + + public ListenableFuture unregister() { + final var iq = new Iq(Iq.Type.SET); + final var register = iq.addExtension(new Register()); + register.addExtension(new Remove()); + return Futures.transform( + connection.sendIqPacket(iq), r -> null, MoreExecutors.directExecutor()); + } + + // TODO support registration + // 3 possible responses: + // 1) username + password + // 2) Captcha as shown here: https://xmpp.org/extensions/xep-0158.html#register + // 3) Redirection as show here: https://xmpp.org/extensions/xep-0077.html#redirect +} diff --git a/src/main/java/im/conversations/android/xmpp/model/register/Password.java b/src/main/java/im/conversations/android/xmpp/model/register/Password.java new file mode 100644 index 000000000..9da687c21 --- /dev/null +++ b/src/main/java/im/conversations/android/xmpp/model/register/Password.java @@ -0,0 +1,10 @@ +package im.conversations.android.xmpp.model.register; + +import im.conversations.android.xmpp.model.Extension; + +public class Password extends Extension { + + public Password() { + super(Password.class); + } +} diff --git a/src/main/java/im/conversations/android/xmpp/model/register/Register.java b/src/main/java/im/conversations/android/xmpp/model/register/Register.java index d16d0ab72..caab0c353 100644 --- a/src/main/java/im/conversations/android/xmpp/model/register/Register.java +++ b/src/main/java/im/conversations/android/xmpp/model/register/Register.java @@ -9,4 +9,12 @@ public class Register extends Extension { public Register() { super(Register.class); } + + public void addUsername(final String username) { + this.addExtension(new Username()).setContent(username); + } + + public void addPassword(final String password) { + this.addExtension(new Password()).setContent(password); + } } diff --git a/src/main/java/im/conversations/android/xmpp/model/register/Remove.java b/src/main/java/im/conversations/android/xmpp/model/register/Remove.java new file mode 100644 index 000000000..bbd327bfd --- /dev/null +++ b/src/main/java/im/conversations/android/xmpp/model/register/Remove.java @@ -0,0 +1,10 @@ +package im.conversations.android.xmpp.model.register; + +import im.conversations.android.xmpp.model.Extension; + +public class Remove extends Extension { + + public Remove() { + super(Remove.class); + } +} diff --git a/src/main/java/im/conversations/android/xmpp/model/register/Username.java b/src/main/java/im/conversations/android/xmpp/model/register/Username.java new file mode 100644 index 000000000..bc93581b6 --- /dev/null +++ b/src/main/java/im/conversations/android/xmpp/model/register/Username.java @@ -0,0 +1,12 @@ +package im.conversations.android.xmpp.model.register; + +import im.conversations.android.annotation.XmlElement; +import im.conversations.android.xmpp.model.Extension; + +@XmlElement +public class Username extends Extension { + + public Username() { + super(Username.class); + } +}