matrix-nio / matrix-nio

A Python Matrix client library, designed according to sans I/O (http://sans-io.readthedocs.io/) principles

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add user registration and deactivation

djotaku opened this issue · comments

I don't see any functions for either in the documentation.

Since you've marked as a good first issue, I'd like to volunteer to tackle it myself.

The way you start adding this is to add a definition here.

After that you'll need to write a json schema for the response here.

A response type will be needed here.

After that you hook up the AsyncClient to send out the requests defined in api.py and tell it to parse it as the response defined in responses.py like here.

I think registering gives you an access token so you'll probably need to handle the response like a login.

Before working on the deactivation, I wanted to be sure I fully understood the code I'd worked on and the examples I had based it on in your code.

Based on your documentation I put in the following code:

import asyncio
from nio import AsyncClient


async def main():
    client = AsyncClient("https://matrix.ericmesa.com", "@username:matrix.ericmesa.com")
    await client.login(password="password")
    await client.close()

asyncio.get_event_loop().run_until_complete(main())

And no matter whether I put in registered names or made-up names, I would not get any output to the terminal or the Pycharm runner. But if I understood responses.py, I should have had an output like: "Logged in as username, device id: something. Right? What am I doing wrong? This is with Python 3.7.

await client.login(password="password") returns a response object, if you print that object out you should get the line you're expecting.

The lib has a lobgook based logging setup, but it won't print out anything by default.

Question for the deactivation. I did a bunch of tests and basically what I need to provide to the server is:

curl --insecure -XPOST -H "Authorization: Bearer An_Admin_User_Token" -H "Content-Type: application/json" -d '{}' "https://server/_matrix/client/r0/admin/deactivate/%40UserName%3Aserver"

So:

  1. Is there a way in matrix-nio to put admin user tokens into the header?
  2. Does the matrix-nio code already handle correct formatting of the username in the URL?

There is no support to use the Authorization header for now, but putting the access token into the query string should work as usual.

Yes, all URLs should already be escaped.

By query string do you mean the part in the the JSON?

No, the part that gets into the URL after the question mark separator.

https://en.wikipedia.org/wiki/Query_string

ah, OK. Will give this a shot over the next couple days.

when I try to register a new user I get RegisterErrorResponse: unknown error

client = AsyncClient("https://matrix.org") response = await client.register("user","password")

I think its because of the auth issue. It does not ask me for my email or ask if I am a robot? which it does if I try to signup manually through element

Hmm, I wonder if I'm running into this too.

I tried to find out what was going on with the unknown error response. I added some print in the library. I found that the transport_resp in async_client.py is quite helpful:
transport_resp:<ClientResponse(https://REDACTED/_matrix/client/r0/register) [404 Not Found]>

I also tried it with matrix.org as the server, and saw:

! transport_resp:<ClientResponse(https://matrix.org/_matrix/client/r0/register) [401 Unauthorized]>

But even then I was greeted with RegisterErrorResponse: unknown error

I kept trying, until I managed to find a server what worked! So it does work, it's just that most servers give a 401 response.

I think its because of the auth issue. It does not ask me for my email or ask if I am a robot?

Yes, it probably all depends on the server. Checking what type of manual acount creation is available is probably a good suggestion to people struggling with this issue, but I also found servers that allowed the creation of these basic accounts in the UI, just not via the API.

My thoughts:

  • Perhaps the library could pass along some more descriptive responses in case of 404 and 401 error?
  • Is it possible to add an example in the documentation? That might help people get the sense that the code works, it's just that most servers don't support it.

// I kept trying public servers. I also spotted a 502 Bad Gateway. And I even saw actual useful error messages too, such as M_UNRECOGNIZED and M_FORBIDDEN :-)

The correct curl request to deactivate user when authenticated by an access_token in a query string seems to be:

$ curl --insecure -XPOST -H "Content-Type: application/json" -d '{}' "https://DOMAIN/_synapse/admin/v1/deactivate/%40USER%3ADOMAIN?access_token=s....C"
{"errcode":"M_UNKNOWN","error":"Can only deactivate local users"}

I still have an error but it's better than the /_matrix/... URL:

$ curl --insecure -XPOST -H "Content-Type: application/json" -d '{}' "https://DOMAIN/_matrix/client/r0/deactivate/%40USER%3ADOMAIN?access_token=s...C"
{"errcode":"M_UNRECOGNIZED","error":"Unrecognized request"}

The first command fits the deactivation request implementation in synapse-admin.