Skip to content

Instantly share code, notes, and snippets.

@buckett
Last active March 19, 2019 08:40
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 buckett/ed3217fced4b9d129758157b4476aaa6 to your computer and use it in GitHub Desktop.
Save buckett/ed3217fced4b9d129758157b4476aaa6 to your computer and use it in GitHub Desktop.
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