Add password prompt and access token retrieval for user creation

This commit is contained in:
Roger Gonzalez 2025-01-31 12:34:10 -03:00
parent d998907add
commit bb23fe1a0f
Signed by: rogs
GPG Key ID: C7ECE9C6C36EC2E6

View File

@ -9,27 +9,71 @@ if [[ ! $USERNAME =~ ^[a-zA-Z0-9_.-]+$ ]]; then
exit 1 exit 1
fi 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 # Prompt for admin status
while true; do while true; do
read -p "Should this user be an admin? (y/n): " ADMIN_RESPONSE read -p "Should this user be an admin? (y/n): " ADMIN_RESPONSE
case $ADMIN_RESPONSE in case $ADMIN_RESPONSE in
[Yy]* ) ADMIN_FLAG="--admin"; break;; [Yy]* ) ADMIN_FLAG="--admin"; break;;
[Nn]* ) ADMIN_FLAG=""; break;; [Nn]* ) ADMIN_FLAG="--no-admin"; break;;
* ) echo "Please answer y or n.";; * ) echo "Please answer y or n.";;
esac esac
done done
# Docker command to create the user
echo "Creating user: $USERNAME" echo "Creating user: $USERNAME"
docker compose exec monolith /usr/bin/create-account \ docker compose exec -it synapse register_new_matrix_user \
-config /etc/dendrite/dendrite.yaml \ -u "$USERNAME" \
-username "$USERNAME" \ -p "$PASSWORD" \
$ADMIN_FLAG $ADMIN_FLAG \
-c /data/homeserver.yaml \
http://localhost:8008
# Check if the command was successful # Check if the user was created successfully
if [ $? -eq 0 ]; then if [ $? -ne 0 ]; then
echo "User \"$USERNAME\" created successfully!"
echo "Make sure to save this access token! You'll need it to enable double puppeting in the bridges."
else
echo "Error: Failed to create user" echo "Error: Failed to create user"
exit 1 exit 1
fi 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