1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env bash
#
# Scripted atproto OAuth sign-in for the iOS Simulator (plan 0038 I5).
#
# Reads ~/.aliceoa_bsky_social_source exactly once, in this command, and never
# prints, echoes, or logs its values. The authorization URL travels only from
# the app container to the headless browser; it is never posted. Output is the
# sign-in result and the granted scope names.
#
# Usage: scripts/sim-signin.sh [device] [app path]
# DEVICE=iPhone 17e scripts/sim-signin.sh
#
set -euo pipefail
lock="${TMPDIR:-/tmp}/atirc-sim-signin.lock"
if ! mkdir "$lock" 2>/dev/null; then
echo "FAIL: another sim-signin is running ($lock); remove it if stale" >&2
exit 1
fi
trap 'rmdir "$lock" 2>/dev/null || true' EXIT
CREDS="${ATIRC_CREDS:-$HOME/.aliceoa_bsky_social_source}"
ACCOUNT="${ATIRC_ACCOUNT:-ALICE}"
DEVICE="${1:-${DEVICE:-iPhone 17e}}"
BUNDLE_ID="com.fedfork.irc.AT-IRC"
APP="${2:-${APP:-.build/Build/Products/Debug-iphonesimulator/AT-IRC.app}}"
SOCIALWEB="${SOCIALWEB_SRC:-/Users/johnandersen777/src/linux-kcp/socialweb-computer-kcp}"
CHROME="${CHROME:-/Applications/Google Chrome.app/Contents/MacOS/Google Chrome}"
LOG="${TMPDIR:-/tmp}/atirc-oauth-signin.$$.log"
cd "$(dirname "$0")/.."
# The one read of the credential file in this shell.
source "$CREDS"
handle_var="${ACCOUNT}_ATPROTO_HANDLE"
handle="${!handle_var:?$handle_var is missing from $CREDS}"
[ -d "$APP" ] || { echo "FAIL: $APP does not exist; build first" >&2; exit 1; }
[ -f "$(dirname "$0")/sim-oauth.ts" ] || { echo "FAIL: Scripts/sim-oauth.ts missing" >&2; exit 1; }
xcrun simctl bootstatus "$DEVICE" -b >/dev/null 2>&1 || true
xcrun simctl terminate "$DEVICE" "$BUNDLE_ID" >/dev/null 2>&1 || true
xcrun simctl uninstall "$DEVICE" "$BUNDLE_ID" >/dev/null 2>&1 || true
# A persisted account would skip LoginView and the auto sign-in.
xcrun simctl keychain "$DEVICE" reset >/dev/null 2>&1 || true
xcrun simctl install "$DEVICE" "$APP"
data="$(xcrun simctl get_app_container "$DEVICE" "$BUNDLE_ID" data)"
url_file="$data/Documents/.atirc-signin-url"
result_file="$data/Documents/.atirc-signin-result"
rm -f "$url_file" "$result_file"
SIMCTL_CHILD_ATIRC_SIGNIN_HANDLE="$handle" \
xcrun simctl launch --terminate-running-process "$DEVICE" "$BUNDLE_ID" >/dev/null
for _ in $(seq 1 120); do
[ -s "$url_file" ] && break
sleep 0.5
done
if [ ! -s "$url_file" ]; then
echo "FAIL: the app did not write an authorization URL (see the app log)" >&2
exit 1
fi
url="$(cat "$url_file")"
# The browser script reads the same credential file itself; no value is passed
# on the command line.
if ! deno run -A --no-lock "$(dirname "$0")/sim-oauth.ts" \
--creds "$CREDS" --account "$ACCOUNT" --url "$url" --chrome "$CHROME" \
>"$LOG" 2>&1; then
echo "FAIL: browser sign-in failed (details in $LOG; it contains no credentials or URLs)" >&2
exit 1
fi
for _ in $(seq 1 180); do
[ -s "$result_file" ] && break
sleep 0.5
done
if [ ! -s "$result_file" ]; then
echo "FAIL: the app did not finish sign-in (see the app log)" >&2
exit 1
fi
result="$(cat "$result_file")"
rm -f "$url_file" "$result_file" "$LOG"
if printf '%s' "$result" | grep -q '^ok '; then
echo "$result" | sed -n '1p'
scopes="$(printf '%s\n' "$result" | sed -n 's/^scope //p')"
count="$(printf '%s' "$scopes" | wc -w | tr -d ' ')"
echo "granted scopes: $count"
printf '%s' "$scopes" | tr ' ' '\n' | sed '/^$/d' | sed 's/^/ /'
else
echo "FAIL: sign-in failed" >&2
printf '%s\n' "$result" | sed -n 's/^error / /p' >&2
exit 1
fi