Skip to content

Commit

Permalink
Merge pull request #206 from jembi/curl-wrapper-utility
Browse files Browse the repository at this point in the history
Added curl wrapper utility
  • Loading branch information
hnnesv committed Sep 23, 2014
2 parents 912a92c + aa21541 commit 00ff264
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions resources/openhim-api-curl.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

if (( $# < 2)); then
echo "OpenHIM API: Curl wrapper that sets up the appropriate OpenHIM authentication headers";
echo "Usage: $0 USERNAME PASSWORD [CURL_ARGS...]";
exit 0;
fi

username=$1;
pass=$2;
shift;
shift;

# which server?
server=""
for arg in $@; do
match=`echo $arg | grep http | perl -pe 's|(https?://.*?)/.*|\1|'`;
if [ "$match" ]; then
server=$match;
fi
done

if [ ! "$server" ]; then
echo "OpenHIM server not specified";
exit 0;
fi

auth=`curl -s $server/authenticate/$username`;
salt=`echo $auth | perl -pe 's|.*"salt":"(.*?)".*|\1|'`;
ts=`echo $auth | perl -pe 's|.*"ts":"(.*?)".*|\1|'`;

passhash=`echo -n "$salt$pass" | shasum -a 512 | awk '{print $1}'`;
token=`echo -n "$passhash$salt$ts" | shasum -a 512 | awk '{print $1}'`;

curl -H "auth-username: $username" -H "auth-ts: $ts" -H "auth-salt: $salt" -H "auth-token: $token" $@;
echo "";

0 comments on commit 00ff264

Please sign in to comment.