40 lines
2.1 KiB
Bash
Executable file
40 lines
2.1 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
# Shows that an OPEN /v0/books/{id}/events stream keeps serving after the session that opened it
|
|
# has been revoked: authentication happens once, in auth.Authenticator.Require, and httpapi.pump
|
|
# never looks at the session again.
|
|
#
|
|
# Needs: a running dev-login tmplatformd, its DSN, and a seeded book.
|
|
# Usage: sse-outlives-revocation.sh <base-url> <dsn> [cookie-name]
|
|
set -u
|
|
B=${1:-http://127.0.0.1:8102}
|
|
DSN=${2:?dsn}
|
|
N=${3:-__Host-tm_session}
|
|
PSQL=${PSQL:-$HOME/.local/pgsql/bin/psql}
|
|
DB=$(printf '%s' "$DSN" | sed -n 's#.*/\([a-zA-Z0-9_]*\)?.*#\1#p')
|
|
export PGHOST=/tmp PGPORT=55433 PGUSER=postgres
|
|
|
|
BOOK=$("$PSQL" -d "$DB" -Atc "select id from books limit 1")
|
|
[ -z "$BOOK" ] && { echo "no book: seed the stand first"; exit 1; }
|
|
# A book "at rest" ends its stream with `end` within a poll tick; keep it live so the connection
|
|
# is the thing under test, not the book.
|
|
"$PSQL" -d "$DB" -Atc "update books set status='parsing' where id='$BOOK'" >/dev/null
|
|
|
|
TOK=$(curl -s --noproxy '*' -i -X POST "$B/auth/dev-login" | sed -n "s/^Set-Cookie: ${N}=\([^;]*\).*/\1/p")
|
|
echo "book=$BOOK session=${TOK:0:10}..."
|
|
OUT=$(mktemp)
|
|
# Open the stream and keep it open for 30s, timestamping every line the server sends.
|
|
( curl -s --noproxy '*' -N --max-time 30 -H "Cookie: ${N}=${TOK}" "$B/v0/books/$BOOK/events" \
|
|
| while IFS= read -r l; do echo "$(date +%s) $l"; done > "$OUT" ) &
|
|
PUMP=$!
|
|
sleep 3
|
|
echo "-- revoking the session (POST /auth/logout) --"
|
|
curl -s --noproxy '*' -o /dev/null -w 'logout=%{http_code}\n' -X POST -H "Cookie: ${N}=${TOK}" -H 'X-TM-Client: probe' "$B/auth/logout"
|
|
"$PSQL" -d "$DB" -Atc "select 'sessions revoked: '||count(*) from sessions where revoked_at is not null"
|
|
echo "a NEW request with the same cookie: $(curl -s --noproxy '*' -o /dev/null -w '%{http_code}' -H "Cookie: ${N}=${TOK}" "$B/v0/books")"
|
|
echo "-- watching the already-open stream for 25 more seconds --"
|
|
wait $PUMP
|
|
echo "--- stream transcript (unix ts + line) ---"
|
|
cat "$OUT"
|
|
FIRST=$(head -1 "$OUT" | cut -d' ' -f1); LAST=$(tail -1 "$OUT" | cut -d' ' -f1)
|
|
echo "--- stream stayed open for $((LAST-FIRST))s across the revocation ---"
|
|
rm -f "$OUT"
|