Skip to content

Instantly share code, notes, and snippets.

@leo60228
Last active August 24, 2024 15:35
Show Gist options
  • Save leo60228/7bf8678b9d714e17128d1f96a778bd8f to your computer and use it in GitHub Desktop.
Save leo60228/7bf8678b9d714e17128d1f96a778bd8f to your computer and use it in GitHub Desktop.
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p bash curl trurl openssl jq
set -ex
# KEY_ID="https://60228.dev/users/leo#main-key"
KEY_ID="${KEY_ID:?missing KEY_ID}"
# ssh 60228.dev 'sudo -u mastodon psql -tAc "SELECT private_key FROM accounts WHERE username = '\''leo'\'' AND domain IS NULL LIMIT 1;"' > key.pem
KEY_PATH="${KEY_PATH:?missing KEY_PATH}"
DATE="$(date -Ru | sed 's/+0000/GMT/')"
HOST="$(trurl --url "${1:?missing url}" --get '{host}')"
REQUEST_TARGET="get $(trurl --url "$1" --get '{:path}')"
HEADERS="(request-target): $REQUEST_TARGET"$'\n'"host: $HOST"$'\n'"date: $DATE"
RAW_SIGNATURE="$(echo -n "$HEADERS" | openssl dgst -binary -sign "$KEY_PATH" -sha256 | openssl enc -A -base64)"
SIGNATURE='keyId="'"$KEY_ID"'",algorithm="rsa-sha256",headers="(request-target) host date",signature="'"$RAW_SIGNATURE"'"'
curl -v \
-H 'Accept: application/ld+json; profile="https://www.w3.org/ns/activitystreams"' \
-H "Date: $DATE" \
-H "Host: $HOST" \
-H "Signature: $SIGNATURE" \
"$1" | jq .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment