diff sat_pubsub/pam.py @ 480:23a51b139024

pam: handle errors in onSubscribeResult
author Goffi <goffi@goffi.org>
date Mon, 30 May 2022 16:37:14 +0200
parents b544109ab4c4
children
line wrap: on
line diff
--- a/sat_pubsub/pam.py	Wed May 11 14:47:53 2022 +0200
+++ b/sat_pubsub/pam.py	Mon May 30 16:37:14 2022 +0200
@@ -71,18 +71,32 @@
             return jid.JID(iq_elt["from"])
 
     def onSubscribeResult(self, iq_req_elt, iq_result_elt, pam_iq_elt):
-        destinee_jid = jid.JID(iq_req_elt["from"])
-        sender_jid = jid.JID(iq_req_elt["to"])
-        message_elt = domish.Element((None, "message"))
-        message_elt["to"] = destinee_jid.userhost()
-        message_elt["from"] = destinee_jid.userhost()
-        # XXX: we explicitely store the notification to be sure that all clients get it
-        message_elt.addElement(("urn:xmpp:hints", "store"))
-        notify_elt = message_elt.addElement((NS_PAM, "notify"))
-        notify_elt["service"] = sender_jid.full()
-        notify_elt.addChild(iq_result_elt.pubsub.subscription)
-        self.backend.privilege.sendMessage(message_elt)
-        pam_iq_result_elt = xmlstream.toResponse(pam_iq_elt, 'result')
+        subscription_elt = iq_result_elt.pubsub.subscription
+        if subscription_elt is not None:
+            destinee_jid = jid.JID(iq_req_elt["from"])
+            sender_jid = jid.JID(iq_req_elt["to"])
+            message_elt = domish.Element((None, "message"))
+            message_elt["to"] = destinee_jid.userhost()
+            message_elt["from"] = destinee_jid.userhost()
+            # XXX: we explicitely store the notification to be sure that all clients get it
+            message_elt.addElement(("urn:xmpp:hints", "store"))
+            notify_elt = message_elt.addElement((NS_PAM, "notify"))
+            notify_elt["service"] = sender_jid.full()
+            notify_elt.addChild(subscription_elt)
+            self.backend.privilege.sendMessage(message_elt)
+            pam_iq_result_elt = xmlstream.toResponse(pam_iq_elt, 'result')
+        else:
+            # no <subscription> element, this must be an error
+            error_elt = iq_result_elt.error
+            if error_elt is None:
+                log.msg(f"Invalid reply received: {iq_result_elt.toXml()}")
+                error_elt = jabber_error.StanzaError(
+                    "service-unavailable",
+                    "received invalid reply from external pubsub service"
+                ).getElement()
+            pam_iq_result_elt = xmlstream.toResponse(pam_iq_elt, 'error')
+            pam_iq_result_elt.addChild(error_elt)
+
         self.xmlstream.send(pam_iq_result_elt)
 
     async def onSubRequest(self, from_jid, iq_elt, subscribe=True):