Last active
January 2, 2020 13:25
-
-
Save wreiske/9e3f28900baa6ec82c0ac567e6f920d6 to your computer and use it in GitHub Desktop.
Change rocket.chat user passwords via API.
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
#!/bin/bash | |
# Simple example Rocket.Chat password changer for sysadmins | |
# Usage: ./reset_rocketchat_user_password.sh | |
# You can optionally store these here (NOT recommended) or pass via environment variables | |
# RC_ADMIN_AUTH_TOKEN=token | |
# RC_ADMIN_USER_ID=userid | |
# RC_SERVER_URL=https://chat.website.com | |
echo "Welcome!" | |
echo "To get started, please feed me your Rocket.Chat personal access token and userId." | |
echo "Not sure where to start? See https://rocket.chat/docs/developer-guides/rest-api/personal-access-tokens/" | |
# You can pass RC_ADMIN_AUTH_TOKEN as an environment variable to skip this step | |
if test -z "$RC_ADMIN_AUTH_TOKEN" | |
then | |
# Let's get the token | |
read -p "What's your auth token?: " RC_ADMIN_AUTH_TOKEN | |
else | |
echo "Yum! I got your access token from the environment. Thanks." | |
fi | |
# You can pass RC_ADMIN_USER_ID as an environment variable to skip this step | |
if test -z "$RC_ADMIN_USER_ID" | |
then | |
# Let's get the admin userid | |
read -p "What's your userid?: " RC_ADMIN_USER_ID | |
else | |
echo "Yum! I got your user id from the environment. Thanks." | |
fi | |
# You can pass RC_SERVER_URL as an environment variable to skip this step | |
if test -z "$RC_SERVER_URL" | |
then | |
# Let's get the admin userid | |
read -p "What's your server Url? (e.g. https://chat.example.com): " RC_SERVER_URL | |
else | |
echo "Yum! I got your server from the environment. Thanks." | |
fi | |
# You can pass RC_USERNAME as an environment variable to skip this step | |
if test -z "$RC_USERNAME" | |
then | |
# Let's get the username of the user we want to edit | |
read -p "What's the username of the user you'd like to edit?: " RC_USERNAME | |
else | |
echo "Yum! I got a user id from the environment. Thanks." | |
fi | |
RES_USER_LOOKUP=$(curl -s -H "X-Auth-Token: $RC_ADMIN_AUTH_TOKEN" \ | |
-H "X-User-Id: $RC_ADMIN_USER_ID" \ | |
"$RC_SERVER_URL/api/v1/users.info?username=$RC_USERNAME") | |
if [[ $RES_USER_LOOKUP == *"\"_id\":"* ]]; then | |
RC_USER_ID=$(echo $RES_USER_LOOKUP | sed -n 's|.*"_id":"\([^"]*\)".*|\1|p') | |
fi | |
if test -z "$RC_USER_ID" | |
then | |
echo "Unable to find that user. Sorry!" | |
echo "DEBUG: $RES_USER_LOOKUP" | |
exit 1 | |
fi | |
echo "User ID to change password for is: $RC_USER_ID" | |
# Let's get the password you want to change the user to | |
read -s -p "New User Password: " NEW_PASSWORD | |
echo | |
# Let's get the password you want to change the user to | |
read -s -p "New User Password (again): " NEW_PASSWORD_AGAIN | |
echo | |
if [ "$NEW_PASSWORD" != "$NEW_PASSWORD_AGAIN" ] | |
then | |
echo "Passwords don't match!" | |
exit 1 | |
fi | |
read -p "Require password change? (type true or false): " -i true -e REQUIRE_PASSWORD_CHANGE | |
echo | |
# Totally stolen 'are you sure' from https://stackoverflow.com/a/1885534 | |
read -p "Are you sure you want to continue? [Y/n]:" -i Y -e -n 1 -r | |
echo | |
if [[ $REPLY =~ ^[Yy]$ ]] | |
then | |
RES_PASSWORD_CHANGE=$(curl -s curl -H "X-Auth-Token: $RC_ADMIN_AUTH_TOKEN" \ | |
-H "X-User-Id: $RC_ADMIN_USER_ID" \ | |
-H "Content-type: application/json" \ | |
"$RC_SERVER_URL/api/v1/users.update" \ | |
-d "{\"userId\": \"$RC_USER_ID\", \"data\": { \"password\": \"$NEW_PASSWORD\", \"requirePasswordChange\": $REQUIRE_PASSWORD_CHANGE }}") | |
if [[ $RES_PASSWORD_CHANGE == *"\"success\":true"* ]]; then | |
echo "Changed password for user." | |
exit 0 | |
else | |
echo "There was an error changing the user's password." | |
echo "You may need to also ensure you have permissions enabled to change user passwords." | |
echo "See $RC_SERVER_URL/admin/permissions" | |
echo "DEBUG: $RES_PASSWORD_CHANGE" | |
exit 1 | |
fi | |
else | |
echo "Canceled." | |
fi |
Works like a treat, thanks!
Firstly, thanks a ton. This works well. Secondly, the print messages from line 33 and 42 should not be the same. (sorry for the nitpick) This does not change the actual functionality of the script itself.
Firstly, thanks a ton. This works well. Secondly, the print messages from line 33 and 42 should not be the same. (sorry for the nitpick) This does not change the actual functionality of the script itself.
Ahh! Thanks. Copy paste error. Fixed!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice ;-)
This presumes they don't remove the API as well....
Shame such hoop jumping is required.