#!/usr/bin/env bash # Local development only. Generates a fresh access token from the local # Stalwart dev container (see docker-compose.yml) and writes it to # .env.development.local (gitignored). Bash equivalent of dev-token.ps1, for # non-Windows shells (and AI agents without PowerShell). # # Requires scripts/dev-server-init.sh to have been run once first (creates # the "devadmin" account this script authenticates as — the # STALWART_RECOVERY_ADMIN account is break-glass only and always issues # fixed 1h tokens regardless of server config, so it can't honor a custom # duration). # # Usage: dev-token.sh [duration_seconds] [api_base_url] # dev-token.sh # 3 hour token (server default, see dev-server-init.sh) # dev-token.sh 1800 # 30 minute token set -euo pipefail DURATION_SECONDS="${1:-10800}" API_BASE_URL="${2:-http://localhost:8080}" DEVADMIN_ACCOUNT="devadmin@example.org" DEVADMIN_SECRET="DevAdminPass123!" ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" if ! date -u -d "+1 minute" +"%Y-%m-%dT%H:%M:%SZ" >/dev/null 2>&1; then EXPIRES_AT=$(date -u -v+"${DURATION_SECONDS}"S +"%Y-%m-%dT%H:%M:%SZ") # BSD/macOS date else EXPIRES_AT=$(date -u -d "+${DURATION_SECONDS} seconds" +"%Y-%m-%dT%H:%M:%SZ") # GNU date fi SESSION=$(curl -sf --compressed -u "$DEVADMIN_ACCOUNT:$DEVADMIN_SECRET" "$API_BASE_URL/jmap/session") || { echo "Could not reach $API_BASE_URL as $DEVADMIN_ACCOUNT. Is the server running ('npm run dev:server') and initialized ('scripts/dev-server-init.sh')?" >&2 exit 1 } ACCOUNT_ID=$(printf '%s' "$SESSION" | grep -o '"urn:stalwart:jmap":"[^"]*"' | cut -d'"' -f4) REQ=$(cat <&2 exit 1 fi ENV_PATH="$ROOT_DIR/.env.development.local" cat > "$ENV_PATH" <