#!/bin/bash # git-credential-raea: git's sign-in for a raea project (docs/cloud-render.feature CR21, CR22), as a credential # helper, so no token is ever written into a repo or typed. Git asks it each time it needs one: # - the host of the repo's .lfsconfig (Git LFS, the media in R2): raea's own token as the password # - Artifacts (the project's Git): a token for that one repo, minted by raea's server just then (an hour) # raea's token is RAEA_AGENT_KEY (an agent key with the repos scope: a cloud box), else RAEA_TOKEN, else the device # token the raea app keeps in the macOS Keychain (service raea, or RAEA_KEYCHAIN). Which repo: git config's # raea.owner and raea.project (repo.sh sets them; `git -c` passes them to a clone). RAEA_API_ORIGIN names raea's # server for the repo token (https://raea.io). repo.sh registers it by its absolute path for both hosts. set -euo pipefail [ "${1:-}" = get ] || exit 0 api=${RAEA_API_ORIGIN:-https://raea.io} host="" while IFS='=' read -r k v; do [ -z "$k" ] && break [ "$k" = host ] && host=$v done token() { if [ -n "${RAEA_AGENT_KEY:-}" ]; then printf %s "$RAEA_AGENT_KEY"; return; fi if [ -n "${RAEA_TOKEN:-}" ]; then printf %s "$RAEA_TOKEN"; return; fi if [ -x /usr/bin/security ]; then /usr/bin/security find-generic-password -s "${RAEA_KEYCHAIN:-raea}" -a device-token -w 2>/dev/null && return; fi echo "git-credential-raea: no raea sign-in (RAEA_AGENT_KEY, RAEA_TOKEN or the raea app's Keychain item)" >&2 exit 1 } # LFS's host[:port], as git names it: the repo's .lfsconfig says where its media goes lfs=$(git config --get lfs.url || git config -f "$(git rev-parse --show-toplevel 2>/dev/null)/.lfsconfig" --get lfs.url 2>/dev/null || true) lfshost=${lfs#*://}; lfshost=${lfshost%%/*} if [ -n "$lfs" ] && [ "$host" = "$lfshost" ]; then echo username=raea echo "password=$(token)" exit 0 fi owner=$(git config --get raea.owner || true) project=$(git config --get raea.project || true) if [ -z "$owner" ] || [ -z "$project" ]; then echo "git-credential-raea: this repo has no raea.owner and raea.project (repo.sh sets them)" >&2 exit 1 fi # --fail-with-body: a refusal's reason (402 enterprise_required: Git on raea's servers is raea Enterprise's for now) out=$(curl -sS --fail-with-body -m 30 -X POST -H "authorization: Bearer $(token)" -H 'content-type: application/json' \ -d "{\"scope\":\"${RAEA_REPO_SCOPE:-write}\"}" "$api/v1/repos/$owner/$project/token") || { echo "git-credential-raea: raea's server gave no token for $project: ${out:-no answer}" >&2 exit 1 } t=$(printf %s "$out" | python3 -c 'import json, sys; print(json.load(sys.stdin)["token"])') echo username=x # Artifacts takes the token's secret, before ?expires=, as basic auth's password echo "password=${t%%\?expires=*}"