diff --git a/lib/SimpleSAML/Utils/Arrays.php b/lib/SimpleSAML/Utils/Arrays.php index 7c081d40f7b58967d59ed5568c1e243d6ada01f9..91e6518f1bfac99baa820dc82442e408a4302839 100644 --- a/lib/SimpleSAML/Utils/Arrays.php +++ b/lib/SimpleSAML/Utils/Arrays.php @@ -23,7 +23,7 @@ class Arrays * @author Andreas Solberg, UNINETT AS <andreas.solberg@uninett.no> * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> */ - public static function arrayize($data, $index = 0) + public static function arrayize($data, $index = 0) : array { return (is_array($data)) ? $data : [$index => $data]; } @@ -38,12 +38,8 @@ class Arrays * * @author Andreas Solberg, UNINETT AS <andreas.solberg@uninett.no> */ - public static function transpose($array) + public static function transpose(array $array) { - if (!is_array($array)) { - return false; - } - $ret = []; foreach ($array as $k1 => $a2) { if (!is_array($a2)) { diff --git a/lib/SimpleSAML/Utils/Attributes.php b/lib/SimpleSAML/Utils/Attributes.php index 6cc0fbb6a35f0b1096fbe63f0c233ee4c91a41ad..b78811329b3d46b4e22956826b72b05632430003 100644 --- a/lib/SimpleSAML/Utils/Attributes.php +++ b/lib/SimpleSAML/Utils/Attributes.php @@ -28,20 +28,8 @@ class Attributes * @throws \InvalidArgumentException If $attributes is not an array or $expected is not a string. * @throws \SimpleSAML\Error\Exception If the expected attribute was not found in the attributes array. */ - public static function getExpectedAttribute($attributes, $expected, $allow_multiple = false) + public static function getExpectedAttribute(array $attributes, string $expected, bool $allow_multiple = false) { - if (!is_array($attributes)) { - throw new \InvalidArgumentException( - 'The attributes array is not an array, it is: ' . print_r($attributes, true) . '.' - ); - } - - if (!is_string($expected)) { - throw new \InvalidArgumentException( - 'The expected attribute is not a string, it is: ' . print_r($expected, true) . '.' - ); - } - if (!array_key_exists($expected, $attributes)) { throw new Error\Exception("No such attribute '" . $expected . "' found."); } @@ -81,14 +69,8 @@ class Attributes * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> */ - public static function normalizeAttributesArray($attributes) + public static function normalizeAttributesArray(array $attributes) : array { - if (!is_array($attributes)) { - throw new \InvalidArgumentException( - 'The attributes array is not an array, it is: ' . print_r($attributes, true) . '".' - ); - } - $newAttrs = []; foreach ($attributes as $name => $values) { if (!is_string($name)) { @@ -124,7 +106,7 @@ class Attributes * * @return array The attribute name, split to the namespace and the actual attribute name. */ - public static function getAttributeNamespace($name, $defaultns) + public static function getAttributeNamespace(string $name, string $defaultns) : array { $slash = strrpos($name, '/'); if ($slash !== false) { diff --git a/lib/SimpleSAML/Utils/Auth.php b/lib/SimpleSAML/Utils/Auth.php index 5adf2a370f426eabbe7e396d338e66d21033b65b..e0df2ea0a5aeafae0b099eb7c833171b3d7bfc5a 100644 --- a/lib/SimpleSAML/Utils/Auth.php +++ b/lib/SimpleSAML/Utils/Auth.php @@ -24,12 +24,8 @@ class Auth * @return string A URL which can be used for admin authentication. * @throws \InvalidArgumentException If $returnTo is neither a string nor null. */ - public static function getAdminLoginURL($returnTo = null) + public static function getAdminLoginURL(?string $returnTo = null) : string { - if (!(is_string($returnTo) || is_null($returnTo))) { - throw new \InvalidArgumentException('Invalid input parameters.'); - } - if ($returnTo === null) { $returnTo = HTTP::getSelfURL(); } @@ -46,12 +42,8 @@ class Auth * @return string A URL which can be used for logging out. * @throws \InvalidArgumentException If $returnTo is neither a string nor null. */ - public static function getAdminLogoutURL($returnTo = null) + public static function getAdminLogoutURL(?string $returnTo = null) : string { - if (!(is_string($returnTo) || is_null($returnTo))) { - throw new \InvalidArgumentException('Invalid input parameters.'); - } - $as = new Authentication\Simple('admin'); return $as->getLogoutURL($returnTo = null); } @@ -64,12 +56,13 @@ class Auth * * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> */ - public static function isAdmin() + public static function isAdmin() : bool { $session = Session::getSessionFromRequest(); return $session->isValid('admin') || $session->isValid('login-admin'); } + /** * Require admin access to the current page. * @@ -82,7 +75,7 @@ class Auth * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> */ - public static function requireAdmin() + public static function requireAdmin() : void { if (self::isAdmin()) { return; diff --git a/lib/SimpleSAML/Utils/ClearableState.php b/lib/SimpleSAML/Utils/ClearableState.php index 60550261efdc93d6b4b7c8ea624fc752e5602e88..526037d5a8abc6f0418b03d97954fca3c192041c 100644 --- a/lib/SimpleSAML/Utils/ClearableState.php +++ b/lib/SimpleSAML/Utils/ClearableState.php @@ -16,5 +16,5 @@ interface ClearableState * Clear any cached internal state. * @return void */ - public static function clearInternalState(); + public static function clearInternalState() : void; } diff --git a/lib/SimpleSAML/Utils/Config.php b/lib/SimpleSAML/Utils/Config.php index 56fe3e0445473f607c826b8c05ae003b811a783e..ea7ef1f776f681ecb1dbab1f03c00573f99b4c63 100644 --- a/lib/SimpleSAML/Utils/Config.php +++ b/lib/SimpleSAML/Utils/Config.php @@ -23,12 +23,8 @@ class Config * * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> */ - public static function getCertPath($path) + public static function getCertPath(string $path) : string { - if (!is_string($path)) { - throw new \InvalidArgumentException('Invalid input parameters.'); - } - $globalConfig = Configuration::getInstance(); $base = $globalConfig->getPathValue('certdir', 'cert/'); return System::resolvePath($path, $base); @@ -50,7 +46,7 @@ class Config * * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> */ - public static function getSecretSalt() + public static function getSecretSalt() : string { $secretSalt = Configuration::getInstance()->getString('secretsalt'); if ($secretSalt === 'defaultsecretsalt') { @@ -68,15 +64,15 @@ class Config * * @return string The path to the configuration directory. */ - public static function getConfigDir() + public static function getConfigDir() : string { $configDir = dirname(dirname(dirname(__DIR__))) . '/config'; $configDirEnv = getenv('SIMPLESAMLPHP_CONFIG_DIR'); - + if ($configDirEnv === false) { $configDirEnv = getenv('REDIRECT_SIMPLESAMLPHP_CONFIG_DIR'); } - + if ($configDirEnv !== false) { if (!is_dir($configDirEnv)) { throw new \InvalidArgumentException( diff --git a/lib/SimpleSAML/Utils/Config/Metadata.php b/lib/SimpleSAML/Utils/Config/Metadata.php index 50e8126513dc2278e86440f1708d5f11e0b50716..2459d4310c11fe0e3a340e806874bc8ffa0fc60b 100644 --- a/lib/SimpleSAML/Utils/Config/Metadata.php +++ b/lib/SimpleSAML/Utils/Config/Metadata.php @@ -96,7 +96,7 @@ class Metadata * * otherwise it will just return the name as "givenName" in the resulting array. * - * @param array $contact The contact to parse and sanitize. + * @param array|null $contact The contact to parse and sanitize. * * @return array An array holding valid contact configuration options. If a key 'name' was part of the input array, * it will try to decompose the name into its parts, and place the parts into givenName and surName, if those are @@ -104,12 +104,8 @@ class Metadata * @throws \InvalidArgumentException If $contact is neither an array nor null, or the contact does not conform to * valid configuration rules for contacts. */ - public static function getContact($contact) + public static function getContact(?array $contact) : array { - if (!(is_array($contact) || is_null($contact))) { - throw new \InvalidArgumentException('Invalid input parameters'); - } - // check the type if (!isset($contact['contactType']) || !in_array($contact['contactType'], self::$VALID_CONTACT_TYPES, true)) { $types = join(', ', array_map( @@ -246,7 +242,7 @@ class Metadata * * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> */ - public static function getDefaultEndpoint(array $endpoints, array $bindings = null) + public static function getDefaultEndpoint(array $endpoints, array $bindings = null) : ?array { $firstNotFalse = null; $firstAllowed = null; @@ -300,7 +296,7 @@ class Metadata * * @return boolean True if the entity should be hidden, false otherwise. */ - public static function isHiddenFromDiscovery(array $metadata) + public static function isHiddenFromDiscovery(array $metadata) : bool { Logger::maskErrors(E_ALL); $hidden = in_array(self::$HIDE_FROM_DISCOVERY, $metadata['EntityAttributes'][self::$ENTITY_CATEGORY], true); @@ -316,7 +312,7 @@ class Metadata * * @return null|array */ - public static function parseNameIdPolicy($nameIdPolicy) + public static function parseNameIdPolicy($nameIdPolicy) : ?array { $policy = null; diff --git a/lib/SimpleSAML/Utils/Crypto.php b/lib/SimpleSAML/Utils/Crypto.php index 425eb56a48024a5682207b1d18ddfc5b1e692bbb..44752f4e5cf8ad82d6d792fc002e332035d35861 100644 --- a/lib/SimpleSAML/Utils/Crypto.php +++ b/lib/SimpleSAML/Utils/Crypto.php @@ -79,7 +79,7 @@ class Crypto * @author Andreas Solberg, UNINETT AS <andreas.solberg@uninett.no> * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> */ - public static function aesDecrypt($ciphertext) + public static function aesDecrypt(string $ciphertext): string { return self::aesDecryptInternal($ciphertext, Config::getSecretSalt()); } @@ -139,7 +139,7 @@ class Crypto * @author Andreas Solberg, UNINETT AS <andreas.solberg@uninett.no> * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> */ - public static function aesEncrypt($data) + public static function aesEncrypt(string $data): string { return self::aesEncryptInternal($data, Config::getSecretSalt()); } @@ -153,7 +153,7 @@ class Crypto * @return string The same data encoded in PEM format. * @see RFC7648 for known types and PEM format specifics. */ - public static function der2pem($der, $type = 'CERTIFICATE') + public static function der2pem(string $der, string $type = 'CERTIFICATE'): string { return "-----BEGIN " . $type . "-----\n" . chunk_split(base64_encode($der), 64, "\n") . @@ -188,12 +188,12 @@ class Crypto * @author Andreas Solberg, UNINETT AS <andreas.solberg@uninett.no> * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> */ - public static function loadPrivateKey(Configuration $metadata, $required = false, $prefix = '', $full_path = false) - { - if (!is_bool($required) || !is_string($prefix) || !is_bool($full_path)) { - throw new \InvalidArgumentException('Invalid input parameters.'); - } - + public static function loadPrivateKey( + Configuration $metadata, + bool $required = false, + string $prefix = '', + bool $full_path = false + ): ?array { $file = $metadata->getString($prefix . 'privatekey', null); if ($file === null) { // no private key found @@ -251,12 +251,8 @@ class Crypto * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> * @author Lasse Birnbaum Jensen */ - public static function loadPublicKey(Configuration $metadata, $required = false, $prefix = '') + public static function loadPublicKey(Configuration $metadata, bool $required = false, string $prefix = ''): ?array { - if (!is_bool($required) || !is_string($prefix)) { - throw new \InvalidArgumentException('Invalid input parameters.'); - } - $keys = $metadata->getPublicKeys(null, false, $prefix); if (!empty($keys)) { foreach ($keys as $key) { @@ -295,7 +291,7 @@ class Crypto * @throws \InvalidArgumentException If $pem is not encoded in PEM format. * @see RFC7648 for PEM format specifics. */ - public static function pem2der($pem) + public static function pem2der(string $pem): string { $pem = trim($pem); $begin = "-----BEGIN "; @@ -330,7 +326,7 @@ class Crypto * @author Dyonisius Visser, TERENA <visser@terena.org> * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> */ - public static function pwHash($password) + public static function pwHash(string $password): string { if (!is_string($password)) { throw new \InvalidArgumentException('Invalid input parameter.'); @@ -352,7 +348,7 @@ class Crypto * * @return bool True if both strings are equal, false otherwise. */ - public static function secureCompare($known, $user) + public static function secureCompare(string $known, string $user): bool { return hash_equals($known, $user); } @@ -370,12 +366,8 @@ class Crypto * * @author Dyonisius Visser, TERENA <visser@terena.org> */ - public static function pwValid($hash, $password) + public static function pwValid(string $hash, string $password): bool { - if (!is_string($hash) || !is_string($password)) { - throw new \InvalidArgumentException('Invalid input parameters.'); - } - if (password_verify($password, $hash)) { return true; } diff --git a/lib/SimpleSAML/Utils/EMail.php b/lib/SimpleSAML/Utils/EMail.php index dd861bc3803f2691afce2c46c539790147b26849..48fb3fac49b1eb3543c355a43c6c8ad8b8ae2e79 100644 --- a/lib/SimpleSAML/Utils/EMail.php +++ b/lib/SimpleSAML/Utils/EMail.php @@ -42,7 +42,7 @@ class EMail * * @throws \PHPMailer\PHPMailer\Exception */ - public function __construct($subject, $from = null, $to = null) + public function __construct(string $subject, string $from = null, string $to = null) { $this->mail = new PHPMailer(true); $this->mail->Subject = $subject; @@ -63,7 +63,7 @@ class EMail * * @return string Default mail address */ - public static function getDefaultMailAddress() + public static function getDefaultMailAddress(): string { $config = Configuration::getInstance(); $address = $config->getString('technicalcontact_email', 'na@example.org'); @@ -74,14 +74,14 @@ class EMail return $address; } - + /** * Set the data that should be embedded in the e-mail body * * @param array $data The data that should be embedded in the e-mail body * @return void */ - public function setData(array $data) + public function setData(array $data): void { /* * Convert every non-array value to an array with the original @@ -107,7 +107,7 @@ class EMail * @param string $text Introduction text * @return void */ - public function setText($text) + public function setText(string $text): void { $this->text = $text; } @@ -119,7 +119,7 @@ class EMail * @param string $address Reply-To e-mail address * @return void */ - public function addReplyTo($address) + public function addReplyTo(string $address): void { $this->mail->addReplyTo($address); } @@ -133,7 +133,7 @@ class EMail * * @throws \PHPMailer\PHPMailer\Exception */ - public function send($plainTextOnly = false) + public function send(bool $plainTextOnly = false): void { if ($plainTextOnly) { $this->mail->isHTML(false); @@ -147,6 +147,7 @@ class EMail $this->mail->send(); } + /** * Sets the method by which the email will be sent. Currently supports what * PHPMailer supports: sendmail, mail and smtp. @@ -158,10 +159,8 @@ class EMail * * @throws \InvalidArgumentException */ - public function setTransportMethod($transportMethod, array $transportOptions = []) + public function setTransportMethod(string $transportMethod, array $transportOptions = []): void { - Assert::string($transportMethod); - switch (strtolower($transportMethod)) { // smtp transport method case 'smtp': @@ -223,6 +222,7 @@ class EMail } } + /** * Initializes the provided EMail object with the configuration provided from the SimpleSAMLphp configuration. * @@ -230,10 +230,8 @@ class EMail * @return EMail * @throws \Exception */ - public static function initFromConfig(EMail $EMail) + public static function initFromConfig(EMail $EMail): EMail { - Assert::isInstanceOf($EMail, EMail::class); - $config = Configuration::getInstance(); $EMail->setTransportMethod( $config->getString('mail.transport.method', 'mail'), @@ -251,7 +249,7 @@ class EMail * * @return string The body of the e-mail */ - public function generateBody($template) + public function generateBody(string $template): string { $config = Configuration::getInstance(); $newui = $config->getBoolean('usenewui', false); diff --git a/lib/SimpleSAML/Utils/HTTP.php b/lib/SimpleSAML/Utils/HTTP.php index 18a78cd676340c9ec890b972ad191a33511bee6f..6ae9f2349f7262bb3f6ee4645d64f4baa97632ac 100644 --- a/lib/SimpleSAML/Utils/HTTP.php +++ b/lib/SimpleSAML/Utils/HTTP.php @@ -88,7 +88,7 @@ class HTTP * * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> */ - public static function getServerHTTPS() + public static function getServerHTTPS(): bool { if (!array_key_exists('HTTPS', $_SERVER)) { // not an https-request @@ -113,7 +113,7 @@ class HTTP * * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> */ - public static function getServerPort() + public static function getServerPort(): string { $default_port = self::getServerHTTPS() ? '443' : '80'; $port = isset($_SERVER['SERVER_PORT']) ? $_SERVER['SERVER_PORT'] : $default_port; @@ -135,7 +135,7 @@ class HTTP * * @return boolean True if the given URL is valid, false otherwise. */ - public static function isValidURL($url) + public static function isValidURL(string $url): bool { $url = filter_var($url, FILTER_VALIDATE_URL); if ($url === false) { @@ -173,11 +173,12 @@ class HTTP * @author Mads Freek Petersen * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> */ - private static function redirect(string $url, array $parameters = []) + private static function redirect(string $url, array $parameters = []): void { if (empty($url)) { throw new \InvalidArgumentException('Invalid input parameters.'); } + if (!self::isValidURL($url)) { throw new Error\Exception('Invalid destination URL.'); } @@ -277,12 +278,8 @@ class HTTP * @author Andreas Solberg, UNINETT AS <andreas.solberg@uninett.no> * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> */ - public static function addURLParameters($url, $parameters) + public static function addURLParameters(string $url, array $parameters): string { - if (!is_string($url) || !is_array($parameters)) { - throw new \InvalidArgumentException('Invalid input parameters.'); - } - $queryStart = strpos($url, '?'); if ($queryStart === false) { $oldQuery = []; @@ -315,12 +312,8 @@ class HTTP * * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> */ - public static function checkSessionCookie($retryURL = null) + public static function checkSessionCookie(?string $retryURL = null): void { - if (!is_null($retryURL) && !is_string($retryURL)) { - throw new \InvalidArgumentException('Invalid input parameters.'); - } - $session = Session::getSessionFromRequest(); if ($session->hasSessionCookie()) { return; @@ -350,7 +343,7 @@ class HTTP * * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> */ - public static function checkURLAllowed($url, array $trustedSites = null) + public static function checkURLAllowed(string $url, array $trustedSites = null): string { if (empty($url)) { return ''; @@ -442,12 +435,8 @@ class HTTP * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> * @author Marco Ferrante, University of Genova <marco@csita.unige.it> */ - public static function fetch($url, $context = [], $getHeaders = false) + public static function fetch(string $url, array $context = [], bool $getHeaders = false) { - if (!is_string($url)) { - throw new \InvalidArgumentException('Invalid input parameters.'); - } - $config = Configuration::getInstance(); $proxy = $config->getString('proxy', null); @@ -533,7 +522,7 @@ class HTTP * * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> */ - public static function getAcceptLanguage() + public static function getAcceptLanguage(): array { if (!array_key_exists('HTTP_ACCEPT_LANGUAGE', $_SERVER)) { // no Accept-Language header, return an empty set @@ -597,7 +586,7 @@ class HTTP * * @return string The guessed base path that should correspond to the root installation of SimpleSAMLphp. */ - public static function guessBasePath() + public static function guessBasePath(): string { if (!array_key_exists('REQUEST_URI', $_SERVER) || !array_key_exists('SCRIPT_FILENAME', $_SERVER)) { return '/'; @@ -632,7 +621,7 @@ class HTTP * * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> */ - public static function getBaseURL() + public static function getBaseURL(): string { $globalConfig = Configuration::getInstance(); $baseURL = $globalConfig->getString('baseurlpath', 'simplesaml/'); @@ -681,7 +670,7 @@ class HTTP * * @author Andreas Solberg, UNINETT AS <andreas.solberg@uninett.no> */ - public static function getFirstPathElement($leadingSlash = true) + public static function getFirstPathElement(bool $leadingSlash = true): string { if (preg_match('|^/(.*?)/|', $_SERVER['SCRIPT_NAME'], $matches)) { return ($leadingSlash ? '/' : '') . $matches[1]; @@ -702,12 +691,8 @@ class HTTP * @author Andjelko Horvat * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> */ - public static function getPOSTRedirectURL($destination, $data) + public static function getPOSTRedirectURL(string $destination, array $data): string { - if (!is_string($destination) || !is_array($data)) { - throw new \InvalidArgumentException('Invalid input parameters.'); - } - $config = Configuration::getInstance(); $allowed = $config->getBoolean('enable.http_post', false); @@ -734,12 +719,13 @@ class HTTP * * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> */ - public static function getSelfHost() + public static function getSelfHost(): string { $decomposed = explode(':', self::getSelfHostWithNonStandardPort()); return array_shift($decomposed); } + /** * Retrieve our own host, including the port in case the it is not standard for the protocol in use. That is port * 80 for HTTP and port 443 for HTTPS. @@ -752,7 +738,7 @@ class HTTP * @author Andreas Solberg, UNINETT AS <andreas.solberg@uninett.no> * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> */ - public static function getSelfHostWithNonStandardPort() + public static function getSelfHostWithNonStandardPort(): string { $url = self::getBaseURL(); @@ -774,7 +760,7 @@ class HTTP * @author Andreas Solberg, UNINETT AS <andreas.solberg@uninett.no> * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> */ - public static function getSelfHostWithPath() + public static function getSelfHostWithPath(): string { $baseurl = explode("/", self::getBaseURL()); $elements = array_slice($baseurl, 3 - count($baseurl), count($baseurl) - 4); @@ -798,7 +784,7 @@ class HTTP * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> */ - public static function getSelfURL() + public static function getSelfURL(): string { $cfg = Configuration::getInstance(); $baseDir = $cfg->getBaseDir(); @@ -861,7 +847,7 @@ class HTTP * @author Andreas Solberg, UNINETT AS <andreas.solberg@uninett.no> * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> */ - public static function getSelfURLHost() + public static function getSelfURLHost(): string { $url = self::getSelfURL(); @@ -881,7 +867,7 @@ class HTTP * @author Andreas Solberg, UNINETT AS <andreas.solberg@uninett.no> * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> */ - public static function getSelfURLNoQuery() + public static function getSelfURLNoQuery(): string { $url = self::getSelfURL(); $pos = strpos($url, '?'); @@ -900,7 +886,7 @@ class HTTP * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> */ - public static function isHTTPS() + public static function isHTTPS(): bool { return strpos(self::getSelfURL(), 'https://') === 0; } @@ -918,12 +904,8 @@ class HTTP * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> */ - public static function normalizeURL($url) + public static function normalizeURL(string $url): string { - if (!is_string($url)) { - throw new \InvalidArgumentException('Invalid input parameters.'); - } - $url = self::resolveURL($url, self::getSelfURL()); // verify that the URL is to a http or https site @@ -950,12 +932,8 @@ class HTTP * * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> */ - public static function parseQueryString($query_string) + public static function parseQueryString(string $query_string): array { - if (!is_string($query_string)) { - throw new \InvalidArgumentException('Invalid input parameters.'); - } - $res = []; if (empty($query_string)) { return $res; @@ -997,12 +975,8 @@ class HTTP * * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> */ - public static function redirectTrustedURL($url, $parameters = []) + public static function redirectTrustedURL(string $url, array $parameters = []): void { - if (!is_string($url) || !is_array($parameters)) { - throw new \InvalidArgumentException('Invalid input parameters.'); - } - $url = self::normalizeURL($url); self::redirect($url, $parameters); } @@ -1029,12 +1003,8 @@ class HTTP * * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> */ - public static function redirectUntrustedURL($url, $parameters = []) + public static function redirectUntrustedURL(string $url, array $parameters = []): void { - if (!is_string($url) || !is_array($parameters)) { - throw new \InvalidArgumentException('Invalid input parameters.'); - } - $url = self::checkURLAllowed($url); self::redirect($url, $parameters); } @@ -1061,16 +1031,12 @@ class HTTP * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> */ - public static function resolveURL($url, $base = null) + public static function resolveURL(string $url, string $base = null): string { if ($base === null) { $base = self::getBaseURL(); } - if (!is_string($url) || !is_string($base)) { - throw new \InvalidArgumentException('Invalid input parameters.'); - } - if (!preg_match('/^((((\w+:)\/\/[^\/]+)(\/[^?#]*))(?:\?[^#]*)?)(?:#.*)?/', $base, $baseParsed)) { throw new \InvalidArgumentException('Unable to parse base url: ' . $base); } @@ -1142,19 +1108,8 @@ class HTTP * @author Andjelko Horvat * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> */ - public static function setCookie($name, $value, $params = null, $throw = true) + public static function setCookie(string $name, ?string $value, array $params = null, bool $throw = true): void { - if ( - !(is_string($name) // $name must be a string - && (is_string($value) - || is_null($value)) // $value can be a string or null - && (is_array($params) - || is_null($params)) // $params can be an array or null - && is_bool($throw)) // $throw must be boolean - ) { - throw new \InvalidArgumentException('Invalid input parameters.'); - } - $default_params = [ 'lifetime' => 0, 'expire' => null, @@ -1281,11 +1236,8 @@ class HTTP * @author Andjelko Horvat * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> */ - public static function submitPOSTData($destination, $data) + public static function submitPOSTData(string $destination, array $data): void { - if (!is_string($destination) || !is_array($data)) { - throw new \InvalidArgumentException('Invalid input parameters.'); - } if (!self::isValidURL($destination)) { throw new Error\Exception('Invalid destination URL.'); } diff --git a/lib/SimpleSAML/Utils/HttpAdapter.php b/lib/SimpleSAML/Utils/HttpAdapter.php index 8065da898514401ba2e20c44255308c0c3f98f6e..d5a833b653f1f9dac8fa4291e7115a3f9dc6f129 100644 --- a/lib/SimpleSAML/Utils/HttpAdapter.php +++ b/lib/SimpleSAML/Utils/HttpAdapter.php @@ -15,7 +15,7 @@ class HttpAdapter * @see HTTP::getServerHTTPS() * @return bool */ - public function getServerHTTPS() + public function getServerHTTPS() : bool { return HTTP::getServerHTTPS(); } @@ -24,7 +24,7 @@ class HttpAdapter * @see HTTP::getServerPort() * @return string */ - public function getServerPort() + public function getServerPort() : string { return HTTP::getServerPort(); } @@ -36,7 +36,7 @@ class HttpAdapter * @param array $parameters * @return string */ - public function addURLParameters($url, $parameters) + public function addURLParameters(string $url, array $parameters) : string { return HTTP::addURLParameters($url, $parameters); } @@ -47,7 +47,7 @@ class HttpAdapter * @param string|null $retryURL * @return void */ - public function checkSessionCookie($retryURL = null) + public function checkSessionCookie(string $retryURL = null) : void { HTTP::checkSessionCookie($retryURL); } @@ -59,7 +59,7 @@ class HttpAdapter * @param array|null $trustedSites * @return string */ - public function checkURLAllowed($url, array $trustedSites = null) + public function checkURLAllowed(string $url, array $trustedSites = null) : string { return HTTP::checkURLAllowed($url, $trustedSites); } @@ -72,7 +72,7 @@ class HttpAdapter * @param bool $getHeaders * @return array|string */ - public function fetch($url, $context = [], $getHeaders = false) + public function fetch(string $url, array $context = [], bool $getHeaders = false) { return HTTP::fetch($url, $context, $getHeaders); } @@ -81,7 +81,7 @@ class HttpAdapter * @see HTTP::getAcceptLanguage() * @return array */ - public function getAcceptLanguage() + public function getAcceptLanguage() : array { return HTTP::getAcceptLanguage(); } @@ -90,7 +90,7 @@ class HttpAdapter * @see HTTP::guessBasePath() * @return string */ - public function guessBasePath() + public function guessBasePath() : string { return HTTP::guessBasePath(); } @@ -99,7 +99,7 @@ class HttpAdapter * @see HTTP::getBaseURL() * @return string */ - public function getBaseURL() + public function getBaseURL() : string { return HTTP::getBaseURL(); } @@ -110,7 +110,7 @@ class HttpAdapter * @param bool $trailingslash * @return string */ - public function getFirstPathElement($trailingslash = true) + public function getFirstPathElement(bool $trailingslash = true) : string { return HTTP::getFirstPathElement($trailingslash); } @@ -122,7 +122,7 @@ class HttpAdapter * @param array $data * @return string */ - public function getPOSTRedirectURL($destination, $data) + public function getPOSTRedirectURL(string $destination, array $data) : string { return HTTP::getPOSTRedirectURL($destination, $data); } @@ -131,7 +131,7 @@ class HttpAdapter * @see HTTP::getSelfHost() * @return string */ - public function getSelfHost() + public function getSelfHost() : string { return HTTP::getSelfHost(); } @@ -140,7 +140,7 @@ class HttpAdapter * @see HTTP::getSelfHostWithNonStandardPort() * @return string */ - public function getSelfHostWithNonStandardPort() + public function getSelfHostWithNonStandardPort() : string { return HTTP::getSelfHostWithNonStandardPort(); } @@ -149,7 +149,7 @@ class HttpAdapter * @see HTTP::getSelfHostWithPath() * @return string */ - public function getSelfHostWithPath() + public function getSelfHostWithPath() : string { return HTTP::getSelfHostWithPath(); } @@ -158,7 +158,7 @@ class HttpAdapter * @see HTTP::getSelfURL() * @return string */ - public function getSelfURL() + public function getSelfURL() : string { return HTTP::getSelfURL(); } @@ -167,7 +167,7 @@ class HttpAdapter * @see HTTP::getSelfURLHost() * @return string */ - public function getSelfURLHost() + public function getSelfURLHost() : string { return HTTP::getSelfURLHost(); } @@ -176,7 +176,7 @@ class HttpAdapter * @see HTTP::getSelfURLNoQuery() * @return string */ - public function getSelfURLNoQuery() + public function getSelfURLNoQuery() : string { return HTTP::getSelfURLNoQuery(); } @@ -185,7 +185,7 @@ class HttpAdapter * @see HTTP::isHTTPS() * @return bool */ - public function isHTTPS() + public function isHTTPS() : bool { return HTTP::isHTTPS(); } @@ -195,7 +195,7 @@ class HttpAdapter * @param string $url * @return string */ - public function normalizeURL($url) + public function normalizeURL(string $url) : string { return HTTP::normalizeURL($url); } @@ -206,7 +206,7 @@ class HttpAdapter * @param string $query_string * @return array */ - public function parseQueryString($query_string) + public function parseQueryString(string $query_string) : array { return HTTP::parseQueryString($query_string); } @@ -218,7 +218,7 @@ class HttpAdapter * @param array $parameters * @return void */ - public function redirectTrustedURL($url, $parameters = []) + public function redirectTrustedURL(string $url, array $parameters = []) : void { HTTP::redirectTrustedURL($url, $parameters); } @@ -230,7 +230,7 @@ class HttpAdapter * @param array $parameters * @return void */ - public function redirectUntrustedURL($url, $parameters = []) + public function redirectUntrustedURL(string $url, array $parameters = []) : void { HTTP::redirectUntrustedURL($url, $parameters); } @@ -242,7 +242,7 @@ class HttpAdapter * @param string|null $base * @return string */ - public function resolveURL($url, $base = null) + public function resolveURL(string $url, string $base = null) : string { return HTTP::resolveURL($url, $base); } @@ -256,7 +256,7 @@ class HttpAdapter * @param bool $throw * @return void */ - public function setCookie($name, $value, $params = null, $throw = true) + public function setCookie(string $name, string $value, array $params = null, bool $throw = true) : void { HTTP::setCookie($name, $value, $params, $throw); } @@ -268,7 +268,7 @@ class HttpAdapter * @param array $data * @return void */ - public function submitPOSTData($destination, $data) + public function submitPOSTData(string $destination, array $data) : void { HTTP::submitPOSTData($destination, $data); } diff --git a/lib/SimpleSAML/Utils/Net.php b/lib/SimpleSAML/Utils/Net.php index 8fe77816130f860f4fa725cec01d1fd870825fbb..5e0a3453f451209bddefc18c65d7689b96da861b 100644 --- a/lib/SimpleSAML/Utils/Net.php +++ b/lib/SimpleSAML/Utils/Net.php @@ -25,7 +25,7 @@ class Net * @author Brook Schofield, GÉANT * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> */ - public static function ipCIDRcheck($cidr, $ip = null) + public static function ipCIDRcheck(string $cidr, string $ip = null) : bool { if ($ip === null) { $ip = $_SERVER['REMOTE_ADDR']; diff --git a/lib/SimpleSAML/Utils/Random.php b/lib/SimpleSAML/Utils/Random.php index 417e5a6dc09e1283645cd65039cacad5183cd3b8..1709038de14489812fbf46532e6fbe48e3082aac 100644 --- a/lib/SimpleSAML/Utils/Random.php +++ b/lib/SimpleSAML/Utils/Random.php @@ -25,7 +25,7 @@ class Random * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> */ - public static function generateID() + public static function generateID() : string { return '_' . bin2hex(openssl_random_pseudo_bytes((int) ((self::ID_LENGTH - 1) / 2))); } diff --git a/lib/SimpleSAML/Utils/System.php b/lib/SimpleSAML/Utils/System.php index c9d8c567646859624c145f7d3fb857aa74a3475c..b2a1c31557672f65df846a9d087bc1da82ff8baf 100644 --- a/lib/SimpleSAML/Utils/System.php +++ b/lib/SimpleSAML/Utils/System.php @@ -73,7 +73,7 @@ class System * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> */ - public static function getTempDir() + public static function getTempDir(): string { $globalConfig = Configuration::getInstance(); @@ -123,7 +123,7 @@ class System * * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> */ - public static function resolvePath($path, $base = null) + public static function resolvePath(string $path, string $base = null): string { if ($base === null) { $config = Configuration::getInstance(); @@ -181,6 +181,8 @@ class System * @param string $data The data we should write to the file. * @param int $mode The permissions to apply to the file. Defaults to 0600. * + * @return void + * * @throws \InvalidArgumentException If any of the input parameters doesn't have the proper types. * @throws Error\Exception If the file cannot be saved, permissions cannot be changed or it is not * possible to write to the target file. @@ -189,15 +191,9 @@ class System * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> * @author Andjelko Horvat * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> - * - * @return void */ - public static function writeFile($filename, $data, $mode = 0600) + public static function writeFile(string $filename, string $data, int $mode = 0600): void { - if (!is_string($filename) || !is_string($data) || !is_numeric($mode)) { - throw new \InvalidArgumentException('Invalid input parameters'); - } - $tmpFile = self::getTempDir() . DIRECTORY_SEPARATOR . rand(); $res = @file_put_contents($tmpFile, $data); diff --git a/lib/SimpleSAML/Utils/Time.php b/lib/SimpleSAML/Utils/Time.php index efb77faccc9676cff942db73ee3035b33cc0a153..d47a707a7e4f47a28f43b643acc9c2816a2cbb6d 100644 --- a/lib/SimpleSAML/Utils/Time.php +++ b/lib/SimpleSAML/Utils/Time.php @@ -32,7 +32,7 @@ class Time * @return string The timestamp. * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> */ - public static function generateTimestamp($instant = null) + public static function generateTimestamp(int $instant = null) : string { if ($instant === null) { $instant = time(); @@ -48,11 +48,10 @@ class Time * * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> * - * @throws \SimpleSAML\Error\Exception If the timezone set in the configuration is invalid. - * * @return void + * @throws \SimpleSAML\Error\Exception If the timezone set in the configuration is invalid. */ - public static function initTimezone() + public static function initTimezone() : void { if (self::$tz_initialized) { return; @@ -92,12 +91,8 @@ class Time * @throws \InvalidArgumentException If $duration is not a valid ISO 8601 duration or if the input parameters do * not have the right data types. */ - public static function parseDuration($duration, $timestamp = null) + public static function parseDuration(string $duration, int $timestamp = null) : int { - if (!(is_string($duration) && (is_int($timestamp) || is_null($timestamp)))) { - throw new \InvalidArgumentException('Invalid input parameters'); - } - // parse the duration. We use a very strict pattern $durationRegEx = '#^(-?)P(?:(?:(?:(\\d+)Y)?(?:(\\d+)M)?(?:(\\d+)D)?(?:T(?:(\\d+)H)?(?:(\\d+)M)?(?:(\\d+)' . '(?:[.,]\d+)?S)?)?)|(?:(\\d+)W))$#D'; diff --git a/lib/SimpleSAML/Utils/XML.php b/lib/SimpleSAML/Utils/XML.php index 6f97ccb4e8e766b579a4a6bcc73d508e1408ce59..46c3ef91f487da4c724f65d9e41bc2ba3e18d3ae 100644 --- a/lib/SimpleSAML/Utils/XML.php +++ b/lib/SimpleSAML/Utils/XML.php @@ -43,10 +43,10 @@ class XML * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> * @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no> */ - public static function checkSAMLMessage($message, $type) + public static function checkSAMLMessage(string $message, string $type) : void { $allowed_types = ['saml20', 'saml11', 'saml-meta']; - if (!(is_string($message) && in_array($type, $allowed_types, true))) { + if (!in_array($type, $allowed_types, true)) { throw new \InvalidArgumentException('Invalid input parameters.'); } @@ -105,9 +105,9 @@ class XML * * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> */ - public static function debugSAMLMessage($message, $type) + public static function debugSAMLMessage($message, string $type) : void { - if (!(is_string($type) && (is_string($message) || $message instanceof DOMElement))) { + if (!(is_string($message) || $message instanceof DOMElement)) { throw new \InvalidArgumentException('Invalid input parameters.'); } @@ -171,12 +171,8 @@ class XML * * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> */ - public static function formatDOMElement(DOMNode $root, $indentBase = '') + public static function formatDOMElement(DOMNode $root, string $indentBase = '') : void { - if (!is_string($indentBase)) { - throw new \InvalidArgumentException('Invalid input parameters'); - } - // check what this element contains $fullText = ''; // all text in this element $textNodes = []; // text nodes which should be deleted @@ -261,12 +257,8 @@ class XML * * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> */ - public static function formatXMLString($xml, $indentBase = '') + public static function formatXMLString(string $xml, string $indentBase = '') : string { - if (!is_string($xml) || !is_string($indentBase)) { - throw new \InvalidArgumentException('Invalid input parameters'); - } - try { $doc = DOMDocumentFactory::fromString($xml); } catch (\Exception $e) { @@ -295,12 +287,8 @@ class XML * @throws \InvalidArgumentException If $element is not an instance of DOMElement, $localName is not a string or * $namespaceURI is not a string. */ - public static function getDOMChildren(DOMNode $element, $localName, $namespaceURI) + public static function getDOMChildren(DOMNode $element, string $localName, string $namespaceURI) : array { - if (!is_string($localName) || !is_string($namespaceURI)) { - throw new \InvalidArgumentException('Invalid input parameters.'); - } - $ret = []; for ($i = 0; $i < $element->childNodes->length; $i++) { @@ -331,7 +319,7 @@ class XML * * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> */ - public static function getDOMText(DOMElement $element) + public static function getDOMText(DOMElement $element) : string { $txt = ''; @@ -372,9 +360,9 @@ class XML * @author Andreas Solberg, UNINETT AS <andreas.solberg@uninett.no> * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> */ - public static function isDOMNodeOfType(DOMNode $element, $name, $nsURI) + public static function isDOMNodeOfType(DOMNode $element, string $name, string $nsURI) : bool { - if (!is_string($name) || !is_string($nsURI) || strlen($nsURI) === 0) { + if (strlen($nsURI) === 0) { // most likely a comment-node return false; } @@ -426,9 +414,9 @@ class XML * * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> */ - public static function isValid($xml, $schema) + public static function isValid($xml, string $schema) { - if (!(is_string($schema) && (is_string($xml) || $xml instanceof DOMDocument))) { + if (!(is_string($xml) || $xml instanceof DOMDocument))) { throw new \InvalidArgumentException('Invalid input parameters.'); } diff --git a/tests/lib/SimpleSAML/Utils/ArraysTest.php b/tests/lib/SimpleSAML/Utils/ArraysTest.php index 8bfe6bccc960166f284ff71c3bfd63972dae8504..d99e2072932805820d0c778806ba085500885635 100644 --- a/tests/lib/SimpleSAML/Utils/ArraysTest.php +++ b/tests/lib/SimpleSAML/Utils/ArraysTest.php @@ -46,10 +46,6 @@ class ArraysTest extends TestCase */ public function testTranspose() { - // check not array - /** @psalm-suppress InvalidArgument Can be removed as soon as the codebase is fully typehinted */ - $this->assertFalse(Arrays::transpose('string')); - // check bad arrays $this->assertFalse( Arrays::transpose(['1', '2', '3']), diff --git a/tests/lib/SimpleSAML/Utils/Config/MetadataTest.php b/tests/lib/SimpleSAML/Utils/Config/MetadataTest.php index d5ef54e2ff99b5e3e894e03cf57dfa2786b57c26..7264c87a456eaf3dddfe6af09920f498bc3cf893 100644 --- a/tests/lib/SimpleSAML/Utils/Config/MetadataTest.php +++ b/tests/lib/SimpleSAML/Utils/Config/MetadataTest.php @@ -20,14 +20,6 @@ class MetadataTest extends TestCase */ public function testGetContact() { - // test invalid argument - try { - /** @psalm-suppress InvalidArgument May be removed in 2.0 when codebase is fully typehinted */ - Metadata::getContact('string'); - } catch (\InvalidArgumentException $e) { - $this->assertEquals('Invalid input parameters', $e->getMessage()); - } - // test missing type $contact = [ 'name' => 'John Doe'