railyard/apps/gitea/scripts/add-rithub-oauth-sources.sh
caelum0x 765a46c6c2 feat(rithub): visible Sign up/Sign in header CTAs + GitHub/Google/Apple OAuth wiring
Owner complaint: "rithub still has no google github apple auth and create
account options" -- the header only showed an icon-only Register control
(no visible text), so a first-time visitor had no obvious way to create
an account.

- head_navbar.tmpl / theme-rithub.css: the signed-out Register/Sign In
  controls now show real "Sign up"/"Sign in" text next to their icons
  (filled accent pill for Sign up, plain icon+text for Sign in), as an
  explicit exception to the icon-only nav chrome rule. Dropped the
  mismatched localized aria-label in favor of matching English text/title
  (the visible text is now the accessible name).
- Login ("Register now.") and sign-up ("Sign in now!") cross-links were
  already present and styled bold -- verified live, left as-is.
- docker-compose.yml: [oauth2_client] ENABLE_AUTO_REGISTRATION=true,
  ACCOUNT_LINKING=auto, so a first OAuth login creates/links the account
  directly, no second form.
- New apps/gitea/scripts/add-rithub-oauth-sources.sh: runs on
  railyard-box (no SSH hop), reads /etc/products/rithub-oauth.env
  (root-only, owner-filled) and calls `gitea admin auth add-oauth` for
  GitHub/Google/Apple (Apple's ES256 client-secret JWT minted from the
  .p8 key via python3+PyJWT, already present on the box). Safe to re-run;
  skips sources that already exist or have no credentials. Verified live
  as a no-op with an empty env file.
- rithub-oauth.env.example documents the exact env keys; a real (empty)
  copy was created at /etc/products/rithub-oauth.env on railyard-box for
  the owner to fill in.
- Kept the existing Mac-side add-oauth-sources.sh working (cross-
  referenced, not replaced).

Deployed to railyard-box (apps/gitea only, backed up custom/+app.ini/
compose first). Verified live: header shows Sign up/Sign in text
(desktop + mobile), real signup via a mail.tm disposable address +
captcha succeeded end to end (account created, welcomed, deleted after).
No provider enabled yet -- OAuth app creation in each provider console
is an owner-only step, documented in plans/railyard.md.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-11-18 02:06:00 +03:00

89 lines
3.3 KiB
Bash

#!/usr/bin/env bash
# Adds GitHub / Google / Apple sign-in to Rithub (rithub.dev).
#
# Runs ON railyard-box itself (no SSH hop), reading credentials from a
# single root-only file: /etc/products/rithub-oauth.env (same convention
# as /etc/products/scribase.env, /etc/products/toolgate.env). That file
# never exists in this repo — it is created on the box by the owner
# (see plans/railyard.md for the exact owner steps and callback URLs).
#
# Usage (on railyard-box):
# sudo bash /srv/railyard-gitea/scripts/add-rithub-oauth-sources.sh
#
# Each source is only added if its env vars are present in the env file;
# existing sources (by name) are left untouched so re-running is safe.
# This does not print any secret value, only which sources were
# added/skipped and why.
set -euo pipefail
ENV_FILE="/etc/products/rithub-oauth.env"
CONTAINER="railyard-gitea-gitea-1"
if [ ! -f "$ENV_FILE" ]; then
echo "no $ENV_FILE on this box yet -- nothing to do. See plans/railyard.md for the owner steps that create it." >&2
exit 0
fi
# shellcheck disable=SC1090
set -a
source "$ENV_FILE"
set +a
existing=$(docker exec -u git "$CONTAINER" gitea admin auth list 2>/dev/null || true)
add() { # name provider key secret [extra args...]
local name=$1 provider=$2 key=$3 secret=$4
shift 4
if [ -z "$key" ] || [ -z "$secret" ]; then
echo "skip $name: credentials not set in $ENV_FILE"
return
fi
if grep -q "[[:space:]]$name[[:space:]]" <<<"$existing"; then
echo "skip $name: already configured"
return
fi
docker exec -u git "$CONTAINER" gitea admin auth add-oauth \
--name "$name" --provider "$provider" --key "$key" --secret "$secret" "$@"
echo "added $name"
}
add GitHub github "${RITHUB_GITHUB_CLIENT_ID:-}" "${RITHUB_GITHUB_CLIENT_SECRET:-}" \
--scopes "read:user,user:email"
add Google openidConnect "${RITHUB_GOOGLE_CLIENT_ID:-}" "${RITHUB_GOOGLE_CLIENT_SECRET:-}" \
--auto-discover-url https://accounts.google.com/.well-known/openid-configuration \
--scopes "openid email profile"
# Apple: Gitea needs a client *secret*, but Sign in with Apple issues a
# private signing key (.p8) instead -- the secret Gitea wants is a
# short-lived ES256 JWT you mint from that key. Max lifetime Apple allows
# is 6 months; re-running this script rotates it (skipped once a source
# named "Apple" already exists -- delete it first via `gitea admin auth
# delete --id <id>` to force a rotation).
APPLE_ID="${RITHUB_APPLE_SERVICES_ID:-}"
TEAM="${APPLE_TEAM_ID:-}"
KID="${RITHUB_APPLE_KEY_ID:-}"
P8="${RITHUB_APPLE_P8_PATH:-}"
if [ -n "$APPLE_ID" ] && [ -n "$TEAM" ] && [ -n "$KID" ] && [ -f "${P8:-/nonexistent}" ]; then
APPLE_SECRET=$(python3 - "$TEAM" "$KID" "$APPLE_ID" "$P8" <<'PY'
import sys, time, jwt
team, kid, sub, p8 = sys.argv[1:]
now = int(time.time())
print(jwt.encode(
{"iss": team, "iat": now, "exp": now + 180 * 86400, "aud": "https://appleid.apple.com", "sub": sub},
open(p8).read(),
algorithm="ES256",
headers={"kid": kid},
))
PY
)
add Apple openidConnect "$APPLE_ID" "$APPLE_SECRET" \
--auto-discover-url https://appleid.apple.com/.well-known/openid-configuration \
--scopes "openid"
else
echo "skip Apple: RITHUB_APPLE_SERVICES_ID / APPLE_TEAM_ID / RITHUB_APPLE_KEY_ID / RITHUB_APPLE_P8_PATH not all set in $ENV_FILE"
fi
echo "--- current oauth2 sources ---"
docker exec -u git "$CONTAINER" gitea admin auth list