Skip to content

Instantly share code, notes, and snippets.

@ritiek
Created May 24, 2024 14:55
Show Gist options
  • Save ritiek/16004f92395aa69d8701786036558def to your computer and use it in GitHub Desktop.
Save ritiek/16004f92395aa69d8701786036558def to your computer and use it in GitHub Desktop.
Register on Matrix homeserver using Registration Shared Secret
# 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