Skip to content

Instantly share code, notes, and snippets.

@gkazior
Forked from dodok1/cas-get.sh
Last active September 22, 2023 12:19
Show Gist options
  • Save gkazior/4cf7e4c38fbcbc310267 to your computer and use it in GitHub Desktop.
Save gkazior/4cf7e4c38fbcbc310267 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Usage: cas-get.sh {url} {username} {password} # If you have any errors try removing the redirects to get more information
# The service to be called, and a url-encoded version (the url encoding isn't perfect, if you're encoding complex stuff you may wish to replace with a different method)
DEST="$1"
ENCODED_DEST=`echo "$DEST" | perl -p -e 's/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg' | sed 's/%2E/./g' | sed 's/%0A//g'`
#IP Addresses or hostnames are fine here
CAS_HOSTNAME=galaxy:9143
#Authentication details. This script only supports username/password login, but curl can handle certificate login if required
USERNAME=$2
PASSWORD=$3
#Temporary files used by curl to store cookies and http headers
COOKIE_JAR=.cookieJar
HEADER_DUMP_DEST=.headers
rm $COOKIE_JAR
rm $HEADER_DUMP_DEST
#The script itself is below
#Visit CAS and get a login form. This includes a unique ID for the form, which we will store in CAS_ID and attach to our form submission. jsessionid cookie will be set here
#CAS_ID=`curl -s -k -c ${COOKIE_JAR} https://${CAS_HOSTNAME}/cas/login?service=${ENCODED_DEST} | tee response1.txt | grep 'name=.execution\|name=.lt' | sed 's/.*value..//' | sed 's/\".*//'`
CAS_ID=`curl -s -k -c $COOKIE_JAR https://$CAS_HOSTNAME/cas/login?service=$ENCODED_DEST | grep name=.lt | sed 's/.*value..//' | sed 's/\".*//'`
if [[ "$CAS_ID" = "" ]]; then
echo "Login ticket is empty."
exit 1
fi
#Submit the login form, using the cookies saved in the cookie jar and the form submission ID just extracted. We keep the headers from this request as the return value should be a 302 including a "ticket" param which we'll need in the next request
curl -s -k --data "username=$USERNAME&password=$PASSWORD&lt=$CAS_ID&execution=e1s1&_eventId=submit" -i -b $COOKIE_JAR -c $COOKIE_JAR https://$CAS_HOSTNAME/cas/login?service=$ENCODED_DEST -D $HEADER_DUMP_DEST -o /dev/null
#Linux may not need this line but my response from the previous call has retrieving windows-style linebreaks in OSX
#dos2unix $HEADER_DUMP_DEST > /dev/null
#Visit the URL with the ticket param to finally set the casprivacy and, more importantly, MOD_AUTH_CAS cookie. Now we've got a MOD_AUTH_CAS cookie, anything we do in this session will pass straight through CAS
CURL_DEST=`grep Location $HEADER_DUMP_DEST | sed 's/Location: //'`
if [[ "$CURL_DEST" = "" ]]; then
echo "Cannot login. Check if you can login in a browser using user/pass = $USERNAME/$PASSWORD and the following url: https://$CAS_HOSTNAME/cas/login?service=$ENCODED_DEST"
exit 1
fi
curl -s -k -b $COOKIE_JAR -c $COOKIE_JAR $CURL_DEST
#If our destination is not a GET we'll need to do a GET to, say, the user dashboard here
#Visit the place we actually wanted to go to
curl -s -k -b $COOKIE_JAR "$DEST"
@gkazior
Copy link
Author

gkazior commented Feb 2, 2015

My forked revision works fine with CAS 3.5.2 4.0.
Possibly the base version dated at 2011 worked with older versions.

@paszczus
Copy link

Hello,
Unfortunately this version is not working for me with CAS 3.x and 4.x.
I am getting:

<title>CAS Authentication wanted!</title>

CAS Authentication wanted!

You should already have been redirected to the CAS server. Click here to continue.

@p-veron
Copy link

p-veron commented Nov 22, 2017

Great script ! but i have a problem. I can fetch the CAS_ID and the ticket, unfortunately when executin the last line of the script, i obtain "<TITLE>Moved Temporarily</TITLE>

Moved Temporarily

The requested resource resides temporarily under ...."
Any idea ?
Thanks a lot

@sunilmbhoi
Copy link

Unfortunately, it is not working for me I am getting CAS_ID but it is not generating location in the header to get ticket ID. I am using CAS version 4.0.4

@mgrRaf
Copy link

mgrRaf commented Feb 26, 2019

My forked rev. is working for CAS 4.0.1

@erseco
Copy link

erseco commented Sep 22, 2023

I wrote an updated fork here feel free to comment any issue

@gkazior
Copy link
Author

gkazior commented Sep 22, 2023

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment