enterprisemediawiki / meza

Setup an enterprise MediaWiki server with simple commands
MIT License
41 stars 27 forks source link

Document how to access API despite SAML #730

Open jamesmontalvo3 opened 7 years ago

jamesmontalvo3 commented 7 years ago

Via bash, this will get recent changes via API, assuming meza is properly configured.

#!/usr/bin/env bash

#Needs curl and jq

USERNAME="Yourusername"
USERPASS="insecurepassword"
QUERY="action=query&list=recentchanges&format=json"
WIKIAPI="https://example.com/mywiki/api.php"
cookie_jar="wikicj"
#Will store file in wikifile

# When I was testing this I had cert issues and used --insecure
insecure="--insecure"
# insecure=""

echo "UTF8 check: ☠"
#################login
echo "Logging into $WIKIAPI as $USERNAME..."

###############
#Login part 1
#printf "%s" "Logging in (1/2)..."
echo "Get login token..."
CR=$(curl -S \
    --location $insecure \
    --retry 2 \
    --retry-delay 5\
    --cookie $cookie_jar \
    --cookie-jar $cookie_jar \
    --user-agent "Curl Shell Script" \
    --keepalive-time 60 \
    --header "Accept-Language: en-us" \
    --header "Connection: keep-alive" \
    --header "X-SKIP-SAML: True" \
    --compressed \
    --request "GET" "${WIKIAPI}?action=query&meta=tokens&type=login&format=json")

echo "$CR" | jq .

rm login.json
echo "$CR" > login.json
TOKEN=$(jq --raw-output '.query.tokens.logintoken' login.json)
TOKEN="${TOKEN//\"/}" #replace double quote by nothing

#Remove carriage return!
printf "%s" "$TOKEN" > token.txt
TOKEN=$(cat token.txt | sed 's/\r$//')

if [ "$TOKEN" == "null" ]; then
    echo "Getting a login token failed."
    exit
else
    echo "Login token is $TOKEN"
    echo "-----"
fi

###############
#Login part 2
echo "Logging in..."
CR=$(curl -S \
    --location $insecure \
    --cookie $cookie_jar \
    --cookie-jar $cookie_jar \
    --user-agent "Curl Shell Script" \
    --keepalive-time 60 \
    --header "Accept-Language: en-us" \
    --header "Connection: keep-alive" \
    --header "X-SKIP-SAML: True" \
    --compressed \
    --data-urlencode "username=${USERNAME}" \
    --data-urlencode "password=${USERPASS}" \
    --data-urlencode "rememberMe=1" \
    --data-urlencode "logintoken=${TOKEN}" \
    --data-urlencode "loginreturnurl=http://en.wikipedia.org" \
    --request "POST" "${WIKIAPI}?action=clientlogin&format=json")

echo "$CR" | jq .

STATUS=$(echo $CR | jq '.clientlogin.status')
if [[ $STATUS == *"PASS"* ]]; then
    echo "Successfully logged in as $USERNAME, STATUS is $STATUS."
    echo "-----"
else
    echo "Unable to login, is logintoken ${TOKEN} correct?"
    exit
fi

###################
#Get recent changes
echo "Fetching recent changes..."
CR=$(curl -S \
    --location $insecure \
    --cookie $cookie_jar \
    --cookie-jar $cookie_jar \
    --user-agent "Curl Shell Script" \
    --keepalive-time 60 \
    --header "Accept-Language: en-us" \
    --header "Connection: keep-alive" \
    --header "X-SKIP-SAML: True" \
    --compressed \
    --request "GET" "${WIKIAPI}?${QUERY}")

echo "$CR" | jq .
echo "$CR" > recentchanges.json
jamesmontalvo3 commented 5 years ago

Can use Pywikibot after this is merged: https://gerrit.wikimedia.org/r/c/pywikibot/core/+/464832