Skip to content

Instantly share code, notes, and snippets.

@andypiper
Forked from AaronNGray/webfinger
Last active March 18, 2024 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andypiper/ed591c89d639c6e2404de50f1882e1a2 to your computer and use it in GitHub Desktop.
Save andypiper/ed591c89d639c6e2404de50f1882e1a2 to your computer and use it in GitHub Desktop.
#!/bin/bash
# webfinger (macOS)
# needs GNU getopt since macOS doesn't support relevant options
# and jq - use `brew install gnu-getopt jq`
if [ ! `which jq` ]
then
echo Installing 'jq'
brew install jq
fi
set -euo pipefail
rel=
verbose=
rawurlencode() {
local string="${1}"
local strlen=${#string}
local encoded=""
local pos c o
for (( pos=0 ; pos<strlen ; pos++ )); do
c=${string:$pos:1}
case "$c" in
[-_.~a-zA-Z0-9] ) o="${c}" ;;
* ) printf -v o '%%%02x' "'$c"
esac
encoded+="${o}"
done
echo "${encoded}" # You can either set a return variable (FASTER)
REPLY="${encoded}" #+or echo the result (EASIER)... or both... :p
}
usage() {
>&2 cat << EOF
Usage: $0
[ -v | --verbose ]
[ -r | --rel | --REL input ]
<user@domain>
EOF
exit 1
}
args=$(/opt/homebrew/opt/gnu-getopt/bin/getopt -a -o hvr: --long help,verbose,rel:,REL: -- "$@")
if [[ $? -gt 0 ]]; then
usage
fi
eval set -- ${args}
while :
do
case $1 in
-h | --help) usage; shift;;
-v | --verbose) verbose=true; shift;;
-r | --rel | --REL) rel=$2; shift 2;;
# -- means the end of the arguments; drop this, and break out of the while loop
--) shift; break ;;
*) >&2 echo Unsupported option: $1
usage ;;
esac
done
if [[ $# -eq 0 ]]; then
usage
fi
name=`echo $1 | sed 's/\(.*\)@\(.*\)$/\1/'`
domain=`echo $1 | sed 's/\(.*\)@\(.*\)$/\2/'`
server=$domain
address=${name}@${domain}
resource=acct:$(rawurlencode $address)
if [ ! -n "${rel}" ]; then
command="curl -s -H \"Accept: application/jrd+json\" https://${server}/.well-known/webfinger?resource=${resource}"
else
command="curl -s -H \"Accept: application/jrd+json\" https://${server}/.well-known/webfinger?resource=${resource}&rel=${rel}"
fi
if [ -n "${verbose}" ]
then
echo ${command}
echo
fi
result=`${command}`
echo $result | jq .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment