Skip to content

Instantly share code, notes, and snippets.

@kmila
Created October 30, 2014 16:31
Show Gist options
  • Save kmila/e9db40a014a48a98dd67 to your computer and use it in GitHub Desktop.
Save kmila/e9db40a014a48a98dd67 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# -------------------
# BEGIN CONFIGURATION
# -------------------
DEBUG=0
CMD="curl -ks"
URI="https://ENV_URI"
# service
SERVICE="SERVICE_NAME"
API="${SERVICE}/?q=${1}"
# application
PARTNER_URI="https://PARTNER_URI/login"
CID="clientuuid"
SECRET="qwerty"
# -------------------
if [ "$DEBUG" -eq 1 ]
then
CMD_OIC="curl -ksv"
else
CMD_OIC="curl -ks -H \"X-OAUTH: ${SERVICE}\""
fi
# -------------------
# -------------------
# END CONFIGURATION
# -------------------
echo -e "[enter]: ${CMD_OIC}\n"
read -rp "Request code [enter]" PROMPT
CODE=$(${CMD_OIC} "${URI}/oic?state=state&client_id=${CID}&response_type=code&scope=openid&redirect_uri=${PARTNER_URI}" | grep -Po '(?<=code=).*(?=&)')
if [ $? -ne 0 ]; then
echo -e "Could not complete request to id.services"
exit $?
fi
read -p "Access token for ${CODE} [enter]" PROMPT
TOKENS=$(${CMD}d "login&grant_type=authorization_code&code=${CODE}&redirect_uri=${PARTNER_URI}&client_id=${CID}&client_secret=${SECRET}" "${URI}/token_endpoint/access_token" | grep -Po '(?<=token":").*?(?=",)')
if [ $? -ne 0 ]; then
echo -e "Could not complete request to token_endpoint"
exit $?
fi
declare -a TOKEN=($TOKENS)
echo "access_token=${TOKEN[0]}"
echo "refresh_token=${TOKEN[1]}"
read -p "${SERVICE} API call [enter]" PROMPT
${CMD}H "Authorization: Bearer ${TOKEN[0]}" "${URI}/${API}"
if [ $? -eq 0 ]; then
echo -e "\n"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment