generated from coop-cloud/example
Add `... api login` subcommand
This commit is contained in:
parent
68a4ae5758
commit
03b8fdd809
|
@ -0,0 +1,66 @@
|
||||||
|
sub_api_login() {
|
||||||
|
require_binary jq
|
||||||
|
|
||||||
|
if [ -n $1 ]; then
|
||||||
|
USERID="$1"
|
||||||
|
else
|
||||||
|
read -rp "User ID (format: @bot:example.com): " USERID
|
||||||
|
fi
|
||||||
|
if [ -n $2 ]; then
|
||||||
|
DISPLAY_NAME="$2"
|
||||||
|
else
|
||||||
|
read -rp "Display name (e.g. Matrix Bot): " DISPLAY_NAME
|
||||||
|
fi
|
||||||
|
if [ -n $3 ]; then
|
||||||
|
PASSWORD="$3"
|
||||||
|
else
|
||||||
|
read -rp "Password: " PASSWORD
|
||||||
|
fi
|
||||||
|
if [ -n $4 ]; then
|
||||||
|
USER_HOMESERVER="$4"
|
||||||
|
else
|
||||||
|
read -rp "User homeserver (e.g. matrix.example.com): " USER_HOMESERVER
|
||||||
|
fi
|
||||||
|
|
||||||
|
USERNAME=$(echo "${USERID/@/}" | cut -d':' -f1)
|
||||||
|
|
||||||
|
AUTH_DATA="{
|
||||||
|
\"identifier\": { \"type\": \"m.id.user\", \"user\": \"$USERNAME\" },
|
||||||
|
\"password\": \"$PASSWORD\",
|
||||||
|
\"type\": \"m.login.password\"
|
||||||
|
}"
|
||||||
|
|
||||||
|
debug "Auth data:
|
||||||
|
$AUTH_DATA"
|
||||||
|
|
||||||
|
AUTH_RESPONSE=$(curl -s -X POST --header 'Content-Type: application/json' -d "$AUTH_DATA" "https://$USER_HOMESERVER/_matrix/client/r0/login")
|
||||||
|
|
||||||
|
TOKEN=$(echo "$AUTH_RESPONSE" | jq -r '.access_token')
|
||||||
|
DEVICE_ID=$(echo "$AUTH_RESPONSE" | jq -r '.device_id')
|
||||||
|
|
||||||
|
CONFIGURE_DATA="{
|
||||||
|
\"UserId\": \"$USERID\",
|
||||||
|
\"AccessToken\": \"$TOKEN\",
|
||||||
|
\"HomeServerURL\": \"$DOMAIN\",
|
||||||
|
\"DeviceId\": \"LSWJCPGUAZ\",
|
||||||
|
\"Sync\": true,
|
||||||
|
\"AutoJoinRooms\": true,
|
||||||
|
\"DisplayName\": \"autono-bot\"
|
||||||
|
}"
|
||||||
|
|
||||||
|
debug "Auth data:
|
||||||
|
$CONFIGURE_DATA"
|
||||||
|
|
||||||
|
curl -s -X POST "https://$DOMAIN/admin/configureClient" --data-binary $CONFIGURE_DATA | jq
|
||||||
|
}
|
||||||
|
|
||||||
|
sub_api() {
|
||||||
|
CMD="$1"
|
||||||
|
if type "sub_api_$CMD" > /dev/null 2>&1; then
|
||||||
|
# shellcheck disable=SC2086
|
||||||
|
shift
|
||||||
|
"sub_api_$CMD" "$@"
|
||||||
|
else
|
||||||
|
error "Subcommand 'sub_api_$CMD' not found"
|
||||||
|
fi
|
||||||
|
}
|
Loading…
Reference in New Issue