/trunk/example/ajax_example.php |
---|
0,0 → 1,41 |
<?php |
/* |
* php_clientchallenge |
* Copyright 2021 Daniel Marschall, ViaThinkSoft |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); |
* you may not use this file except in compliance with the License. |
* You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software |
* distributed under the License is distributed on an "AS IS" BASIS, |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
* See the License for the specific language governing permissions and |
* limitations under the License. |
*/ |
if (file_exists(__DIR__ . '/../vendor/autoload.php')) { |
require_once __DIR__ . '/../vendor/autoload.php'; |
} |
require_once __DIR__ . '/../ClientChallenge.class.php'; |
define('MAX_TIME', 10); // seconds |
if (isset($_REQUEST['action']) && ($_REQUEST['action'] === 'add_numbers')) { |
// Check request field "vts_validation_result" for valid response of the Challenge |
\ViaThinkSoft\RateLimitingChallenge\ClientChallenge::checkValidation(MAX_TIME); |
// Do your stuff here. Example: |
$a = $_REQUEST['a']; |
$b = $_REQUEST['b']; |
$res = array("result" => ($a+$b)); |
header('Content-Type:application/json'); |
die(json_encode($res)); |
} |
/trunk/example/ajax_get_challenge.php |
---|
0,0 → 1,28 |
<?php |
/* |
* php_clientchallenge |
* Copyright 2021 Daniel Marschall, ViaThinkSoft |
* |
* Licensed under the Apache License, Version 2.0 (the "License"); |
* you may not use this file except in compliance with the License. |
* You may obtain a copy of the License at |
* |
* http://www.apache.org/licenses/LICENSE-2.0 |
* |
* Unless required by applicable law or agreed to in writing, software |
* distributed under the License is distributed on an "AS IS" BASIS, |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
* See the License for the specific language governing permissions and |
* limitations under the License. |
*/ |
if (file_exists(__DIR__ . '/../vendor/autoload.php')) { |
require_once __DIR__ . '/../vendor/autoload.php'; |
} |
require_once __DIR__ . '/../ClientChallenge.class.php'; |
define('COMPLEXITY', 500000); |
\ViaThinkSoft\RateLimitingChallenge\ClientChallenge::createChallenge(COMPLEXITY); |
/trunk/example/index.html |
---|
0,0 → 1,59 |
<!DOCTYPE HTML> |
<html> |
<head> |
<title>Example of server request using Client-Challenge in order to mitigate resource starvation</title> |
<meta charset="utf-8"> |
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> |
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-sha3/0.8.0/sha3.min.js"></script> |
<script src="../ClientChallenge.js"></script> |
<script> |
let error_cb = function (request, status, error) { |
$("#out").val("Error!"); |
} |
let callback = function(params, vts_validation_result) { |
$.ajax({ |
type: "POST", |
url: "ajax_example.php", |
data: { |
// This is required: |
"vts_validation_result": vts_validation_result, |
// This you can set yourself: |
"action": "add_numbers", |
"a": params['a'], |
"b": params['b'] |
}, |
success: function(data) { |
$("#out").val(data["result"]); |
}, |
error: error_cb |
}); |
} |
function calc() { |
var a = $("#in_a").val(); |
var b = $("#in_b").val(); |
var params = { |
"a": a, |
"b": b |
}; |
$("#out").val("Please wait..."); |
vts_validated_call("ajax_get_challenge.php", callback, params, error_cb); |
} |
</script> |
</head> |
<body> |
<h2>Example of server request using Client-Challenge in order to mitigate resource starvation</h2> |
<p><input id="in_a" value="2"> + <input id="in_b" value="3"> = <input id="out"> <input type="button" onclick="calc()" value="Calculate"></p> |
</body> |
</html> |