scripts: per-app SSO first-login scripts (gitea/redmine/discourse)

Each script: full OIDC webflow per app (gitea /user/oauth2/cloudron,
redmine /oauth?oauth_provider=1 GET-form, discourse /auth/oidc with
CSRF + confirm-page token), TOTP from vault seed, session verify.
Ran fleet-wide 2026-09-09: accounts created for all 32 org identities;
gitea tokens + redmine/discourse api keys minted via admin levers and
stored per identity in the vault.
https://projects.knownelement.com/issues/942
This commit is contained in:
2026-09-09 14:38:28 -05:00
parent 0076b4f941
commit 4699cd5070
3 changed files with 179 additions and 0 deletions
@@ -0,0 +1,56 @@
#!/bin/bash
# Redmine SSO first-login via Cloudron OIDC (redmine_oauth plugin, GET form).
# usage: redmine-sso-login.sh <cloudron-username> [vault-item]
# Verifies a logged-in session at /my/account.
set -uo pipefail
U="${1:?usage: redmine-sso-login.sh <cloudron-username> [vault-item]}"
ITEM="${2:-$U Cloudron}"
BASE="https://projects.knownelement.com"
IDP="https://my.knownelement.com"
JAR="/tmp/redmine-jar-$U.txt"; rm -f "$JAR"
SM() { if [ "$(id -un)" = "TSGCOO" ]; then /data2/TSGCOO/.local/bin/sm "$@" </dev/null; else sudo -u TSGCOO /data2/TSGCOO/.local/bin/sm "$@" </dev/null; fi; }
PW=$(SM get "$ITEM" --field password)
[ -n "$PW" ] || { echo "FAIL: no password in vault item $ITEM" >&2; exit 1; }
SEED=$(SM get "$ITEM" --field totp_seed 2>/dev/null || true)
# entry: the oauth GET form kick-off
INT=$(curl -sk -b "$JAR" -c "$JAR" -o /dev/null -w '%{url_effective}' -L --max-redirs 8 "$BASE/oauth?oauth_provider=1")
case "$INT" in
*/openid/interaction/*) : ;;
*) echo "FAIL: no interaction reached: $INT" >&2; exit 3 ;;
esac
UIDPATH=$(printf '%s' "$INT" | grep -o '/openid/interaction/[^?]*')
BODY="{\"username\":\"$U\",\"password\":\"$PW\""
if [ -n "$SEED" ]; then
TCODE=$(bash "$(dirname "$0")/totp.sh" "$SEED")
BODY="$BODY,\"totpToken\":\"$TCODE\""
fi
BODY="$BODY}"
LOGIN_RESP=$(curl -sk -b "$JAR" -c "$JAR" -X POST "$IDP$UIDPATH/login" -H 'Content-Type: application/json' -d "$BODY")
unset BODY
case "$LOGIN_RESP" in
*redirectTo*) : ;;
*) echo "FAIL: login rejected: $LOGIN_RESP" >&2; exit 5 ;;
esac
RED=$(printf '%s' "$LOGIN_RESP" | sed -n 's/.*"redirectTo":"\([^"]*\)".*/\1/p')
FINAL=$(curl -sk -b "$JAR" -c "$JAR" -o /dev/null -w '%{url_effective}' -L --max-redirs 8 "$RED")
case "$FINAL" in
*/openid/interaction/*)
CUIDPATH=$(printf '%s' "$FINAL" | grep -o '/openid/interaction/[^?]*')
CLOC=$(curl -sk -b "$JAR" -c "$JAR" -D - -o /dev/null -X POST "$IDP$CUIDPATH/confirm" -d '' | grep -i '^location:' | head -1 | tr -d '\r' | sed 's/^[Ll]ocation: //')
[ -n "$CLOC" ] || { echo "FAIL: consent confirm produced no redirect" >&2; exit 6; }
CLOC=$(printf '%s' "$CLOC" | sed 's|^https//|https://|; s|^http//|http://|')
case "$CLOC" in http://*|https://*) : ;; /*) CLOC="$IDP$CLOC" ;; *) CLOC="$IDP/$CLOC" ;; esac
FINAL=$(curl -sk -b "$JAR" -c "$JAR" -o /dev/null -w '%{url_effective}' -L --max-redirs 8 "$CLOC")
;;
esac
CODE=$(curl -sk -b "$JAR" -o /dev/null -w '%{http_code}' "$BASE/my/account")
if [ "$CODE" = "200" ]; then
echo "OK: $U logged into redmine (session verified)"
exit 0
fi
echo "FAIL: no redmine session (my/account HTTP $CODE), landed: $FINAL" >&2
exit 7