Search code examples
curlmediawikintlm

Using curl to fetch MediaWiki page info through SSO redirects


tl;dr: HTTP request to URL works through Chrome and Firefox, but fails using curl.

Details:

My company uses MediaWiki for an Intranet wiki. My application used to successfully use cURL to fetch information about pages using this command:

curl -s -k -u "username:password" "https://wiki.intranet/mainwiki/api.php?action=query&titles=SomePage&prop=info&format=xml"

However, the IT department has recently changed the wiki to use some sort of Single-Sign On system. If I request that URL now I get a series of HTTP 302 redirects, going through the following Locations:

  • /sso?request_uri=/mainwiki/api.php?action=query&titles=SomePage&prop=info&format=xml
  • http://wiki.intranet/sso/?request_uri=/mainwiki/api.php%3faction=query&titles=SomePage&prop=info&format=xml
  • https://wiki.intranet/sso/?request_uri=/mainwiki/api.php%3faction=query&titles=SomePage&prop=info&format=xml

When I request that last URL using cURL I get a 400 (Bad Request) response from the server. With the -I parameter to show the headers, here it is:

> curl -S -k -I -u "username:password" "https://wiki.intranet/sso/?request_uri=/mainwiki/api.php%3faction=query&titles=SomePage&prop=info&format=xml"
HTTP/1.1 400 Bad Request
Date: Wed, 29 Apr 2015 20:47:49 GMT
Server: Apache/2.2.3 (CentOS)
Connection: close
Content-Type: text/html; charset=iso-8859-1

When I request that same URL using Google Chrome, however, I see the results that I want!

How do I get the information using curl?


Edit: When I request the original URL using Firefox, it also goes through the same redirects, and then shows me two 401 (Authorization Required) statuses. When I fill out the username/password in Firefox it takes me through another redirect back to the original url, at which point it gets a status 200 response with the XML I want.


Solution

  • Finally got this to work by adding the following:

    curl -s -k-L --ntlm -c cookies.txt-u "user:pass" "originalurl"

    Note that some pages suggest that you can use -u : on Windows to pass your credentials along. This did not work for me.

    This worked using the following curl version on Windows:

    c:\> curl --version
    curl 7.36.0 (x86_64-w64-mingw32) libcurl/7.36.0 OpenSSL/1.0.1c zlib/1.2.8
    Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smtp smtps telnet tftp
    Features: Largefile NTLM SSL libz TLS-SRP