Skip to content

Instantly share code, notes, and snippets.

@buckett

buckett/ccurl

Last active Mar 19, 2019
Embed
What would you like to do?
Canvas cURL
#!/bin/bash
# Canvas cURL
# Just pulls OAuth token for Canvas from keychain and puts it in the Authorization header
# You can set passwords with:
# security add-generic-password -a buckett -s canvas.instructure.com -w ....
# and if you need to update a token you need to first delete it with:
# security delete-generic-password -a buckett -s canvas.instructure.com
# This will also mean that the the security tool has read access by default.
# This should be set with something like:
# export CCURL_HOSTS="inst.instructure.com inst.test.instructure.com inst.beta.instructure.com"
canvas_hosts=(${CCURL_HOSTS})
# If your canvas account isn't your unix username set it with:
# export CCURL_USER=myusername
account=${CCURL_USER:-${USER}}
if [ -z "$canvas_hosts" ]; then
echo "You need to set the CCURL_HOSTS environmental variable to the hosts your want to add authorisation for."
exit 2
fi
for host in ${canvas_hosts[@]}; do
if [[ $@ == *${host}* ]]; then
service=$host
break
fi
done
if [ -z "$service" ]; then
echo "Failed to find service in arguments." 1>&2
exit 2
fi
token=$(security find-generic-password -s ${service} -a ${account} -w 2>/dev/null)
if [ -z "$token" ]; then
echo "Failed to find token, looking for account $account for $service" 1>&2
exit 3
fi
exec curl -H "Authorization: Bearer ${token}" "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.