diff options
author | Frédéric Guillot <fred@kanboard.net> | 2018-06-21 14:13:41 -0700 |
---|---|---|
committer | Frédéric Guillot <fred@kanboard.net> | 2018-06-21 14:13:41 -0700 |
commit | a491348d442ab8e6cd2fa403d4365cdad78e52ce (patch) | |
tree | a00f575d82afb2c9051bad95398b4250f4a3d44d /libs/jsonrpc/src/JsonRPC/Validator | |
parent | c73ac5f1f818b6b21083f6785b4b2f6d778a6496 (diff) |
Vendoring deprecated composer libs
Diffstat (limited to 'libs/jsonrpc/src/JsonRPC/Validator')
5 files changed, 203 insertions, 0 deletions
diff --git a/libs/jsonrpc/src/JsonRPC/Validator/HostValidator.php b/libs/jsonrpc/src/JsonRPC/Validator/HostValidator.php new file mode 100644 index 00000000..3f9d6989 --- /dev/null +++ b/libs/jsonrpc/src/JsonRPC/Validator/HostValidator.php @@ -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)); + } +} diff --git a/libs/jsonrpc/src/JsonRPC/Validator/JsonEncodingValidator.php b/libs/jsonrpc/src/JsonRPC/Validator/JsonEncodingValidator.php new file mode 100644 index 00000000..0bbc4abd --- /dev/null +++ b/libs/jsonrpc/src/JsonRPC/Validator/JsonEncodingValidator.php @@ -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); + } + } +} diff --git a/libs/jsonrpc/src/JsonRPC/Validator/JsonFormatValidator.php b/libs/jsonrpc/src/JsonRPC/Validator/JsonFormatValidator.php new file mode 100644 index 00000000..ca8e7a69 --- /dev/null +++ b/libs/jsonrpc/src/JsonRPC/Validator/JsonFormatValidator.php @@ -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'); + } + } +} + diff --git a/libs/jsonrpc/src/JsonRPC/Validator/RpcFormatValidator.php b/libs/jsonrpc/src/JsonRPC/Validator/RpcFormatValidator.php new file mode 100644 index 00000000..f253a5a1 --- /dev/null +++ b/libs/jsonrpc/src/JsonRPC/Validator/RpcFormatValidator.php @@ -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'); + } + } +} + diff --git a/libs/jsonrpc/src/JsonRPC/Validator/UserValidator.php b/libs/jsonrpc/src/JsonRPC/Validator/UserValidator.php new file mode 100644 index 00000000..4f889719 --- /dev/null +++ b/libs/jsonrpc/src/JsonRPC/Validator/UserValidator.php @@ -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'); + } + } +} |