[FIX] Put back tt
All checks were successful
Testing pushed commits / compile-all (push) Successful in 38s

This commit is contained in:
lda 2023-11-03 12:24:32 +01:00
parent ed712c2e5f
commit 263bcb9e16

61
tools/bin/tt Executable file
View file

@ -0,0 +1,61 @@
#!/usr/bin/env sh
BASE="http://localhost:8008"
USERNAME="tt_user"
PASSWORD=$(json -e 'p@s$w0rd')
ENDPOINT="$1"
: "${METH:=GET}"
register_payload() {
printf '{'
printf ' "auth": {'
printf ' "type": "m.login.dummy",'
printf ' "session": %s' "$(json -e "$1")"
printf ' },'
printf ' "username": %s,' "$(json -e "$USERNAME")"
printf ' "password": %s,' "$PASSWORD"
printf ' "inhibit_login": true '
printf '}'
}
login_payload() {
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 '}'
}
# 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=$(http -X POST -d '{}' "$BASE/_matrix/client/v3/register" | json -s "session->@decode")
register_payload "$session" | 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")
ACCESS_TOKEN=$(echo "$RESPONSE" | json -s "access_token->@decode")
if [ -z "$ACCESS_TOKEN" ]; then
echo "Failed to log in."
echo "$RESPONSE" | json
exit 1
fi
# Make request
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
# Log out
http -X POST -H "Authorization: Bearer $ACCESS_TOKEN" \
"$BASE/_matrix/client/v3/logout/all" > /dev/null