Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user registration and deactivation #112

Open
djotaku opened this issue Mar 29, 2020 · 13 comments
Open

Add user registration and deactivation #112

djotaku opened this issue Mar 29, 2020 · 13 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@djotaku
Copy link
Contributor

djotaku commented Mar 29, 2020

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

@poljar poljar added enhancement New feature or request good first issue Good for newcomers labels Mar 30, 2020
@djotaku
Copy link
Contributor Author

djotaku commented Mar 30, 2020

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

@poljar
Copy link
Collaborator

poljar commented Mar 31, 2020

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.

@djotaku
Copy link
Contributor Author

djotaku commented Apr 5, 2020

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.

@poljar
Copy link
Collaborator

poljar commented Apr 5, 2020

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.

@djotaku
Copy link
Contributor Author

djotaku commented Apr 6, 2020

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?

@poljar
Copy link
Collaborator

poljar commented Apr 7, 2020

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.

@djotaku
Copy link
Contributor Author

djotaku commented Apr 7, 2020

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

@poljar
Copy link
Collaborator

poljar commented Apr 7, 2020

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

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

@djotaku
Copy link
Contributor Author

djotaku commented Apr 7, 2020

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

@aditya006
Copy link

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

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

@aditya006
Copy link

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

@createcandle
Copy link

createcandle commented Apr 1, 2022

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 :-)

@sblondon
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants