Created
May 24, 2024 14:55
-
-
Save ritiek/16004f92395aa69d8701786036558def to your computer and use it in GitHub Desktop.
Register on Matrix homeserver using Registration Shared Secret
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Referred from: | |
# https://element-hq.github.io/synapse/latest/admin_api/register_api.html | |
import requests | |
import hmac | |
import hashlib | |
import json | |
# Fill these variables in. | |
DOMAIN = "example.com" | |
PORT = 8008 | |
REGISTRATION_SHARED_SECRET = "super_secret_registration_shared_secret" | |
USERNAME = "mee" | |
DISPLAY_NAME = "It's me!" | |
PASSWORD = "super_secret_password" | |
IS_ADMIN = False | |
USER_TYPE = None | |
def generate_mac(nonce, user, password, admin=False, user_type=None): | |
mac = hmac.new( | |
key=REGISTRATION_SHARED_SECRET.encode('utf8'), | |
digestmod=hashlib.sha1, | |
) | |
mac.update(nonce.encode('utf8')) | |
mac.update(b"\x00") | |
mac.update(user.encode('utf8')) | |
mac.update(b"\x00") | |
mac.update(password.encode('utf8')) | |
mac.update(b"\x00") | |
mac.update(b"admin" if admin else b"notadmin") | |
if user_type: | |
mac.update(b"\x00") | |
mac.update(user_type.encode('utf8')) | |
return mac.hexdigest() | |
url = f"https://{DOMAIN}:{PORT}/_synapse/admin/v1/register" | |
response = requests.get(url) | |
registration_info = response.json() | |
nonce = registration_info["nonce"] | |
mac = generate_mac(nonce, USERNAME, PASSWORD, admin=IS_ADMIN, user_type=USER_TYPE) | |
create_user = { | |
"nonce": nonce, | |
"username": USERNAME, | |
"displayname": DISPLAY_NAME, | |
"password":PASSWORD, | |
"admin": IS_ADMIN, | |
"mac": mac, | |
} | |
response = requests.post(url, json=create_user) | |
content = response.json() | |
print(json.dumps(content, indent=2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment