XEP-0077: API changes

This commit is contained in:
mathieui 2021-02-18 20:24:37 +01:00
parent 0b3233a6e8
commit 504067d5a8
3 changed files with 60 additions and 12 deletions

View file

@ -8,6 +8,53 @@ XEP-0077: In-Band Registration
:members: :members:
:exclude-members: session_bind, plugin_init, plugin_end :exclude-members: session_bind, plugin_init, plugin_end
Internal APi methods
--------------------
The API here is made to allow components to manage registered users.
The default handlers make use of the plugin options and store users
in memory.
.. glossary::
user_get
- **jid**: unused
- **node**: unused
- **ifrom**: who the request is coming from
- **args**: :class:`~.Iq` registration request.
- **returns**: ``dict`` containing user data or None.
Get user data for a user.
user_validate
- **jid**: unused
- **node**: unused
- **ifrom**: who the request is coming from
- **args**: :class:`~.Iq` registration request, 'register' payload.
- **raises**: ValueError if some fields are invalid
Validate form fields and save user data.
user_remove
- **jid**: unused
- **node**: unused
- **ifrom**: who the request is coming from
- **args**: :class:`~.Iq` registration removal request.
- **raises**: KeyError if the user is not found.
Remove a user from the store.
make_registration_form
- **jid**: unused
- **node**: unused
- **ifrom**: who the request is coming from
- **args**: :class:`~.Iq` registration request.
- **raises**: KeyError if the user is not found.
Return an :class:`~.Iq` reply for the request, with a form and
options set. By default, use ``form_fields`` and ``form_instructions``
plugin config options.
Stanza elements Stanza elements
--------------- ---------------

View file

@ -106,10 +106,9 @@ class XEP_0077(BasePlugin):
def _user_remove(self, jid, node, ifrom, iq): def _user_remove(self, jid, node, ifrom, iq):
return self._user_store.pop(iq["from"].bare) return self._user_store.pop(iq["from"].bare)
def _make_registration_form(self, jid, node, ifrom, iq: Iq): async def _make_registration_form(self, jid, node, ifrom, iq: Iq):
reg = iq["register"] reg = iq["register"]
user = self.api["user_get"](None, None, None, iq) user = await self.api["user_get"](None, None, iq['from'], iq)
if user is None: if user is None:
user = {} user = {}
@ -135,11 +134,11 @@ class XEP_0077(BasePlugin):
async def _handle_registration(self, iq: Iq): async def _handle_registration(self, iq: Iq):
if iq["type"] == "get": if iq["type"] == "get":
self._send_form(iq) await self._send_form(iq)
elif iq["type"] == "set": elif iq["type"] == "set":
if iq["register"]["remove"]: if iq["register"]["remove"]:
try: try:
self.api["user_remove"](None, None, iq["from"], iq) await self.api["user_remove"](None, None, iq["from"], iq)
except KeyError: except KeyError:
_send_error( _send_error(
iq, iq,
@ -168,7 +167,7 @@ class XEP_0077(BasePlugin):
return return
try: try:
self.api["user_validate"](None, None, iq["from"], iq["register"]) await self.api["user_validate"](None, None, iq["from"], iq["register"])
except ValueError as e: except ValueError as e:
_send_error( _send_error(
iq, iq,
@ -182,8 +181,8 @@ class XEP_0077(BasePlugin):
reply.send() reply.send()
self.xmpp.event("user_register", iq) self.xmpp.event("user_register", iq)
def _send_form(self, iq): async def _send_form(self, iq):
reply = self.api["make_registration_form"](None, None, iq["from"], iq) reply = await self.api["make_registration_form"](None, None, iq["from"], iq)
reply.send() reply.send()
def _force_registration(self, event): def _force_registration(self, event):

View file

@ -91,7 +91,9 @@ class TestRegistration(SlixTest):
self.send("<iq type='result' id='reg2' from='shakespeare.lit' to='bill@shakespeare.lit/globe'/>") self.send("<iq type='result' id='reg2' from='shakespeare.lit' to='bill@shakespeare.lit/globe'/>")
pseudo_iq = self.xmpp.Iq() pseudo_iq = self.xmpp.Iq()
pseudo_iq["from"] = "bill@shakespeare.lit/globe" pseudo_iq["from"] = "bill@shakespeare.lit/globe"
user = self.xmpp["xep_0077"].api["user_get"](None, None, None, pseudo_iq) fut = self.xmpp.wrap(self.xmpp["xep_0077"].api["user_get"](None, None, None, pseudo_iq))
self.run_coro(fut)
user = fut.result()
self.assertEqual(user["username"], "bill") self.assertEqual(user["username"], "bill")
self.assertEqual(user["password"], "Calliope") self.assertEqual(user["password"], "Calliope")
self.recv( self.recv(