2023-03-10 18:48:52 +00:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
|
|
|
BASE="http://localhost:8008"
|
|
|
|
USERNAME="tt_user"
|
|
|
|
PASSWORD=$(json -e 'p@s$w0rd')
|
|
|
|
|
|
|
|
ENDPOINT="$1"
|
|
|
|
|
2023-03-12 03:37:57 +00:00
|
|
|
: "${METH:=GET}"
|
|
|
|
|
2023-03-18 14:32:09 +00:00
|
|
|
register_payload() {
|
|
|
|
printf '{'
|
|
|
|
printf ' "auth": {'
|
|
|
|
printf ' "type": "m.login.dummy",'
|
|
|
|
printf ' "session": %s' "$(json -e "$session")"
|
|
|
|
printf ' },'
|
|
|
|
printf ' "username": %s,' "$(json -e "$USERNAME")"
|
|
|
|
printf ' "password": %s,' "$PASSWORD"
|
|
|
|
printf ' "inhibit_login": true '
|
|
|
|
printf '}'
|
|
|
|
}
|
2023-03-10 18:48:52 +00:00
|
|
|
|
2023-03-18 14:32:09 +00:00
|
|
|
login_payload() {
|
2023-03-10 18:48:52 +00:00
|
|
|
printf '{'
|
|
|
|
printf ' "identifier": {'
|
|
|
|
printf ' "type": "m.id.user",'
|
|
|
|
printf ' "user": %s' "$(json -e "$USERNAME")"
|
|
|
|
printf ' },'
|
|
|
|
printf ' "type": "m.login.password",'
|
|
|
|
printf ' "password": %s' "$PASSWORD"
|
|
|
|
printf '}'
|
2023-03-18 14:32:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Check if user is available. If it is, register it.
|
|
|
|
user_available=$(http "$BASE/_matrix/client/v3/register/available?username=$USERNAME" | json -s "available")
|
|
|
|
if [ "$user_available" = "true" ]; then
|
|
|
|
session=$(echo '{}' | http -X POST "$BASE/_matrix/client/v3/register" | json -s "session->@decode")
|
|
|
|
register_payload | http -X POST -d @- "$BASE/_matrix/client/v3/register" > /dev/null
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Log in
|
|
|
|
RESPONSE=$(login_payload | http -X POST -d @- "$BASE/_matrix/client/v3/login")
|
2023-03-15 13:36:49 +00:00
|
|
|
|
|
|
|
ACCESS_TOKEN=$(echo "$RESPONSE" | json -s "access_token->@decode")
|
2023-03-10 18:48:52 +00:00
|
|
|
|
|
|
|
if [ -z "$ACCESS_TOKEN" ]; then
|
2023-03-15 13:36:49 +00:00
|
|
|
echo "$RESPONSE" | json
|
2023-03-10 18:48:52 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Make request
|
2023-03-12 03:37:57 +00:00
|
|
|
if [ -n "$DATA" ]; then
|
|
|
|
http -X "$METH" -d "$DATA" -H "Authorization: Bearer $ACCESS_TOKEN" "${BASE}${ENDPOINT}" | json
|
|
|
|
else
|
|
|
|
http -X "$METH" -H "Authorization: Bearer $ACCESS_TOKEN" "${BASE}${ENDPOINT}" | json
|
|
|
|
fi
|
2023-03-10 18:48:52 +00:00
|
|
|
|
|
|
|
# Log out
|
|
|
|
http -X POST -H "Authorization: Bearer $ACCESS_TOKEN" \
|
2023-03-18 14:32:09 +00:00
|
|
|
"$BASE/_matrix/client/v3/logout/all" > /dev/null
|