Vendoring deprecated composer libs
This commit is contained in:
73
libs/jsonrpc/src/JsonRPC/Validator/HostValidator.php
Normal file
73
libs/jsonrpc/src/JsonRPC/Validator/HostValidator.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace JsonRPC\Validator;
|
||||
|
||||
use JsonRPC\Exception\AccessDeniedException;
|
||||
|
||||
/**
|
||||
* Class HostValidator
|
||||
*
|
||||
* @package JsonRPC\Validator
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class HostValidator
|
||||
{
|
||||
/**
|
||||
* Validate
|
||||
*
|
||||
* @static
|
||||
* @access public
|
||||
* @param array $hosts
|
||||
* @param string $remoteAddress
|
||||
* @throws AccessDeniedException
|
||||
*/
|
||||
public static function validate(array $hosts, $remoteAddress)
|
||||
{
|
||||
if (!empty($hosts)) {
|
||||
foreach ($hosts as $host) {
|
||||
if (self::ipMatch($remoteAddress, $host)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw new AccessDeniedException('Access Forbidden');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate remoteAddress match host
|
||||
* @param $remoteAddress
|
||||
* @param $host
|
||||
* @return bool
|
||||
*/
|
||||
public static function ipMatch($remoteAddress, $host)
|
||||
{
|
||||
$host = trim($host);
|
||||
if (strpos($host, '/') !== false) {
|
||||
list($network, $mask) = explode('/', $host);
|
||||
if (self::netMatch($remoteAddress, $network, $mask)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if ($host === $remoteAddress) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* validate the ipAddress in network
|
||||
* 192.168.1.1/24
|
||||
* @param $clientIp
|
||||
* @param $networkIp
|
||||
* @param $mask
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function netMatch($clientIp, $networkIp, $mask)
|
||||
{
|
||||
$mask1 = 32 - $mask;
|
||||
return ((ip2long($clientIp) >> $mask1) == (ip2long($networkIp) >> $mask1));
|
||||
}
|
||||
}
|
||||
44
libs/jsonrpc/src/JsonRPC/Validator/JsonEncodingValidator.php
Normal file
44
libs/jsonrpc/src/JsonRPC/Validator/JsonEncodingValidator.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace JsonRPC\Validator;
|
||||
|
||||
use JsonRPC\Exception\ResponseEncodingFailureException;
|
||||
|
||||
/**
|
||||
* Class JsonEncodingValidator
|
||||
*
|
||||
* @package JsonRPC\Validator
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class JsonEncodingValidator
|
||||
{
|
||||
public static function validate()
|
||||
{
|
||||
$jsonError = json_last_error();
|
||||
|
||||
if ($jsonError !== JSON_ERROR_NONE) {
|
||||
switch ($jsonError) {
|
||||
case JSON_ERROR_DEPTH:
|
||||
$errorMessage = 'Maximum stack depth exceeded';
|
||||
break;
|
||||
case JSON_ERROR_STATE_MISMATCH:
|
||||
$errorMessage = 'Underflow or the modes mismatch';
|
||||
break;
|
||||
case JSON_ERROR_CTRL_CHAR:
|
||||
$errorMessage = 'Unexpected control character found';
|
||||
break;
|
||||
case JSON_ERROR_SYNTAX:
|
||||
$errorMessage = 'Syntax error, malformed JSON';
|
||||
break;
|
||||
case JSON_ERROR_UTF8:
|
||||
$errorMessage = 'Malformed UTF-8 characters, possibly incorrectly encoded';
|
||||
break;
|
||||
default:
|
||||
$errorMessage = 'Unknown error';
|
||||
break;
|
||||
}
|
||||
|
||||
throw new ResponseEncodingFailureException($errorMessage, $jsonError);
|
||||
}
|
||||
}
|
||||
}
|
||||
30
libs/jsonrpc/src/JsonRPC/Validator/JsonFormatValidator.php
Normal file
30
libs/jsonrpc/src/JsonRPC/Validator/JsonFormatValidator.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace JsonRPC\Validator;
|
||||
|
||||
use JsonRPC\Exception\InvalidJsonFormatException;
|
||||
|
||||
/**
|
||||
* Class JsonFormatValidator
|
||||
*
|
||||
* @package JsonRPC\Validator
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class JsonFormatValidator
|
||||
{
|
||||
/**
|
||||
* Validate
|
||||
*
|
||||
* @static
|
||||
* @access public
|
||||
* @param mixed $payload
|
||||
* @throws InvalidJsonFormatException
|
||||
*/
|
||||
public static function validate($payload)
|
||||
{
|
||||
if (! is_array($payload)) {
|
||||
throw new InvalidJsonFormatException('Malformed payload');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
35
libs/jsonrpc/src/JsonRPC/Validator/RpcFormatValidator.php
Normal file
35
libs/jsonrpc/src/JsonRPC/Validator/RpcFormatValidator.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace JsonRPC\Validator;
|
||||
|
||||
use JsonRPC\Exception\InvalidJsonRpcFormatException;
|
||||
|
||||
/**
|
||||
* Class RpcFormatValidator
|
||||
*
|
||||
* @package JsonRPC\Validator
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class RpcFormatValidator
|
||||
{
|
||||
/**
|
||||
* Validate
|
||||
*
|
||||
* @static
|
||||
* @access public
|
||||
* @param array $payload
|
||||
* @throws InvalidJsonRpcFormatException
|
||||
*/
|
||||
public static function validate(array $payload)
|
||||
{
|
||||
if (! isset($payload['jsonrpc']) ||
|
||||
! isset($payload['method']) ||
|
||||
! is_string($payload['method']) ||
|
||||
$payload['jsonrpc'] !== '2.0' ||
|
||||
(isset($payload['params']) && ! is_array($payload['params']))) {
|
||||
|
||||
throw new InvalidJsonRpcFormatException('Invalid JSON RPC payload');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
21
libs/jsonrpc/src/JsonRPC/Validator/UserValidator.php
Normal file
21
libs/jsonrpc/src/JsonRPC/Validator/UserValidator.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace JsonRPC\Validator;
|
||||
|
||||
use JsonRPC\Exception\AuthenticationFailureException;
|
||||
|
||||
/**
|
||||
* Class UserValidator
|
||||
*
|
||||
* @package JsonRPC\Validator
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class UserValidator
|
||||
{
|
||||
public static function validate(array $users, $username, $password)
|
||||
{
|
||||
if (! empty($users) && (! isset($users[$username]) || $users[$username] !== $password)) {
|
||||
throw new AuthenticationFailureException('Access not allowed');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user