synapse-docker-bridges/create-user.sh

80 lines
2.1 KiB
Bash
Executable File

#!/bin/bash
# Prompt for username
read -p "Enter username: " USERNAME
# Validate username
if [[ ! $USERNAME =~ ^[a-zA-Z0-9_.-]+$ ]]; then
echo "Error: Username can only contain letters, numbers, underscores, dots, and hyphens"
exit 1
fi
# Prompt for password
read -s -p "Enter password: " PASSWORD
echo
read -s -p "Confirm password: " PASSWORD_CONFIRM
echo
# Validate password match
if [ "$PASSWORD" != "$PASSWORD_CONFIRM" ]; then
echo "Error: Passwords do not match."
exit 1
fi
# Prompt for admin status
while true; do
read -p "Should this user be an admin? (y/n): " ADMIN_RESPONSE
case $ADMIN_RESPONSE in
[Yy]* ) ADMIN_FLAG="--admin"; break;;
[Nn]* ) ADMIN_FLAG="--no-admin"; break;;
* ) echo "Please answer y or n.";;
esac
done
# Docker command to create the user
echo "Creating user: $USERNAME"
docker compose exec -it synapse register_new_matrix_user \
-u "$USERNAME" \
-p "$PASSWORD" \
$ADMIN_FLAG \
-c /data/homeserver.yaml \
http://localhost:8008
# Check if the user was created successfully
if [ $? -ne 0 ]; then
echo "Error: Failed to create user"
exit 1
fi
# Authenticate the user to get an access token
echo "Fetching access token for user: $USERNAME"
# Use curl to make a POST request to the Synapse /login endpoint
RESPONSE=$(curl -s -X POST http://localhost:8008/_matrix/client/v3/login \
-H "Content-Type: application/json" \
-d '{
"type": "m.login.password",
"user": "'"$USERNAME"'",
"password": "'"$PASSWORD"'"
}')
# Extract the access token using grep and sed
ACCESS_TOKEN=$(echo "$RESPONSE" | grep -o '"access_token":"[^"]*"' | sed 's/"access_token":"\(.*\)"/\1/')
# Check if the access token was retrieved
if [[ -z "$ACCESS_TOKEN" ]]; then
echo
echo
echo
echo "Error: Failed to retrieve access token."
echo "Response from server: $RESPONSE"
exit 1
else
echo
echo
echo
echo "User \"$USERNAME\" created successfully!"
echo "Access Token: $ACCESS_TOKEN"
echo "Make sure to save this access token! You'll need it to enable double puppeting in the bridges."
fi