diff --git a/lib/SimpleSAML/Compat/Logger.php b/lib/SimpleSAML/Compat/Logger.php new file mode 100644 index 0000000000000000000000000000000000000000..0b0bede4ecd36ae2ef25091fbf3da2bb45703c62 --- /dev/null +++ b/lib/SimpleSAML/Compat/Logger.php @@ -0,0 +1,186 @@ +<?php + +declare(strict_types=1); + +namespace SimpleSAML\Compat; + +use Psr\Log\InvalidArgumentException; +use Psr\Log\LoggerInterface; +use Psr\Log\LogLevel; +use SimpleSAML\Assert\Assert; +use SimpleSAML\Logger as SspLogger; + +class Logger implements LoggerInterface +{ + /** + * System is unusable. + * + * @param string $message + * @param array $context + * + * Type hint not possible due to upstream method signature + */ + public function emergency($message, array $context = []): void + { + SspLogger::emergency($message . ($context ? " " . var_export($context, true) : "")); + } + + + /** + * Action must be taken immediately. + * + * Example: Entire website down, database unavailable, etc. This should + * trigger the SMS alerts and wake you up. + * + * @param string $message + * @param array $context + * + * Type hint not possible due to upstream method signature + */ + public function alert($message, array $context = []): void + { + SspLogger::alert($message . ($context ? " " . var_export($context, true) : "")); + } + + + /** + * Critical conditions. + * + * Example: Application component unavailable, unexpected exception. + * + * @param string $message + * @param array $context + * + * Type hint not possible due to upstream method signature + */ + public function critical($message, array $context = []): void + { + SspLogger::critical($message . ($context ? " " . var_export($context, true) : "")); + } + + + /** + * Runtime errors that do not require immediate action but should typically + * be logged and monitored. + * + * @param string $message + * @param array $context + * + * Type hint not possible due to upstream method signature + */ + public function error($message, array $context = []): void + { + SspLogger::error($message . ($context ? " " . var_export($context, true) : "")); + } + + + /** + * Exceptional occurrences that are not errors. + * + * Example: Use of deprecated APIs, poor use of an API, undesirable things + * that are not necessarily wrong. + * + * @param string $message + * @param array $context + * + * Type hint not possible due to upstream method signature + */ + public function warning($message, array $context = []): void + { + SspLogger::warning($message . ($context ? " " . var_export($context, true) : "")); + } + + + /** + * Normal but significant events. + * + * @param string $message + * @param array $context + * + * Type hint not possible due to upstream method signature + */ + public function notice($message, array $context = []): void + { + SspLogger::notice($message . ($context ? " " . var_export($context, true) : "")); + } + + + /** + * Interesting events. + * + * Example: User logs in, SQL logs. + * + * @param string $message + * @param array $context + * + * Type hint not possible due to upstream method signature + */ + public function info($message, array $context = []): void + { + SspLogger::info($message . ($context ? " " . var_export($context, true) : "")); + } + + + /** + * Detailed debug information. + * + * @param string $message + * @param array $context + * + * Type hint not possible due to upstream method signature + */ + public function debug($message, array $context = []): void + { + SspLogger::debug($message . ($context ? " " . var_export($context, true) : "")); + } + + + /** + * Logs with an arbitrary level. + * + * @param mixed $level + * @param string $message + * @param array $context + * + * @throws \SimpleSAML\Assert\AssertionFailedException if assertions are false + * + * Type hint not possible due to upstream method signature + */ + public function log($level, $message, array $context = []): void + { + Assert::string($message); + + switch ($level) { + /* From PSR: Calling this method with one of the log level constants + * MUST have the same result as calling the level-specific method + */ + case LogLevel::ALERT: + $this->alert($message, $context); + break; + case LogLevel::CRITICAL: + $this->critical($message, $context); + break; + case LogLevel::DEBUG: + $this->debug($message, $context); + break; + case LogLevel::EMERGENCY: + $this->emergency($message, $context); + break; + case LogLevel::ERROR: + $this->error($message, $context); + break; + case LogLevel::INFO: + $this->info($message, $context); + break; + case LogLevel::NOTICE: + $this->notice($message, $context); + break; + case LogLevel::WARNING: + $this->warning($message, $context); + break; + default: + throw new InvalidArgumentException("Unrecognized log level '$level''"); + } + } +} + diff --git a/lib/SimpleSAML/Compat/SspContainer.php b/lib/SimpleSAML/Compat/SspContainer.php new file mode 100644 index 0000000000000000000000000000000000000000..17e53fb876e9a3e435801530f233913001c0473d --- /dev/null +++ b/lib/SimpleSAML/Compat/SspContainer.php @@ -0,0 +1,152 @@ +<?php + +declare(strict_types=1); + +namespace SimpleSAML\Compat; + +use Psr\Log\LoggerInterface; +use SAML2\Compat\AbstractContainer; +use SAML2\XML\saml\CustomIdentifierInterface; +use SimpleSAML\Assert\Assert; +use SimpleSAML\Utils\HTTP; +use SimpleSAML\Utils\Random; +use SimpleSAML\Utils\System; +use SimpleSAML\Utils\XML; +use SimpleSAML\XML\AbstractXMLElement; + +class SspContainer extends AbstractContainer +{ + /** @var \Psr\Log\LoggerInterface */ + protected LoggerInterface $logger; + + /** @var array */ + protected array $registry = []; + + + /** + * Create a new SimpleSAMLphp compatible container. + */ + public function __construct() + { + $this->logger = new Logger(); + } + + + /** + * {@inheritdoc} + * @return \Psr\Log\LoggerInterface + */ + public function getLogger(): LoggerInterface + { + return $this->logger; + } + + + /** + * {@inheritdoc} + * @return string + */ + public function generateId(): string + { + return Random::generateID(); + } + + + /** + * {@inheritdoc} + * @param mixed $message + * @param string $type + */ + public function debugMessage($message, string $type): void + { + XML::debugSAMLMessage($message, $type); + } + + + /** + * {@inheritdoc} + * @param string $url + * @param array $data + */ + public function redirect(string $url, array $data = []): void + { + HTTP::redirectTrustedURL($url, $data); + } + + + /** + * {@inheritdoc} + * @param string $url + * @param array $data + */ + public function postRedirect(string $url, array $data = []): void + { + HTTP::submitPOSTData($url, $data); + } + + + /** + * {@inheritdoc} + * @return string + */ + public function getTempDir(): string + { + /** @psalm-suppress UndefinedClass */ + return System::getTempDir(); + } + + + /** + * {@inheritdoc} + * @param string $filename + * @param string $date + * @param int|null $mode + */ + public function writeFile(string $filename, string $data, int $mode = null): void + { + if ($mode === null) { + $mode = 0600; + } + System::writeFile($filename, $data, $mode); + } + + + /** + * @inheritDoc + public function registerExtensionHandler(string $class): void + { + Assert::subclassOf($class, AbstractXMLElement::class); + + if (is_subclass_of($class, CustomIdentifierInterface::class, true)) { + $key = $class::getXsiType() . ':BaseID'; + } else { + $key = join(':', [urlencode($class::NS), AbstractXMLElement::getClassName($class)]); + } + $this->registry[$key] = $class; + } + */ + + + /** + * @inheritDoc + public function getElementHandler(string $namespace, string $element): ?string + { + Assert::notEmpty($namespace, 'Cannot search for handlers without an associated namespace URI.'); + Assert::notEmpty($element, 'Cannot search for handlers without an associated element name.'); + + return $this->registry[join(':', [urlencode($namespace), $element])]; + } + */ + + + /** + * @inheritDoc + public function getIdentifierHandler(string $type): ?string + { + Assert::notEmpty($type, 'Cannot search for identifier handlers with an empty type.'); + + $handler = $type . ':BaseID'; + return array_key_exists($handler, $this->registry) ? $this->registry[$handler] : null; + } + */ +} diff --git a/www/_include.php b/www/_include.php index 1403bfd429b3af68be0fac6c9dfde286cd3de977..1508069ca0e4e3801c8e0c63a88b162fb26b8be9 100644 --- a/www/_include.php +++ b/www/_include.php @@ -65,3 +65,7 @@ try { // set the timezone \SimpleSAML\Utils\Time::initTimezone(); + +// set the SAML2 container +$container = new \SimpleSAML\Compat\SspContainer(); +\SAML2\Compat\ContainerSingleton::setContainer($container);