Skip to content

Instantly share code, notes, and snippets.

@eduard-sukharev
Last active December 27, 2018 14:30
Show Gist options
  • Save eduard-sukharev/9ebd5e8be59b935fdf647e0a405efaff to your computer and use it in GitHub Desktop.
Save eduard-sukharev/9ebd5e8be59b935fdf647e0a405efaff to your computer and use it in GitHub Desktop.
Fetch OAuth2 Authorization code for client and cookie value, urldecoded
#!/bin/bash
authorize_path='oauth2/authorize'
cookie_name='ACC_SID'
redirect_uri=${4:+"redirect_uri=$4"}
if [ "$#" -lt 3 ]; then
echo "Usage is:"
echo ""
echo -e "\033[0;32m$0\033[0;33m host_url client_id COOKIE_VALUE\033[0m"
echo -e " \033[0;33mhost_url\033[0m - base host url, e.g. https://example.com"
echo -e " \033[0;33mclient_id\033[0m - OAuth2 client id, e.g. oauth2-test"
echo -e " \033[0;33mCOOKIE_VALUE\033[0m - value of authentication cookie set after authenticating at server"
echo -e " \033[0;33mredirect_uri\033[0m - redirect uri for valid this client (optional if client has only 1 redirect uri)"
exit 1;
fi
curl --get "$1/$authorize_path" --data-urlencode "response_type=code" --data-urlencode "client_id=$2" --data-urlencode "$redirect_uri" -H "Cookie: $cookie_name=$3;" -Ls -o /dev/null -w %{url_effective} \
| grep --color=none -aPo "(?<=code=)(.+)$" \
| php -R 'echo urldecode($argn)."\n";'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment