test-challenger.sh (5851B)
1 #!/usr/bin/env bash 2 # This file is in the public domain. 3 # 4 # Tests happy path of challenger mostly. 5 6 set -eu 7 8 # Exit, with status code "skip" (no 'real' failure) 9 function exit_skip() { 10 echo " SKIP: $1" 11 exit 77 12 } 13 14 # Exit, with error message (hard failure) 15 function exit_fail() { 16 echo " FAIL: $@" 17 exit 1 18 } 19 20 # Cleanup to run whenever we exit 21 function cleanup() 22 { 23 for n in $(jobs -p) 24 do 25 kill $n 2> /dev/null || true 26 done 27 rm -f "$LAST_RESPONSE" "$FILENAME" 28 wait 29 } 30 31 LAST_RESPONSE=$(mktemp responseXXXXXX.log) 32 FILENAME="test-challenger.txt" 33 34 # Install cleanup handler (except for kill -9) 35 trap cleanup EXIT 36 37 export PATH="$PATH:." 38 39 echo -n "Testing for jq" 40 jq -h > /dev/null || exit_skip "jq required" 41 echo " FOUND" 42 echo -n "Testing for curl" 43 curl -h > /dev/null || exit_skip "curl required" 44 echo " FOUND" 45 echo -n "Testing for wget" 46 wget -h > /dev/null || exit_skip "wget required" 47 echo " FOUND" 48 echo -n "Testing for challenger-httpd ..." 49 challenger-httpd -h > /dev/null || exit_skip "challenger-httpd required" 50 echo " FOUND" 51 52 CONF="test-challenger.conf" 53 BURL="http://localhost:9967" 54 REDIRECT_URI="http://client.example.com/" 55 56 echo -n "Initialize challenger database ..." 57 challenger-dbinit -r -c "${CONF}" &> dbinit.log 58 echo " OK" 59 60 echo -n "Add challenger client ..." 61 CLIENT_SECRET="secret-token:secret" 62 challenger-admin -c "${CONF}" -a "${CLIENT_SECRET}" "${REDIRECT_URI}" &> admin.log 63 echo " OK" 64 # We just reset the DB, thus the client ID must be 1 here: 65 CLIENT_ID=1 66 67 echo -n "Start challenger-httpd ..." 68 challenger-httpd -L INFO -c "${CONF}" &> httpd.log & 69 70 # Wait for challenger to be available 71 for n in $(seq 1 50) 72 do 73 echo -n "." 74 sleep 0.2 75 OK=0 76 # bank 77 wget --tries=1 --timeout=1 "${BURL}/config" -o /dev/null -O /dev/null >/dev/null || continue 78 OK=1 79 break 80 done 81 if [ 1 != $OK ] 82 then 83 exit_skip "Failed to launch challenger service" 84 fi 85 86 87 echo -n "Setup new validation process..." 88 SETUP_TIME=$(date +%s) 89 STATUS=$(curl "${BURL}/setup/${CLIENT_ID}" \ 90 -H "Authorization: Bearer ${CLIENT_SECRET}" \ 91 -d '' \ 92 -w "%{http_code}" -s -o $LAST_RESPONSE) 93 94 if [ "$STATUS" != "200" ] 95 then 96 exit_fail "Expected 200 OK. Got: $STATUS" $(cat $LAST_RESPONSE) 97 fi 98 NONCE=$(jq -r .nonce < "$LAST_RESPONSE") 99 EXPIRES=$(jq -er '.expires.t_s | numbers' < "$LAST_RESPONSE") || \ 100 exit_fail "Expected a finite expires timestamp in the /setup response" 101 if [ "$EXPIRES" -lt "$((SETUP_TIME + 3590))" ] || \ 102 [ "$EXPIRES" -gt "$((SETUP_TIME + 3610))" ] 103 then 104 exit_fail "Expected /setup expiration about one hour in the future, got $EXPIRES" 105 fi 106 echo " OK" 107 108 CLIENT_STATE="the-client-state" 109 CLIENT_SCOPE="the-client-scope" 110 111 echo -n "Initiating user login..." 112 STATUS=$(curl "${BURL}/authorize/${NONCE}" \ 113 -G \ 114 -H "Accept: application/json" \ 115 --data-urlencode "response_type=code" \ 116 --data-urlencode "client_id=${CLIENT_ID}" \ 117 --data-urlencode "redirect_uri=${REDIRECT_URI}" \ 118 --data-urlencode "state=${CLIENT_STATE}" \ 119 --data-urlencode "scope=${CLIENT_SCOPE}" \ 120 -w "%{http_code}" -s -o $LAST_RESPONSE) 121 122 if [ "$STATUS" != "200" ] 123 then 124 exit_fail "Expected 200 OK. Got: $STATUS" $(cat $LAST_RESPONSE) 125 exit 1 126 fi 127 echo "OK" 128 129 130 echo -n "Initiating address submission..." 131 STATUS=$(curl "${BURL}/challenge/${NONCE}" \ 132 -X POST \ 133 -H "Accept: application/json" \ 134 --data-urlencode "filename=${FILENAME}" \ 135 -w "%{http_code}" -s -o $LAST_RESPONSE) 136 137 if [ "$STATUS" != "200" ] 138 then 139 exit_fail "Expected 200 OK. Got: $STATUS" $(cat $LAST_RESPONSE) 140 fi 141 echo "OK" 142 143 TAN=$(cat ${FILENAME} | awk '{print $5}') 144 145 echo -n "Initiating TAN ${TAN} submission..." 146 RESULT=$(curl "${BURL}/solve/${NONCE}" \ 147 -X POST \ 148 -H "Accept: text/html" \ 149 --data-urlencode "pin=${TAN}" \ 150 -w "%{http_code} %{redirect_url}" -s -o $LAST_RESPONSE) 151 STATUS=$(echo "$RESULT" | awk '{print $1}') 152 TARGET=$(echo "$RESULT" | awk '{print $2}') 153 154 if [ "$STATUS" != "302" ] 155 then 156 exit_fail "Expected 302. Got: $STATUS" $(cat $LAST_RESPONSE) 157 fi 158 159 TURL=$(echo "$TARGET" | sed -e "s/?.*//g") 160 TCODE=$(echo "$TARGET" | sed -e "s/.*?code=//g" -e "s/&.*//g") 161 TSTATE=$(echo "$TARGET" | sed -e "s/.*&state=//g") 162 163 if [ "${TURL}" != "${REDIRECT_URI}" ] 164 then 165 exit_fail "Invalid redirect URI ${TURL} returned, wanted ${REDIRECT_URI}" 166 fi 167 if [ "${TSTATE}" != "${CLIENT_STATE}" ] 168 then 169 exit_fail "Invalid client state ${TSTATE} returned, wanted ${CLIENT_STATE}" 170 fi 171 echo "OK" 172 173 echo -n "Requesting authorization for client ..." 174 STATUS=$(curl "${BURL}/token" \ 175 -X POST \ 176 --data-urlencode "client_id=${CLIENT_ID}" \ 177 --data-urlencode "redirect_uri=${REDIRECT_URI}" \ 178 --data-urlencode "client_secret=${CLIENT_SECRET}" \ 179 --data-urlencode "code=${TCODE}" \ 180 --data-urlencode "grant_type=authorization_code" \ 181 -w "%{http_code}" -s -o $LAST_RESPONSE) 182 183 if [ "$STATUS" != "200" ] 184 then 185 exit_fail "Expected 200 OK. Got: $STATUS" $(cat $LAST_RESPONSE) 186 fi 187 TOKEN_TYPE="$(cat $LAST_RESPONSE | jq -r .token_type)" 188 if [ "$TOKEN_TYPE" != "Bearer" ] 189 then 190 exit_fail "Expected Bearer token. Got: $TOKEN_TYPE" 191 fi 192 ACCESS_TOKEN=$(cat $LAST_RESPONSE | jq -r .access_token) 193 EXPIRES_IN=$(cat $LAST_RESPONSE | jq -r .expires_in) 194 echo "OK" 195 196 echo -n "Requesting user information for client ..." 197 STATUS=$(curl "${BURL}/info" \ 198 -H "Authorization: Bearer ${ACCESS_TOKEN}" \ 199 -w "%{http_code}" -s -o $LAST_RESPONSE) 200 if [ "$STATUS" != "200" ] 201 then 202 exit_fail "Expected 200 OK. Got: $STATUS" $(cat $LAST_RESPONSE) 203 fi 204 205 # cat $LAST_RESPONSE | jq 206 207 TADDRESS=$(cat "$LAST_RESPONSE" | jq -r .address.filename) 208 TADDRESS_TYPE=$(cat "$LAST_RESPONSE" | jq -r .address_type) 209 210 if [ "$TADDRESS" != "$FILENAME" ] 211 then 212 exit_fail "Expected $FILENAME. Got: $TADDRESS" 213 fi 214 215 if [ "$TADDRESS_TYPE" != "file-access" ] 216 then 217 exit_fail "Expected file-access. Got: $TADDRESS_TYPE" 218 fi 219 echo "OK" 220 221 222 exit 0