Skip to content

Instantly share code, notes, and snippets.

@jpf
Created April 28, 2016 17:57
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 jpf/4b60d6134401138022ee0d7155a4aa74 to your computer and use it in GitHub Desktop.
Save jpf/4b60d6134401138022ee0d7155a4aa74 to your computer and use it in GitHub Desktop.
#!/bin/bash
# http://stackoverflow.com/questions/8175000/parsing-arguments-options-flags-in-a-bash-script
base_url=""
client_id=""
origin=""
password=""
username=""
verbose=0
while getopts ":b:c:o:u:p:v" OPTION
do
case $OPTION in
b)
base_url="$OPTARG"
;;
c)
client_id="$OPTARG"
;;
o)
origin="$OPTARG"
;;
u)
username="$OPTARG"
;;
p)
password="$OPTARG"
;;
v)
verbose=1
;;
[?])
echo "Usage: $0 -b base_url -c client_id -o origin -u username -p password" >&2
exit 1
;;
esac
done
# redirect_uri="https%3A%2F%2Fjfranusic.ngrok.io"
redirect_uri=$(curl --silent --output /dev/null --write-out %{url_effective} --get --data-urlencode "$origin" "" | cut -d '?' -f 2)
rv=$(curl --silent "${base_url}/api/v1/authn" \
-H "Origin: ${origin}" \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
--data-binary $(printf '{"username":"%s","password":"%s"}' $username $password) )
if [ $verbose -eq 1 ]; then
echo $rv
fi
session_token=$(echo $rv | /usr/local/bin/jq '.sessionToken' | tr -d \")
if [ $verbose -eq 1 ]; then
echo "Session token: '${session_token}'"
fi
url=$(printf "%s/oauth2/v1/authorize?sessionToken=%s&client_id=%s&scope=openid+email+profile+groups&response_type=id_token&response_mode=fragment&nonce=%s&redirect_uri=%s&state=%s" \
$base_url \
$session_token \
$client_id \
"staticNonce" \
$redirect_uri \
"staticState")
if [ $verbose -eq 1 ]; then
echo "Here is the URL: '${url}'"
curl -v $url;
exit
fi
rv=$(curl --silent -v $url 2>&1)
id_token=$(echo "$rv" | egrep '^< Location: .*id_token=' | cut -d '=' -f 2 | egrep -o '[[:alnum:]_\.\-]*')
echo $id_token
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment