1

I wanted to execute one API using the command line but need to pass Cookie: JSESSIONID=<>; auth_cookie=<> value while executing it. We have CAS authentication for the target API.

Please let me know the curl or another command to get Cookie: JSESSIONID=<>; auth_cookie=<> value so I can pass it while executing API.

I have tried with

 curl -v -s --cacert /etc/ssl/certs/cacert.crt https://example.com/login -c cookiefile -d "user=user1@domain.com&password=xxxx" -X POST

But it is just storing JSESSIONID in the cookie file. In browser login https://example.com/login will redirect to CAS server login page after authentication it will redirect to https://example.com/index

I followed the approach given by @HBruijn.

ENCODED_DEST=`echo "https://example.com/login/login" | perl -p -e 's/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg' | sed 's/%2E/./g' | sed 's/%0A//g'`
CAS_ID=`curl -L -s --cacert /etc/ssl/certs/cacertga.crt -c $COOKIE_JAR https://cas-server.example2.com/cas/login?service=$ENCODED_DEST | grep name=.lt | sed 's/.*value..//' | sed 's/\".*//'`

This CAS_ID and JSESSIONID need to use in next call

curl -L -s --cacert /etc/ssl/certs/cacertga.crt --data "username=$USERNAME&password=$PASSWORD&lt=$CAS_ID&execution=e1s1&_eventId=submit" -i -b $COOKIE_JAR -c $COOKIE_JAR https://cas-server.example2.com/cas/login?service=$ENCODED_DEST -D .header.txt -o /dev/null

and finally .header.txt should have location URL appending with ticket id. Need to do final call on that location to get auth_cookie. But in my case, it is not giving location URL itself.

I refer solution here

I am editing this question, As I got a solution using python. Use mechanize.Browser() and it will easy to get require auth cookie.

Sunil Bhoi
  • 189
  • 1
  • 1
  • 9

1 Answers1

1

The fact that you need to log in first before you can access the API means that a single curl request is probably not sufficient.

You probably need a script with multiple curl requests and work flow similar to:

  1. Make API request with the existing cookies from the cookiefile
  2. Check response code, if OK then the session cookies were not expired yet and you don't need to do more. Yeah!
  3. If the response code indicates an authentication error status (your authentication session is empty or has expired); then you need to log in.
  4. Post your credentials to the CAS server login form.
  5. Check the response code and see if log in was successful and update the cookie file.
    Or die();
  6. GOTO 1.

The standard Chrome developer tools plugin can standard help you to create the basis for such a curl script with the Copy All as cURL option in the network console.

enter image description here

HBruijn
  • 72,524
  • 21
  • 127
  • 192