Skip to content

Instantly share code, notes, and snippets.

@sroccaserra
Created May 26, 2016 08:53
Show Gist options
  • Select an option

  • Save sroccaserra/c87d2031b5463afbd4cfe03eb12cbde0 to your computer and use it in GitHub Desktop.

Select an option

Save sroccaserra/c87d2031b5463afbd4cfe03eb12cbde0 to your computer and use it in GitHub Desktop.
Script to understand OAuth2 Authorization Code flow with httpie (use it to understand only, not to test: don't test this manually)
#!/usr/bin/env bash
while [[ $# > 1 ]]
do
key="$1"
case $key in
-c|--code)
CODE="$2"
shift
;;
-i|--client-id)
CLIENT_ID="$2"
shift
;;
-s|--client-secret)
CLIENT_SECRET="$2"
shift
;;
-t|--token)
TOKEN="$2"
shift
;;
*)
# unknown option
;;
esac
shift
done
CLIENT_ID=${CLIENT_ID:='aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'}
CLIENT_SECRET=${CLIENT_SECRET:='aaaaaaaaaaaaaaaaaaaa'}
# CODE='aaaaaaaaaaaaaaaaaaaa'
# TOKEN='aaahbGciOiJSUzI1NiJ9.aaaqdGkiOiJhMTNjMTM1ZjkxMjliOWVkZjE0OCIsImlzcyI6Imh0dHA6Ly9zaG9wZ3VuLnByZXN0YXNob3AubmV0OjMwMDAiLCJzdWIiOiIxM2U4MDZjZS0xMzBmLTQ0MTAtODI3MC1iZGY2ODFhNWM0MzMiLCJhdWQiOiJlMjI0YTU5YS1lZTlhLTRlMmMtOWFiMS05ZGNhMjY1MDI4YTkiLCJleHAiOjE0NjQwNzc5NzMsImlhdCI6MTQ2NDA3NDM3Mywic2NvcGUiOiJlbWFpbCBvcGVuaWQgcHJvZmlsZaaa.aaaV6ufAZs1KMT2JEcPPyZndnEGrdVH1NvwAsZR9ZMnaaa-aaaNMdnZXDV_lmDk6r2jnHgXCRBPY6NGBuSH7SB9XJsc_zXE_zg8pmPvjMfiZTM1NmVzfZwEKLY9yg9NsBaaa-aaaO4xtLq_Lv1PSaaa-aaaY1IwxOivAZCdzdhmOClv6CTj3Hhh0EIwxsfj1ohsLn_3yq_ipCBhaaa-aaapyXgT5cFhuFhUBVEQdHOrc4aaa-aaaKmCHGeuUfuFBQGFzChxarmr6sAgUccvs3xh28PSID1olpoL49IeKhfZPxx3kjUedSY9ySm6OYIBruhIedukMtC_dUtlIkn4Nawf1CCbJfBI_x1ae7oGPi3M8LM1Jby9tflzZSqoEdBgiLUI0CYP519skgllFQdsZDaaU2DrO3_gTaNIffMKM8aaa-aaaoXxECNAC5IpkeSvgAJkUch07RujPiiWqKekJFQyCwFaaa-aaat58Qx8DT8pw7xi4L4l7YLGZjl9Kfzuy7k61VRn0ZIE9HSNVc3Hub2483zYxaaa-aaaw8zp3Oaaaa-aaaofm6RX4FBYmeqRF0A4wSIpFODU7HNUOK9LWaaa-z-aaaB0UeFe8ludPiaaa-aaaeoGv6dkI0LLh8DeG5_CDQrAfzzCdVIc4bpJOaaa-aaa2he7Y8wlt8Nfeaaa'
if [ -z "${CODE}" -a -z "${TOKEN}" ]
then
echo "https://auth.site.com/signin?client_id=${CLIENT_ID}&redirect_uri=https://my.site.com&response_type=code&scope=email%20openid"
exit 0
fi
if [ -z "${TOKEN}" ]
then
http --verbose "https://auth.site.com/token" "client_id=${CLIENT_ID}" "client_secret=${CLIENT_SECRET}" "redirect_uri=https://my.site.com" "grant_type=authorization_code" "code=${CODE}" Referrer:test
exit 0
fi
PAYLOAD='{ "name": "Coco" }'
http --verbose POST "https://api.site.com/v1/users" "${PAYLOAD}" "Authorization: Bearer ${TOKEN}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment