Skip to content

Instantly share code, notes, and snippets.

@TruncatedDinoSour
Last active December 31, 2023 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TruncatedDinoSour/b3646071cc404a8ba8201f4950bfc031 to your computer and use it in GitHub Desktop.
Save TruncatedDinoSour/b3646071cc404a8ba8201f4950bfc031 to your computer and use it in GitHub Desktop.
user deactivation script for local admin management in matrix dendrite implementation
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""deactivate a user account on dendrite
( usage : `./deactivate.py <username of the user that you want deactivated>`,
itll prompt u for ur access token,
in element and schildichat at least you can get ur access token by going to
settings -> help & about -> advanced -> access token,
your account should be admin in dendrite ( `./bin/create-user ... -admin` ) )
note : change the homeserver ( `ari.lt` ) to yours in this script
----------------------------------------
UNLICENSE
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>"""
import random
import sys
from pprint import pprint
from typing import Dict
import requests
def main() -> int:
"""entry/main function"""
token: str = input("token : ")
localpart: str = sys.argv[1]
pw: str = f"{random.random() * 1024}{random.random() * 512}{random.random() * 256}"
s: requests.Session = requests.Session()
print(f"changing password to : {pw}")
pprint(
requests.post(
f"http://127.0.0.1:8008/_dendrite/admin/resetPassword/@{localpart}:ari.lt",
json={"password": pw, "logout_devices": True},
headers={"Authorization": f"Bearer {token}"},
).json()
)
input(
"if you continue now, the account will be deactivated, ctrl+c if you want to stop, press enter otherwise"
)
print("logging in as the user")
u: Dict[str, str] = s.post(
"http://127.0.0.1:8008/_matrix/client/v3/login",
json={
"type": "m.login.password",
"identifier": {
"type": "m.id.user",
"user": localpart,
},
"password": pw,
},
).json()
pprint(u)
print("deactivating")
pprint(
s.post(
"http://127.0.0.1:8008/_matrix/client/v3/account/deactivate",
json={
"auth": {
"type": "m.login.password",
"user": localpart,
"password": pw,
},
},
headers={"Authorization": f"Bearer {u['access_token']}"},
).json()
)
return 0
if __name__ == "__main__":
assert main.__annotations__.get("return") is int, "main() should return an integer"
raise SystemExit(main())
@TruncatedDinoSour
Copy link
Author

TruncatedDinoSour commented Dec 29, 2023

( usage : ./deactivate.py <username of the user that you want deactivated>, itll prompt u for ur access token, in element and schildichat at least you can get ur access token by going to settings -> help & about -> advanced -> access token, your account should be admin in dendrite ( ./bin/create-user ... -admin ) )

note : change the homeserver ( ari.lt ) to yours in https://gist.github.com/TruncatedDinoSour/b3646071cc404a8ba8201f4950bfc031#file-deactivate-py-L63

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment