From bb23fe1a0f830c8dcf95ee3ed3f774563a90c55d Mon Sep 17 00:00:00 2001 From: Roger Gonzalez Date: Fri, 31 Jan 2025 12:34:10 -0300 Subject: [PATCH] Add password prompt and access token retrieval for user creation --- create-user.sh | 64 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 54 insertions(+), 10 deletions(-) diff --git a/create-user.sh b/create-user.sh index b7e4a57..b787d8e 100755 --- a/create-user.sh +++ b/create-user.sh @@ -9,27 +9,71 @@ if [[ ! $USERNAME =~ ^[a-zA-Z0-9_.-]+$ ]]; then 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=""; 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 monolith /usr/bin/create-account \ - -config /etc/dendrite/dendrite.yaml \ - -username "$USERNAME" \ - $ADMIN_FLAG +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 command was successful -if [ $? -eq 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 +# 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