0) { debug("Replayed session counter=".$sessionCounter.', seen='.$seenSessionCounter); sendResp(S_REPLAYED_OTP); exit; } else { debug("Session counter OK (".$sessionCounter.")"); } //// Check the high counter // $hi = $decoded_token["high"]; // From the req $seenHi = $ad['high']; // From DB $hiDiff = $seenHi - $hi; if ($scDiff == 0 && $hiDiff > 0) { debug("Replayed hi counter=".$hi.', seen='.$seenHi); sendResp(S_REPLAYED_OTP); exit; } else { debug("Hi counter OK (".$hi.")"); } //// Check the low counter // $lo = $decoded_token["low"]; // From the req $seenLo = $ad['low']; // From DB $loDiff = $seenLo - $lo; if ($scDiff == 0 && $hiDiff == 0 && $loDiff >= 0) { debug("Replayed low counter=".$lo.', seen='.$seenLo); sendResp(S_REPLAYED_OTP); exit; } else { debug("Lo counter OK (".$lo.")"); } //// Update the DB only upon validation success // if (updDB($ad['id'], $decoded_token)) { debug('Validation database updated'); sendResp(S_OK); } else { debug('Failed to update validation database'); sendResp(S_BACKEND_ERROR); } ////////////////////////// // Functions ////////////////////////// function sendResp($status, $info=null) { global $ad, $apiKey; if ($status == null) { $status = S_BACKEND_ERROR; } echo 'status='.($a['status'] = $status).PHP_EOL; if ($info != null) { echo 'info='.($a['info'] = $info).PHP_EOL; } echo 't='.($a['t']=getUTCTimeStamp()).PHP_EOL; $h = sign($a, $apiKey); echo 'h='.$h.PHP_EOL; echo PHP_EOL; } // End sendResp function updDB($keyid, $new) { $stmt = 'UPDATE yubikeys SET '. 'accessed=NOW(),'. 'counter='.$new['session_counter'].','. 'low='.$new['low'].','. 'high='.$new['high']. ' WHERE id='.$keyid; if (!query($stmt)) { $err = 'Failed to update validation data of key: '.$keyid.' by '.$stmt; debug($err); writeLog($err); return false; } return true; } ?>