From cc64093daf630993bb7209193069a4602b179c65 Mon Sep 17 00:00:00 2001 From: Tim van Dijen <tvdijen@gmail.com> Date: Wed, 14 Aug 2019 17:22:42 +0200 Subject: [PATCH] Fully typehint lib/Metadata/*.php --- .../Metadata/MetaDataStorageHandler.php | 38 +++++------- .../MetaDataStorageHandlerFlatFile.php | 8 +-- .../Metadata/MetaDataStorageHandlerPdo.php | 23 +++---- .../MetaDataStorageHandlerSerialize.php | 29 +++------ .../Metadata/MetaDataStorageHandlerXML.php | 4 +- .../Metadata/MetaDataStorageSource.php | 43 +++++-------- lib/SimpleSAML/Metadata/SAMLBuilder.php | 48 ++++++--------- lib/SimpleSAML/Metadata/SAMLParser.php | 61 ++++++++++--------- lib/SimpleSAML/Metadata/Signer.php | 2 +- lib/SimpleSAML/Metadata/Sources/MDQ.php | 31 ++++++++-- 10 files changed, 125 insertions(+), 162 deletions(-) diff --git a/lib/SimpleSAML/Metadata/MetaDataStorageHandler.php b/lib/SimpleSAML/Metadata/MetaDataStorageHandler.php index 292cdee7c..6d3e4b1c9 100644 --- a/lib/SimpleSAML/Metadata/MetaDataStorageHandler.php +++ b/lib/SimpleSAML/Metadata/MetaDataStorageHandler.php @@ -49,7 +49,7 @@ class MetaDataStorageHandler implements \SimpleSAML\Utils\ClearableState * * @return MetaDataStorageHandler The current metadata handler instance. */ - public static function getMetadataHandler() + public static function getMetadataHandler(): MetaDataStorageHandler { if (self::$metadataHandler === null) { self::$metadataHandler = new MetaDataStorageHandler(); @@ -91,10 +91,10 @@ class MetaDataStorageHandler implements \SimpleSAML\Utils\ClearableState * @param string $property The metadata property which should be auto-generated. * @param string $set The set we the property comes from. * - * @return string The auto-generated metadata property. + * @return string|array The auto-generated metadata property. * @throws \Exception If the metadata cannot be generated automatically. */ - public function getGenerated($property, $set) + public function getGenerated(string $property, string $set) { // first we check if the user has overridden this property in the metadata try { @@ -145,10 +145,8 @@ class MetaDataStorageHandler implements \SimpleSAML\Utils\ClearableState * * @return array An associative array with the metadata from from the given set. */ - public function getList($set = 'saml20-idp-remote', $showExpired = false) + public function getList(string $set = 'saml20-idp-remote', bool $showExpired = false): array { - Assert::string($set); - $result = []; foreach ($this->sources as $source) { @@ -184,7 +182,7 @@ class MetaDataStorageHandler implements \SimpleSAML\Utils\ClearableState * * @return array An associative array with the metadata. */ - public function getMetaDataCurrent($set) + public function getMetaDataCurrent(string $set): array { return $this->getMetaData(null, $set); } @@ -200,10 +198,8 @@ class MetaDataStorageHandler implements \SimpleSAML\Utils\ClearableState * @return string The entity id which is associated with the current hostname/path combination. * @throws \Exception If no default metadata can be found in the set for the current host. */ - public function getMetaDataCurrentEntityID($set, $type = 'entityid') + public function getMetaDataCurrentEntityID(string $set, string $type = 'entityid'): string { - Assert::string($set); - // first we look for the hostname/path combination $currenthostwithpath = Utils\HTTP::getSelfHostWithPath(); // sp.example.org/university @@ -250,7 +246,7 @@ class MetaDataStorageHandler implements \SimpleSAML\Utils\ClearableState * @return string|null The entity id of a entity which have a CIDR hint where the provided * IP address match. */ - public function getPreferredEntityIdFromCIDRhint($set, $ip) + public function getPreferredEntityIdFromCIDRhint(string $set, string $ip): ?string { foreach ($this->sources as $source) { $entityId = $source->getPreferredEntityIdFromCIDRhint($set, $ip); @@ -262,6 +258,7 @@ class MetaDataStorageHandler implements \SimpleSAML\Utils\ClearableState return null; } + /** * This function loads the metadata for entity IDs in $entityIds. It is returned as an associative array * where the key is the entity id. An empty array may be returned if no matching entities were found @@ -269,7 +266,7 @@ class MetaDataStorageHandler implements \SimpleSAML\Utils\ClearableState * @param string $set The set we want to get metadata from. * @return array An associative array with the metadata for the requested entities, if found. */ - public function getMetaDataForEntities(array $entityIds, $set) + public function getMetaDataForEntities(array $entityIds, string $set): array { $result = []; foreach ($this->sources as $source) { @@ -295,6 +292,7 @@ class MetaDataStorageHandler implements \SimpleSAML\Utils\ClearableState return $result; } + /** * This function looks up the metadata for the given entity id in the given set. It will throw an * exception if it is unable to locate the metadata. @@ -307,10 +305,8 @@ class MetaDataStorageHandler implements \SimpleSAML\Utils\ClearableState * @throws \Exception If metadata for the specified entity is expired. * @throws \SimpleSAML\Error\MetadataNotFound If no metadata for the entity specified can be found. */ - public function getMetaData($index, $set) + public function getMetaData(?string $index, string $set): array { - Assert::string($set); - if ($index === null) { $index = $this->getMetaDataCurrentEntityID($set, 'metaindex'); } @@ -352,11 +348,8 @@ class MetaDataStorageHandler implements \SimpleSAML\Utils\ClearableState * @return \SimpleSAML\Configuration The configuration object representing the metadata. * @throws \SimpleSAML\Error\MetadataNotFound If no metadata for the entity specified can be found. */ - public function getMetaDataConfig($entityId, $set) + public function getMetaDataConfig(string $entityId, string $set): Configuration { - Assert::string($entityId); - Assert::string($set); - $metadata = $this->getMetaData($entityId, $set); return Configuration::loadFromArray($metadata, $set . '/' . var_export($entityId, true)); } @@ -371,11 +364,8 @@ class MetaDataStorageHandler implements \SimpleSAML\Utils\ClearableState * @return null|\SimpleSAML\Configuration The metadata corresponding to the entity, or null if the entity cannot be * found. */ - public function getMetaDataConfigForSha1($sha1, $set) + public function getMetaDataConfigForSha1(string $sha1, string $set): ?Configuration { - Assert::string($sha1); - Assert::string($set); - $result = []; foreach ($this->sources as $source) { @@ -407,7 +397,7 @@ class MetaDataStorageHandler implements \SimpleSAML\Utils\ClearableState * when running phpunit tests and needing to alter config.php and metadata sources between test cases * @return void */ - public static function clearInternalState() + public static function clearInternalState(): void { self::$metadataHandler = null; } diff --git a/lib/SimpleSAML/Metadata/MetaDataStorageHandlerFlatFile.php b/lib/SimpleSAML/Metadata/MetaDataStorageHandlerFlatFile.php index b1921c9a3..36a1b894b 100644 --- a/lib/SimpleSAML/Metadata/MetaDataStorageHandlerFlatFile.php +++ b/lib/SimpleSAML/Metadata/MetaDataStorageHandlerFlatFile.php @@ -44,10 +44,8 @@ class MetaDataStorageHandlerFlatFile extends MetaDataStorageSource * * @param array $config An associative array with the configuration for this handler. */ - protected function __construct($config) + protected function __construct(array $config) { - Assert::isArray($config); - // get the configuration $globalConfig = Configuration::getInstance(); @@ -78,7 +76,7 @@ class MetaDataStorageHandlerFlatFile extends MetaDataStorageSource * or null if we are unable to load metadata from the given file. * @throws \Exception If the metadata set cannot be loaded. */ - private function load(string $set) + private function load(string $set): ?array { $metadatasetfile = $this->directory . $set . '.php'; @@ -108,7 +106,7 @@ class MetaDataStorageHandlerFlatFile extends MetaDataStorageSource * @return array An associative array with the metadata. Each element in the array is an entity, and the * key is the entity id. */ - public function getMetadataSet($set) + public function getMetadataSet(string $set): array { if (array_key_exists($set, $this->cachedMetadata)) { return $this->cachedMetadata[$set]; diff --git a/lib/SimpleSAML/Metadata/MetaDataStorageHandlerPdo.php b/lib/SimpleSAML/Metadata/MetaDataStorageHandlerPdo.php index 27d185d7b..2ab95ddba 100644 --- a/lib/SimpleSAML/Metadata/MetaDataStorageHandlerPdo.php +++ b/lib/SimpleSAML/Metadata/MetaDataStorageHandlerPdo.php @@ -58,10 +58,8 @@ class MetaDataStorageHandlerPdo extends MetaDataStorageSource * * @param array $config An associative array with the configuration for this handler. */ - public function __construct($config) + public function __construct(array $config) { - Assert::isArray($config); - $this->db = Database::getInstance(); } @@ -78,7 +76,7 @@ class MetaDataStorageHandlerPdo extends MetaDataStorageSource * @throws \Exception If a database error occurs. * @throws \SimpleSAML\Error\Exception If the metadata can be retrieved from the database, but cannot be decoded. */ - private function load(string $set) + private function load(string $set): ?array { $tableName = $this->getTableName($set); @@ -117,10 +115,8 @@ class MetaDataStorageHandlerPdo extends MetaDataStorageSource * * @return array $metadata An associative array with all the metadata for the given set. */ - public function getMetadataSet($set) + public function getMetadataSet(string $set): array { - Assert::string($set); - if (array_key_exists($set, $this->cachedMetadata)) { return $this->cachedMetadata[$set]; } @@ -138,6 +134,7 @@ class MetaDataStorageHandlerPdo extends MetaDataStorageSource return $metadataSet; } + /** * Retrieve a metadata entry. * @@ -147,11 +144,8 @@ class MetaDataStorageHandlerPdo extends MetaDataStorageSource * @return array|null An associative array with metadata for the given entity, or NULL if we are unable to * locate the entity. */ - public function getMetaData($entityId, $set) + public function getMetaData(string $entityId, string $set): ?array { - Assert::string($entityId); - Assert::string($set); - // validate the metadata set is valid if (!in_array($set, $this->supportedSets, true)) { return null; @@ -212,6 +206,7 @@ class MetaDataStorageHandlerPdo extends MetaDataStorageSource return null; } + /** * Add metadata to the configured database * @@ -221,12 +216,8 @@ class MetaDataStorageHandlerPdo extends MetaDataStorageSource * * @return bool True/False if entry was successfully added */ - public function addEntry($index, $set, $entityData) + public function addEntry(string $index, string $set, array $entityData): bool { - Assert::string($index); - Assert::string($set); - assert::isArray($entityData); - if (!in_array($set, $this->supportedSets, true)) { return false; } diff --git a/lib/SimpleSAML/Metadata/MetaDataStorageHandlerSerialize.php b/lib/SimpleSAML/Metadata/MetaDataStorageHandlerSerialize.php index b797b1da7..4a81c14c2 100644 --- a/lib/SimpleSAML/Metadata/MetaDataStorageHandlerSerialize.php +++ b/lib/SimpleSAML/Metadata/MetaDataStorageHandlerSerialize.php @@ -40,10 +40,8 @@ class MetaDataStorageHandlerSerialize extends MetaDataStorageSource * * @param array $config The configuration for this metadata handler. */ - public function __construct($config) + public function __construct(array $config) { - Assert::isArray($config); - $globalConfig = Configuration::getInstance(); $cfgHelp = Configuration::loadFromArray($config, 'serialize metadata source'); @@ -76,7 +74,7 @@ class MetaDataStorageHandlerSerialize extends MetaDataStorageSource * * @return array An array with the available sets. */ - public function getMetadataSets() + public function getMetadataSets(): array { $ret = []; @@ -120,10 +118,8 @@ class MetaDataStorageHandlerSerialize extends MetaDataStorageSource * * @return array An associative array with all the metadata for the given set. */ - public function getMetadataSet($set) + public function getMetadataSet(string $set): array { - Assert::string($set); - $ret = []; $dir = $this->directory . '/' . rawurlencode($set); @@ -175,11 +171,8 @@ class MetaDataStorageHandlerSerialize extends MetaDataStorageSource * @return array|null An associative array with metadata for the given entity, or NULL if we are unable to * locate the entity. */ - public function getMetaData($entityId, $set) + public function getMetaData(string $entityId, string $set): ?array { - Assert::string($entityId); - Assert::string($set); - $filePath = $this->getMetadataPath($entityId, $set); if (!file_exists($filePath)) { @@ -219,12 +212,8 @@ class MetaDataStorageHandlerSerialize extends MetaDataStorageSource * * @return bool True if successfully saved, false otherwise. */ - public function saveMetadata($entityId, $set, $metadata) + public function saveMetadata(string $entityId, string $set, array $metadata): bool { - Assert::string($entityId); - Assert::string($set); - Assert::isArray($metadata); - $filePath = $this->getMetadataPath($entityId, $set); $newPath = $filePath . '.new'; @@ -271,11 +260,8 @@ class MetaDataStorageHandlerSerialize extends MetaDataStorageSource * @param string $set The metadata set this metadata entry belongs to. * @return void */ - public function deleteMetadata($entityId, $set) + public function deleteMetadata(string $entityId, string $set): void { - Assert::string($entityId); - Assert::string($set); - $filePath = $this->getMetadataPath($entityId, $set); if (!file_exists($filePath)) { @@ -297,6 +283,7 @@ class MetaDataStorageHandlerSerialize extends MetaDataStorageSource } } + /** * This function loads the metadata for entity IDs in $entityIds. It is returned as an associative array * where the key is the entity id. An empty array may be returned if no matching entities were found @@ -304,7 +291,7 @@ class MetaDataStorageHandlerSerialize extends MetaDataStorageSource * @param string $set The set we want to get metadata from. * @return array An associative array with the metadata for the requested entities, if found. */ - public function getMetaDataForEntities(array $entityIds, $set) + public function getMetaDataForEntities(array $entityIds, string $set): array { return $this->getMetaDataForEntitiesIndividually($entityIds, $set); } diff --git a/lib/SimpleSAML/Metadata/MetaDataStorageHandlerXML.php b/lib/SimpleSAML/Metadata/MetaDataStorageHandlerXML.php index 523b52e34..d73443195 100644 --- a/lib/SimpleSAML/Metadata/MetaDataStorageHandlerXML.php +++ b/lib/SimpleSAML/Metadata/MetaDataStorageHandlerXML.php @@ -35,7 +35,7 @@ class MetaDataStorageHandlerXML extends MetaDataStorageSource * * @throws \Exception If neither the 'file' or 'url' options are defined in the configuration. */ - protected function __construct($config) + protected function __construct(array $config) { $src = $srcXml = null; if (array_key_exists('file', $config)) { @@ -97,7 +97,7 @@ class MetaDataStorageHandlerXML extends MetaDataStorageSource * * @return array An associative array with all entities in the given set. */ - public function getMetadataSet($set) + public function getMetadataSet(string $set): array { if (array_key_exists($set, $this->metadata)) { return $this->metadata[$set]; diff --git a/lib/SimpleSAML/Metadata/MetaDataStorageSource.php b/lib/SimpleSAML/Metadata/MetaDataStorageSource.php index 9a32daa8e..a36807ca4 100644 --- a/lib/SimpleSAML/Metadata/MetaDataStorageSource.php +++ b/lib/SimpleSAML/Metadata/MetaDataStorageSource.php @@ -35,10 +35,8 @@ abstract class MetaDataStorageSource * * @throws \Exception If something is wrong in the configuration. */ - public static function parseSources($sourcesConfig) + public static function parseSources(array $sourcesConfig): array { - Assert::isArray($sourcesConfig); - $sources = []; foreach ($sourcesConfig as $sourceConfig) { @@ -64,10 +62,8 @@ abstract class MetaDataStorageSource * * @throws \Exception If the metadata source type is invalid. */ - public static function getSource($sourceConfig) + public static function getSource(array $sourceConfig): MetaDataStorageSource { - Assert::isArray($sourceConfig); - if (array_key_exists('type', $sourceConfig)) { $type = $sourceConfig['type']; } else { @@ -118,7 +114,7 @@ abstract class MetaDataStorageSource * @return array An associative array with all entities in the given set, or an empty array if we are * unable to generate this list. */ - public function getMetadataSet($set) + public function getMetadataSet(string $set): array { return []; } @@ -138,15 +134,9 @@ abstract class MetaDataStorageSource * @return string|null An entity id which matches the given host/path combination, or NULL if * we are unable to locate one which matches. */ - public function getEntityIdFromHostPath($hostPath, $set, $type = 'entityid') + public function getEntityIdFromHostPath(string $hostPath, string $set, string $type = 'entityid'): ?string { - $metadataSet = $this->getMetadataSet($set); - /** @psalm-suppress DocblockTypeContradiction */ - if ($metadataSet === null) { - // this metadata source does not have this metadata set - return null; - } foreach ($metadataSet as $index => $entry) { if (!array_key_exists('host', $entry)) { @@ -180,7 +170,7 @@ abstract class MetaDataStorageSource * @return string|null The entity id of a entity which have a CIDR hint where the provided * IP address match. */ - public function getPreferredEntityIdFromCIDRhint($set, $ip, $type = 'entityid') + public function getPreferredEntityIdFromCIDRhint(string $set, string $ip, string $type = 'entityid'): ?string { $metadataSet = $this->getMetadataSet($set); @@ -236,12 +226,8 @@ abstract class MetaDataStorageSource * @return array|null An associative array with metadata for the given entity, or NULL if we are unable to * locate the entity. */ - public function getMetaData($index, $set) + public function getMetaData(string $index, string $set): ?array { - - Assert::string($index); - Assert::notNull($set); - $metadataSet = $this->getMetadataSet($set); $indexLookup = $this->lookupIndexFromEntityId($index, $metadataSet); @@ -252,6 +238,7 @@ abstract class MetaDataStorageSource return null; } + /** * This function loads the metadata for entity IDs in $entityIds. It is returned as an associative array * where the key is the entity id. An empty array may be returned if no matching entities were found. @@ -261,7 +248,7 @@ abstract class MetaDataStorageSource * @param string $set The set we want to get metadata from. * @return array An associative array with the metadata for the requested entities, if found. */ - public function getMetaDataForEntities(array $entityIds, $set) + public function getMetaDataForEntities(array $entityIds, string $set): array { if (count($entityIds) === 1) { return $this->getMetaDataForEntitiesIndividually($entityIds, $set); @@ -270,6 +257,7 @@ abstract class MetaDataStorageSource return array_intersect_key($entities, array_flip($entityIds)); } + /** * Loads metadata entities one at a time, rather than the default implementation of loading all entities * and filtering. @@ -278,7 +266,7 @@ abstract class MetaDataStorageSource * @param string $set The set we want to get metadata from. * @return array An associative array with the metadata for the requested entities, if found. */ - protected function getMetaDataForEntitiesIndividually(array $entityIds, $set) + protected function getMetaDataForEntitiesIndividually(array $entityIds, string $set): array { $entities = []; foreach ($entityIds as $entityId) { @@ -290,6 +278,7 @@ abstract class MetaDataStorageSource return $entities; } + /** * This method returns the full metadata set for a given entity id or null if the entity id cannot be found * in the given metadata set. @@ -298,10 +287,8 @@ abstract class MetaDataStorageSource * @param array $metadataSet the already loaded metadata set * @return mixed|null */ - protected function lookupIndexFromEntityId($entityId, array $metadataSet) + protected function lookupIndexFromEntityId(string $entityId, array $metadataSet) { - Assert::string($entityId); - // check for hostname $currentHost = Utils\HTTP::getSelfHost(); // sp.example.org @@ -321,6 +308,7 @@ abstract class MetaDataStorageSource return null; } + /** * @param string $set * @throws \Exception @@ -355,11 +343,8 @@ abstract class MetaDataStorageSource * * @throws \Exception */ - protected function updateEntityID($metadataSet, $entityId, array $metadataEntry) + protected function updateEntityID(string $metadataSet, string $entityId, array $metadataEntry): array { - Assert::string($metadataSet); - Assert::string($entityId); - $modifiedMetadataEntry = $metadataEntry; // generate a dynamic hosted url diff --git a/lib/SimpleSAML/Metadata/SAMLBuilder.php b/lib/SimpleSAML/Metadata/SAMLBuilder.php index 392024ebf..988371bb1 100644 --- a/lib/SimpleSAML/Metadata/SAMLBuilder.php +++ b/lib/SimpleSAML/Metadata/SAMLBuilder.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace SimpleSAML\Metadata; +use DOMElement; use SAML2\Constants; use SAML2\XML\md\AttributeAuthorityDescriptor; use SAML2\XML\md\AttributeConsumingService; @@ -73,7 +74,7 @@ class SAMLBuilder * to null. * @return void */ - public function __construct($entityId, $maxCache = null, $maxDuration = null) + public function __construct(string $entityId, int $maxCache = null, int $maxDuration = null) { Assert::string($entityId); @@ -89,7 +90,7 @@ class SAMLBuilder * @param array $metadata * @return void */ - private function setExpiration(array $metadata) + private function setExpiration(array $metadata): void { if (array_key_exists('expire', $metadata)) { if ($metadata['expire'] - time() < $this->maxDuration) { @@ -111,7 +112,7 @@ class SAMLBuilder * * @return \DOMElement The EntityDescriptor element of this entity. */ - public function getEntityDescriptor() + public function getEntityDescriptor(): DOMElement { $xml = $this->entityDescriptor->toXML(); $xml->ownerDocument->appendChild($xml); @@ -129,7 +130,7 @@ class SAMLBuilder * * @return string The serialized EntityDescriptor. */ - public function getEntityDescriptorText($formatted = true) + public function getEntityDescriptorText(bool $formatted = true): string { Assert::boolean($formatted); @@ -148,9 +149,8 @@ class SAMLBuilder * @param array $metadata The metadata with the information about the SecurityTokenServiceType. * @return void */ - public function addSecurityTokenServiceType($metadata) + public function addSecurityTokenServiceType(array $metadata): void { - Assert::isArray($metadata); Assert::notNull($metadata['entityid']); Assert::notNull($metadata['metadata-set']); @@ -172,7 +172,7 @@ class SAMLBuilder * @param \SAML2\XML\md\RoleDescriptor $e Reference to the element where the Extensions element should be included. * @return void */ - private function addExtensions(Configuration $metadata, RoleDescriptor $e) + private function addExtensions(Configuration $metadata, RoleDescriptor $e): void { if ($metadata->hasValue('tags')) { $a = new Attribute(); @@ -320,7 +320,7 @@ class SAMLBuilder * @param array $orgURL An array with the localized OrganizationURL. * @return void */ - public function addOrganization(array $orgName, array $orgDisplayName, array $orgURL) + public function addOrganization(array $orgName, array $orgDisplayName, array $orgURL): void { $org = new Organization(); @@ -338,7 +338,7 @@ class SAMLBuilder * @param array $metadata The metadata we should extract the organization information from. * @return void */ - public function addOrganizationInfo(array $metadata) + public function addOrganizationInfo(array $metadata): void { if ( empty($metadata['OrganizationName']) || @@ -424,7 +424,7 @@ class SAMLBuilder private function addAttributeConsumingService( SPSSODescriptor $spDesc, Configuration $metadata - ) { + ): void { $attributes = $metadata->getArray('attributes', []); $name = $metadata->getLocalizedString('name', null); @@ -477,11 +477,8 @@ class SAMLBuilder * @param array $metadata The metadata. * @return void */ - public function addMetadata($set, $metadata) + public function addMetadata(string $set, array $metadata): void { - Assert::string($set); - Assert::isArray($metadata); - $this->setExpiration($metadata); switch ($set) { @@ -507,10 +504,8 @@ class SAMLBuilder * @param array $protocols The protocols supported. Defaults to \SAML2\Constants::NS_SAMLP. * @return void */ - public function addMetadataSP20($metadata, $protocols = [Constants::NS_SAMLP]) + public function addMetadataSP20(array $metadata, array $protocols = [Constants::NS_SAMLP]): void { - Assert::isArray($metadata); - Assert::isArray($protocols); Assert::notNull($metadata['entityid']); Assert::notNull($metadata['metadata-set']); @@ -564,9 +559,8 @@ class SAMLBuilder * @param array $metadata The metadata. * @return void */ - public function addMetadataIdP20($metadata) + public function addMetadataIdP20(array $metadata): void { - Assert::isArray($metadata); Assert::notNull($metadata['entityid']); Assert::notNull($metadata['metadata-set']); @@ -614,9 +608,8 @@ class SAMLBuilder * @param array $metadata The metadata. * @return void */ - public function addMetadataSP11($metadata) + public function addMetadataSP11(array $metadata): void { - Assert::isArray($metadata); Assert::notNull($metadata['entityid']); Assert::notNull($metadata['metadata-set']); @@ -655,9 +648,8 @@ class SAMLBuilder * @param array $metadata The metadata. * @return void */ - public function addMetadataIdP11($metadata) + public function addMetadataIdP11(array $metadata): void { - Assert::isArray($metadata); Assert::notNull($metadata['entityid']); Assert::notNull($metadata['metadata-set']); @@ -688,7 +680,7 @@ class SAMLBuilder * \SimpleSAML\Metadata\SAMLParser. * @return void */ - public function addAttributeAuthority(array $metadata) + public function addAttributeAuthority(array $metadata): void { Assert::notNull($metadata['entityid']); Assert::notNull($metadata['metadata-set']); @@ -727,10 +719,8 @@ class SAMLBuilder * @todo Change the signature to remove $type. * @todo Remove the capability to pass a name and parse it inside the method. */ - public function addContact($type, $details) + public function addContact(string $type, array $details): void { - Assert::string($type); - Assert::isArray($details); Assert::oneOf($type, ['technical', 'support', 'administrative', 'billing', 'other']); // TODO: remove this check as soon as getContact() is called always before calling this function @@ -785,7 +775,7 @@ class SAMLBuilder * @param string $x509data The certificate data. * @return void */ - private function addX509KeyDescriptor(RoleDescriptor $rd, string $use, string $x509data) + private function addX509KeyDescriptor(RoleDescriptor $rd, string $use, string $x509data): void { Assert::oneOf($use, ['encryption', 'signing']); @@ -804,7 +794,7 @@ class SAMLBuilder * @param \SimpleSAML\Configuration $metadata The metadata of the entity. * @return void */ - private function addCertificate(RoleDescriptor $rd, Configuration $metadata) + private function addCertificate(RoleDescriptor $rd, Configuration $metadata): void { $keys = $metadata->getPublicKeys(); foreach ($keys as $key) { diff --git a/lib/SimpleSAML/Metadata/SAMLParser.php b/lib/SimpleSAML/Metadata/SAMLParser.php index 7cb5b20d8..71ff4dc5e 100644 --- a/lib/SimpleSAML/Metadata/SAMLParser.php +++ b/lib/SimpleSAML/Metadata/SAMLParser.php @@ -178,7 +178,7 @@ class SAMLParser */ private function __construct( EntityDescriptor $entityElement, - int $maxExpireTime = null, + ?int $maxExpireTime, array $validators = [], array $parentExtensions = [] ) { @@ -234,7 +234,7 @@ class SAMLParser * @return SAMLParser An instance of this class with the metadata loaded. * @throws \Exception If the file does not parse as XML. */ - public static function parseFile($file) + public static function parseFile(string $file): SAMLParser { /** @var string $data */ $data = Utils\HTTP::fetch($file); @@ -257,7 +257,7 @@ class SAMLParser * @return SAMLParser An instance of this class with the metadata loaded. * @throws \Exception If the string does not parse as XML. */ - public static function parseString($metadata) + public static function parseString(string $metadata): SAMLParser { try { $doc = DOMDocumentFactory::fromString($metadata); @@ -276,7 +276,7 @@ class SAMLParser * * @return SAMLParser An instance of this class with the metadata loaded. */ - public static function parseDocument($document) + public static function parseDocument(DOMDocument $document): SAMLParser { Assert::isInstanceOf($document, DOMDocument::class); @@ -294,9 +294,10 @@ class SAMLParser * * @return SAMLParser An instance of this class with the metadata loaded. */ - public static function parseElement($entityElement) + public static function parseElement(\SAML2\XML\md\EntityDescriptor $entityElement): SAMLParser { Assert::isInstanceOf($entityElement, EntityDescriptor::class); + return new SAMLParser($entityElement, null, []); } @@ -307,15 +308,15 @@ class SAMLParser * the file contains a single EntityDescriptorElement, then the array will contain a single SAMLParser * instance. * - * @param string|null $file The path to the file which contains the EntityDescriptor or EntitiesDescriptor element. + * @param string $file The path to the file which contains the EntityDescriptor or EntitiesDescriptor element. * * @return SAMLParser[] An array of SAMLParser instances. * @throws \Exception If the file does not parse as XML. */ - public static function parseDescriptorsFile($file) + public static function parseDescriptorsFile(string $file): array { - if ($file === null) { - throw new \Exception('Cannot open file NULL. File name not specified.'); + if (empty($file)) { + throw new \Exception('Cannot open file; file name not specified.'); } /** @var string $data */ @@ -342,7 +343,7 @@ class SAMLParser * be the entity id. * @throws \Exception If the string does not parse as XML. */ - public static function parseDescriptorsString($string) + public static function parseDescriptorsString(string $string): array { try { $doc = DOMDocumentFactory::fromString($string); @@ -365,7 +366,7 @@ class SAMLParser * be the entity id. * @throws \Exception if the document is empty or the root is an unexpected node. */ - public static function parseDescriptorsElement(DOMElement $element = null) + public static function parseDescriptorsElement(DOMElement $element = null): array { if ($element === null) { throw new \Exception('Document was empty.'); @@ -392,10 +393,10 @@ class SAMLParser */ private static function processDescriptorsElement( SignedElementHelper $element, - int $maxExpireTime = null, + ?int $maxExpireTime, array $validators = [], array $parentExtensions = [] - ) { + ): array { if ($element instanceof EntityDescriptor) { $ret = new SAMLParser($element, $maxExpireTime, $validators, $parentExtensions); $ret = [$ret->getEntityId() => $ret]; @@ -431,7 +432,7 @@ class SAMLParser * @return int|null The unix timestamp for when the element should expire. Will be NULL if no * limit is set for the element. */ - private static function getExpireTime($element, int $maxExpireTime = null) + private static function getExpireTime($element, ?int $maxExpireTime): ?int { // validUntil may be null $expire = $element->getValidUntil(); @@ -449,7 +450,7 @@ class SAMLParser * * @return string The entity id of this parsed entity. */ - public function getEntityId() + public function getEntityId(): string { return $this->entityId; } @@ -492,7 +493,7 @@ class SAMLParser * @param array $roleDescriptor The parsed role descriptor. * @return void */ - private function addExtensions(array &$metadata, array $roleDescriptor) + private function addExtensions(array &$metadata, array $roleDescriptor): void { Assert::keyExists($roleDescriptor, 'scope'); Assert::keyExists($roleDescriptor, 'tags'); @@ -546,7 +547,7 @@ class SAMLParser * @return array|null An associative array with metadata or NULL if we are unable to * generate metadata for a SAML 2.x SP. */ - public function getMetadata20SP() + public function getMetadata20SP(): ?array { $ret = $this->getMetadataCommon(); $ret['metadata-set'] = 'saml20-sp-remote'; @@ -648,7 +649,7 @@ class SAMLParser * @return array|null An associative array with metadata or NULL if we are unable to * generate metadata for a SAML 2.0 IdP. */ - public function getMetadata20IdP() + public function getMetadata20IdP(): ?array { $ret = $this->getMetadataCommon(); $ret['metadata-set'] = 'saml20-idp-remote'; @@ -706,7 +707,7 @@ class SAMLParser * * @return array Array of AttributeAuthorityDescriptor entries. */ - public function getAttributeAuthorities() + public function getAttributeAuthorities(): array { return $this->attributeAuthorityDescriptors; } @@ -726,7 +727,7 @@ class SAMLParser * * @return array An associative array with metadata we have extracted from this element. */ - private static function parseRoleDescriptorType(RoleDescriptor $element, int $expireTime = null) + private static function parseRoleDescriptorType(RoleDescriptor $element, ?int $expireTime): array { $ret = []; @@ -775,7 +776,7 @@ class SAMLParser * * @return array An associative array with metadata we have extracted from this element. */ - private static function parseSSODescriptor(SSODescriptorType $element, int $expireTime = null): array + private static function parseSSODescriptor(SSODescriptorType $element, ?int $expireTime): array { $sd = self::parseRoleDescriptorType($element, $expireTime); @@ -801,7 +802,7 @@ class SAMLParser * NULL if unknown. * @return void */ - private function processSPSSODescriptor(SPSSODescriptor $element, int $expireTime = null) + private function processSPSSODescriptor(SPSSODescriptor $element, ?int $expireTime): void { $sp = self::parseSSODescriptor($element, $expireTime); @@ -836,7 +837,7 @@ class SAMLParser * NULL if unknown. * @return void */ - private function processIDPSSODescriptor(IDPSSODescriptor $element, int $expireTime = null) + private function processIDPSSODescriptor(IDPSSODescriptor $element, ?int $expireTime): void { $idp = self::parseSSODescriptor($element, $expireTime); @@ -863,8 +864,8 @@ class SAMLParser */ private function processAttributeAuthorityDescriptor( AttributeAuthorityDescriptor $element, - $expireTime - ) { + ?int $expireTime + ): void { Assert::nullOrInteger($expireTime); $aad = self::parseRoleDescriptorType($element, $expireTime); @@ -1041,7 +1042,7 @@ class SAMLParser * @param \SAML2\XML\md\Organization $element The Organization element. * @return void */ - private function processOrganization(Organization $element) + private function processOrganization(Organization $element): void { $this->organizationName = $element->getOrganizationName(); $this->organizationDisplayName = $element->getOrganizationDisplayName(); @@ -1055,7 +1056,7 @@ class SAMLParser * @param \SAML2\XML\md\ContactPerson $element The ContactPerson element. * @return void */ - private function processContactPerson(ContactPerson $element) + private function processContactPerson(ContactPerson $element): void { $contactPerson = []; if ($element->getContactType() !== '') { @@ -1089,7 +1090,7 @@ class SAMLParser * @param array $sp The array with the SP's metadata. * @return void */ - private static function parseAttributeConsumerService(AttributeConsumingService $element, array &$sp) + private static function parseAttributeConsumerService(AttributeConsumingService $element, array &$sp): void { $sp['name'] = $element->getServiceName(); $sp['description'] = $element->getServiceDescription(); @@ -1196,7 +1197,7 @@ class SAMLParser * * @return array|null An associative array describing the key, or null if this is an unsupported key. */ - private static function parseKeyDescriptor(KeyDescriptor $kd) + private static function parseKeyDescriptor(KeyDescriptor $kd): ?array { $r = []; @@ -1307,7 +1308,7 @@ class SAMLParser * @return boolean True if it is possible to check the signature with the certificate, false otherwise. * @throws \Exception If the certificate file cannot be found. */ - public function validateSignature($certificates) + public function validateSignature(array $certificates): bool { foreach ($certificates as $cert) { Assert::string($cert); diff --git a/lib/SimpleSAML/Metadata/Signer.php b/lib/SimpleSAML/Metadata/Signer.php index b0441fbc4..f496f3cd8 100644 --- a/lib/SimpleSAML/Metadata/Signer.php +++ b/lib/SimpleSAML/Metadata/Signer.php @@ -228,7 +228,7 @@ class Signer * @return string The $metadataString with the signature embedded. * @throws \Exception If the certificate or private key cannot be loaded, or the metadata doesn't parse properly. */ - public static function sign($metadataString, $entityMetadata, $type) + public static function sign(string $metadataString, array $entityMetadata, string $type): string { $config = Configuration::getInstance(); diff --git a/lib/SimpleSAML/Metadata/Sources/MDQ.php b/lib/SimpleSAML/Metadata/Sources/MDQ.php index dea938b49..513cbeb40 100644 --- a/lib/SimpleSAML/Metadata/Sources/MDQ.php +++ b/lib/SimpleSAML/Metadata/Sources/MDQ.php @@ -61,10 +61,13 @@ class MDQ extends \SimpleSAML\Metadata\MetaDataStorageSource * * @throws \Exception If no server option can be found in the configuration. */ - protected function __construct($config) + protected function __construct(array $config) { +<<<<<<< HEAD Assert::isArray($config); +======= +>>>>>>> Fully typehint lib/Metadata/*.php if (!array_key_exists('server', $config)) { throw new \Exception(__CLASS__ . ": the 'server' configuration option is not set."); } else { @@ -93,7 +96,7 @@ class MDQ extends \SimpleSAML\Metadata\MetaDataStorageSource * * @return array An empty array. */ - public function getMetadataSet($set) + public function getMetadataSet(string $set) : array { // we don't have this metadata set return []; @@ -108,11 +111,14 @@ class MDQ extends \SimpleSAML\Metadata\MetaDataStorageSource * * @return string The full path to the cache file. */ - private function getCacheFilename($set, $entityId) + private function getCacheFilename(string $set, string $entityId) : string { +<<<<<<< HEAD Assert::string($set); Assert::string($entityId); +======= +>>>>>>> Fully typehint lib/Metadata/*.php if ($this->cacheDir === null) { throw new Error\ConfigurationError("Missing cache directory configuration."); } @@ -132,7 +138,11 @@ class MDQ extends \SimpleSAML\Metadata\MetaDataStorageSource * if the entity could not be found. * @throws \Exception If an error occurs while loading metadata from cache. */ +<<<<<<< HEAD private function getFromCache(string $set, string $entityId) +======= + private function getFromCache(string $set, string $entityId) : ?array +>>>>>>> Fully typehint lib/Metadata/*.php { if (empty($this->cacheDir)) { return null; @@ -189,7 +199,11 @@ class MDQ extends \SimpleSAML\Metadata\MetaDataStorageSource * @throws \Exception If metadata cannot be written to cache. * @return void */ +<<<<<<< HEAD private function writeToCache(string $set, string $entityId, array $data) +======= + private function writeToCache(string $set, string $entityId, array $data) : void +>>>>>>> Fully typehint lib/Metadata/*.php { if (empty($this->cacheDir)) { return; @@ -213,7 +227,11 @@ class MDQ extends \SimpleSAML\Metadata\MetaDataStorageSource * @return array|NULL The associative array with the metadata, or NULL if no metadata for * the given set was found. */ +<<<<<<< HEAD private static function getParsedSet(SAMLParser $entity, string $set) +======= + private static function getParsedSet(SAMLParser $entity, string $set) : ?array +>>>>>>> Fully typehint lib/Metadata/*.php { switch ($set) { case 'saml20-idp-remote': @@ -250,11 +268,14 @@ class MDQ extends \SimpleSAML\Metadata\MetaDataStorageSource * @throws \Exception If an error occurs while validating the signature or the metadata is in an * incorrect set. */ - public function getMetaData($index, $set) + public function getMetaData(string $index, string $set) : ?array { +<<<<<<< HEAD Assert::string($index); Assert::string($set); +======= +>>>>>>> Fully typehint lib/Metadata/*.php Logger::info(__CLASS__ . ': loading metadata entity [' . $index . '] from [' . $set . ']'); // read from cache if possible @@ -321,7 +342,7 @@ class MDQ extends \SimpleSAML\Metadata\MetaDataStorageSource * @param string $set The set we want to get metadata from. * @return array An associative array with the metadata for the requested entities, if found. */ - public function getMetaDataForEntities(array $entityIds, $set) + public function getMetaDataForEntities(array $entityIds, string $set) : array { return $this->getMetaDataForEntitiesIndividually($entityIds, $set); } -- GitLab