#!/usr/bin/env bash # Creates the Gitea OAuth2 application for web login (owned by peppa-bot) and writes # GITEA_OAUTH_CLIENT_ID / GITEA_OAUTH_CLIENT_SECRET into .env. Idempotent: an existing # "Peppa" app is reused only if .env already has its secret (Gitea never shows it again). set -euo pipefail cd "$(dirname "$0")/.." PEPPA_ROOT="$PWD"; . scripts/lib/load-env.sh . scripts/gitea-lib.sh REDIRECTS="[\"${PEPPA_WEB_URL%/}/auth/callback\",\"http://localhost:3100/auth/callback\"]" existing=$(gitea_api "$GITEA_URL/api/v1/user/applications/oauth2" | python3 -c 'import json,sys;print(next((str(a["id"]) for a in json.load(sys.stdin) if a["name"]=="Peppa"),""))') if [ -n "$existing" ] && [ -n "${GITEA_OAUTH_CLIENT_SECRET:-}" ]; then echo "OAuth app exists (id $existing), .env has its secret"; exit 0; fi [ -n "$existing" ] && gitea_api -X DELETE "$GITEA_URL/api/v1/user/applications/oauth2/$existing" >/dev/null gitea_api -X POST "$GITEA_URL/api/v1/user/applications/oauth2" \ -d "{\"name\":\"Peppa\",\"redirect_uris\":$REDIRECTS,\"confidential_client\":true}" | python3 -c ' import json,re,sys d=json.load(sys.stdin); p=".env"; s=open(p).read() for k,v in (("GITEA_OAUTH_CLIENT_ID",d["client_id"]),("GITEA_OAUTH_CLIENT_SECRET",d["client_secret"])): s=re.sub(rf"^{k}=.*\n?","",s,flags=re.M) s=s.rstrip("\n")+f"\n{k}={v}\n" open(p,"w").write(s) print("created OAuth app, client id", d["client_id"])'