diff --git a/lib/SimpleSAML/Utilities.php b/lib/SimpleSAML/Utilities.php index f3d8822abecdcd572ad7b7c5aefdc3ecf718c8b0..4de7246eea6cc594815d1c6f58e91a076158017a 100644 --- a/lib/SimpleSAML/Utilities.php +++ b/lib/SimpleSAML/Utilities.php @@ -2113,6 +2113,57 @@ class SimpleSAML_Utilities { self::$logMask = $lastMask[1]; } + + /** + * Find the default endpoint in an endpoint array. + * + * @param array $endpoints Array with endpoints. + * @param array $bindings Array with acceptable bindings. Can be NULL if any binding is allowed. + * @return array|NULL The default endpoint, or NULL if no acceptable endpoints are used. + */ + public static function getDefaultEndpoint(array $endpoints, array $bindings = NULL) { + + $firstNotFalse = NULL; + $firstAllowed = NULL; + + /* Look through the endpoint list for acceptable endpoints. */ + foreach ($endpoints as $i => $ep) { + if ($bindings !== NULL && !in_array($ep['Binding'], $ep, TRUE)) { + /* Unsupported binding. Skip it. */ + continue; + } + + if (array_key_exists('isDefault', $ep)) { + if ($ep['isDefault'] === TRUE) { + /* This is the first endpoitn with isDefault set to TRUE. */ + return $ep; + } + /* isDefault is set to FALSE, but the endpoint is still useable as a last resort. */ + if ($firstAllowed === NULL) { + /* This is the first endpoint that we can use. */ + $firstAllowed = $ep; + } + } else { + if ($firstNotFalse === NULL) { + /* This is the first endpoint without isDefault set. */ + $firstNotFalse = $ep; + } + } + } + + if ($firstNotFalse !== NULL) { + /* We have an endpoint without isDefault set to FALSE. */ + return $firstNotFalse; + } + + /* + * $firstAllowed either contains the first endpoint we can use, or it + * contains NULL if we cannot use any of the endpoints. Either way we + * return the value of it. + */ + return $firstAllowed; + } + } ?> \ No newline at end of file