1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
<?php
declare(strict_types=1);
namespace SimpleSAML\Metadata;
use SimpleSAML\Error;
use SimpleSAML\Module;
use SimpleSAML\Utils;
/**
* This abstract class defines an interface for metadata storage sources.
*
* It also contains the overview of the different metadata storage sources.
* A metadata storage source can be loaded by passing the configuration of it
* to the getSource static function.
*
* @author Olav Morken, UNINETT AS.
* @author Andreas Aakre Solberg, UNINETT AS.
* @package SimpleSAMLphp
*/
abstract class MetaDataStorageSource
{
/**
* Parse array with metadata sources.
*
* This function accepts an array with metadata sources, and returns an array with
* each metadata source as an object.
*
* @param array $sourcesConfig Array with metadata source configuration.
*
* @return array Parsed metadata configuration.
*
* @throws \Exception If something is wrong in the configuration.
*/
public static function parseSources($sourcesConfig)
{
assert(is_array($sourcesConfig));
$sources = [];
foreach ($sourcesConfig as $sourceConfig) {
if (!is_array($sourceConfig)) {
throw new \Exception("Found an element in metadata source configuration which wasn't an array.");
}
$sources[] = self::getSource($sourceConfig);
}
return $sources;
}
/**
* This function creates a metadata source based on the given configuration.
* The type of source is based on the 'type' parameter in the configuration.
* The default type is 'flatfile'.
*
* @param array $sourceConfig Associative array with the configuration for this metadata source.
*
* @return \SimpleSAML\Metadata\MetaDataStorageSource An instance of a metadata source with the given configuration.
*
* @throws \Exception If the metadata source type is invalid.
*/
public static function getSource($sourceConfig)
{
assert(is_array($sourceConfig));
if (array_key_exists('type', $sourceConfig)) {
$type = $sourceConfig['type'];
} else {
$type = 'flatfile';
}
switch ($type) {
case 'flatfile':
return new MetaDataStorageHandlerFlatFile($sourceConfig);
case 'xml':
return new MetaDataStorageHandlerXML($sourceConfig);
case 'serialize':
return new MetaDataStorageHandlerSerialize($sourceConfig);
case 'mdx':
case 'mdq':
return new Sources\MDQ($sourceConfig);
case 'pdo':
return new MetaDataStorageHandlerPdo($sourceConfig);
default:
// metadata store from module
try {
$className = Module::resolveClass(
$type,
'MetadataStore',
'\SimpleSAML\Metadata\MetaDataStorageSource'
);
} catch (\Exception $e) {
throw new Error\CriticalConfigurationError(
"Invalid 'type' for metadata source. Cannot find store '$type'.",
null
);
}
/** @var \SimpleSAML\Metadata\MetaDataStorageSource */
return new $className($sourceConfig);
}
}
/**
* This function attempts to generate an associative array with metadata for all entities in the
* given set. The key of the array is the entity id.
*
* A subclass should override this function if it is able to easily generate this list.
*
* @param string $set The set we want to list metadata for.
*
* @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)
{
return [];
}
/**
* This function resolves an host/path combination to an entity id.
*
* This class implements this function using the getMetadataSet-function. A subclass should
* override this function if it doesn't implement the getMetadataSet function, or if the
* implementation of getMetadataSet is slow.
*
* @param string $hostPath The host/path combination we are looking up.
* @param string $set Which set of metadata we are looking it up in.
* @param string $type Do you want to return the metaindex or the entityID. [entityid|metaindex]
*
* @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')
{
$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)) {
continue;
}
if ($hostPath === $entry['host']) {
if ($type === 'entityid') {
return $entry['entityid'];
} else {
return $index;
}
}
}
// no entries matched, we should return null
return null;
}
/**
* This function will go through all the metadata, and check the DiscoHints->IPHint
* parameter, which defines a network space (ip range) for each remote entry.
* This function returns the entityID for any of the entities that have an
* IP range which the IP falls within.
*
* @param string $set Which set of metadata we are looking it up in.
* @param string $ip IP address
* @param string $type Do you want to return the metaindex or the entityID. [entityid|metaindex]
*
* @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')
{
$metadataSet = $this->getMetadataSet($set);
foreach ($metadataSet as $index => $entry) {
$cidrHints = [];
// support hint.cidr for idp discovery
if (array_key_exists('hint.cidr', $entry) && is_array($entry['hint.cidr'])) {
$cidrHints = $entry['hint.cidr'];
}
// support discohints in idp metadata for idp discovery
if (
array_key_exists('DiscoHints', $entry)
&& array_key_exists('IPHint', $entry['DiscoHints'])
&& is_array($entry['DiscoHints']['IPHint'])
) {
// merge with hints derived from discohints, but prioritize hint.cidr in case it is used
$cidrHints = array_merge($entry['DiscoHints']['IPHint'], $cidrHints);
}
if (empty($cidrHints)) {
continue;
}
foreach ($cidrHints as $hint_entry) {
if (Utils\Net::ipCIDRcheck($hint_entry, $ip)) {
if ($type === 'entityid') {
return $entry['entityid'];
} else {
return $index;
}
}
}
}
// no entries matched, we should return null
return null;
}
/**
* This function retrieves metadata for the given entity id in the given set of metadata.
* It will return NULL if it is unable to locate the metadata.
*
* This class implements this function using the getMetadataSet-function. A subclass should
* override this function if it doesn't implement the getMetadataSet function, or if the
* implementation of getMetadataSet is slow.
*
* @param string $index The entityId or metaindex we are looking up.
* @param string $set The set we are looking for metadata in.
*
* @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)
{
assert(is_string($index));
assert(isset($set));
$metadataSet = $this->getMetadataSet($set);
$indexLookup = $this->lookupIndexFromEntityId($index, $metadataSet);
if (isset($indexLookup) && array_key_exists($indexLookup, $metadataSet)) {
return $metadataSet[$indexLookup];
}
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.
* Subclasses should override if their getMetadataSet returns nothing or is slow. Subclasses may want to
* delegate to getMetaDataForEntitiesIndividually if loading entities one at a time is faster.
* @param array $entityIds The entity ids to load
* @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)
{
if (count($entityIds) === 1) {
return $this->getMetaDataForEntitiesIndividually($entityIds, $set);
}
$entities = $this->getMetadataSet($set);
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.
* @see MetaDataStorageSource::getMetaDataForEntities()
* @param array $entityIds The entity ids to load
* @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)
{
$entities = [];
foreach ($entityIds as $entityId) {
$metadata = $this->getMetaData($entityId, $set);
if ($metadata !== null) {
$entities[$entityId] = $metadata;
}
}
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.
*
* @param string $entityId
* @param array $metadataSet the already loaded metadata set
* @return mixed|null
*/
protected function lookupIndexFromEntityId($entityId, array $metadataSet)
{
assert(is_string($entityId));
// check for hostname
$currentHost = Utils\HTTP::getSelfHost(); // sp.example.org
foreach ($metadataSet as $index => $entry) {
// explicit index match
if ($index === $entityId) {
return $index;
}
if ($entry['entityid'] === $entityId) {
if ($entry['host'] === '__DEFAULT__' || $entry['host'] === $currentHost) {
return $index;
}
}
}
return null;
}
/**
* @param string $set
* @throws \Exception
* @return string
*/
private function getDynamicHostedUrl(string $set): string
{
// get the configuration
$baseUrl = Utils\HTTP::getBaseURL();
if ($set === 'saml20-idp-hosted') {
return $baseUrl . 'saml2/idp/metadata.php';
} elseif ($set === 'saml20-sp-hosted') {
return $baseUrl . 'saml2/sp/metadata.php';
} elseif ($set === 'shib13-idp-hosted') {
return $baseUrl . 'shib13/idp/metadata.php';
} elseif ($set === 'shib13-sp-hosted') {
return $baseUrl . 'shib13/sp/metadata.php';
} elseif ($set === 'adfs-idp-hosted') {
return 'urn:federation:' . Utils\HTTP::getSelfHost() . ':idp';
} else {
throw new \Exception('Can not generate dynamic EntityID for metadata of this type: [' . $set . ']');
}
}
/**
* Updates the metadata entry's entity id and returns the modified array. If the entity id is __DYNAMIC:*__ a
* the current url is assigned. If it is explicit the entityid array key is updated to the entityId that was
* provided.
*
* @param string $metadataSet a metadata set (saml20-idp-hosted, saml20-sp-remote, etc)
* @param string $entityId the entity id we are modifying
* @param array $metadataEntry the fully populated metadata entry
* @return array modified metadata to include the valid entityid
*
* @throws \Exception
*/
protected function updateEntityID($metadataSet, $entityId, array $metadataEntry)
{
assert(is_string($metadataSet));
assert(is_string($entityId));
$modifiedMetadataEntry = $metadataEntry;
// generate a dynamic hosted url
if (preg_match('/__DYNAMIC(:[0-9]+)?__/', $entityId)) {
$modifiedMetadataEntry['entityid'] = $this->getDynamicHostedUrl($metadataSet);
} else {
// set the entityid metadata array key to the provided entity id
$modifiedMetadataEntry['entityid'] = $entityId;
}
return $modifiedMetadataEntry;
}
}
|