Skip to content

Commit

Permalink
Refactor to xsd types
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Jan 27, 2025
1 parent 5af7263 commit 1239661
Show file tree
Hide file tree
Showing 131 changed files with 2,117 additions and 1,949 deletions.
76 changes: 0 additions & 76 deletions phpstan-baseline.neon

This file was deleted.

2 changes: 0 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@ parameters:
level: 1
paths:
- src
includes:
- phpstan-baseline.neon
10 changes: 6 additions & 4 deletions src/SAML11/Assert/AnyURITrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

namespace SimpleSAML\SAML11\Assert;

use InvalidArgumentException;
use SimpleSAML\Assert\AssertionFailedException;
use SimpleSAML\SAML11\Exception\ProtocolViolationException;
use SimpleSAML\XML\Exception\SchemaViolationException;

/**
* @package simplesamlphp/saml11
Expand All @@ -29,8 +27,12 @@ protected static function validAnyURI(string $value, string $message = ''): void
* Unless otherwise indicated in this specification, all URI reference values MUST consist
* of at least one non-whitespace character
*/
static::notWhitespaceOnly($value, $message ?: '%s is not a SAML1.1-compliant URI', ProtocolViolationException::class);
} catch (InvalidArgumentException $e) {
static::notWhitespaceOnly(
$value,
$message ?: '%s is not a SAML1.1-compliant URI',
ProtocolViolationException::class,
);
} catch (AssertionFailedException $e) {
throw new ProtocolViolationException($e->getMessage());
}
}
Expand Down
1 change: 0 additions & 1 deletion src/SAML11/Assert/DateTimeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use SimpleSAML\Assert\AssertionFailedException;
use SimpleSAML\SAML11\Exception\ProtocolViolationException;
use SimpleSAML\XML\Exception\SchemaViolationException;

/**
* @package simplesamlphp/saml11
Expand Down
37 changes: 20 additions & 17 deletions src/SAML11/Compat/AbstractContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use Psr\Log\LoggerInterface;
use SimpleSAML\SAML11\Assert\Assert;
use SimpleSAML\SAML11\XML\ExtensionPointInterface;
use SimpleSAML\XML\AbstractElement;
use SimpleSAML\XML\Exception\SchemaViolationException;
use SimpleSAML\XML\{AbstractElement, ElementInterface};
use SimpleSAML\XML\Type\QNameValue;
use SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmFactory;
use SimpleSAML\XMLSecurity\Alg\KeyTransport\KeyTransportAlgorithmFactory;
use SimpleSAML\XMLSecurity\Alg\Signature\SignatureAlgorithmFactory;
Expand Down Expand Up @@ -71,20 +71,17 @@ public function registerExtensionHandler(string $class): void
* Such classes must have been registered previously by calling registerExtensionHandler(), and they must
* extend \SimpleSAML\XML\AbstractElement.
*
* @param string|null $namespace The namespace URI for the given element.
* @param string $element The local name of the element.
* @param \SimpleSAML\XML\Typr\QNameValue|null $qName The qualified name of the element.
*
* @return string|null The fully-qualified name of a class extending \SimpleSAML\XML\AbstractElement and
* implementing support for the given element, or null if no such class has been registered before.
* @psalm-return class-string|null
*/
public function getElementHandler(?string $namespace, string $element): ?string
public function getElementHandler(QNameValue $qName): ?string
{
Assert::nullOrValidURI($namespace, SchemaViolationException::class);
Assert::validNCName($element, SchemaViolationException::class);

$key = ($namespace === null) ? $element : implode(':', [$namespace, $element]);
$key = $qName->getRawValue();
if (array_key_exists($key, $this->registry) === true) {
Assert::implementsInterface($this->registry[$key], ElementInterface::class);
return $this->registry[$key];
}

Expand All @@ -98,21 +95,27 @@ public function getElementHandler(?string $namespace, string $element): ?string
* Such classes must have been registered previously by calling registerExtensionHandler(), and they must
* implement \SimpleSAML\SAML11\XML\saml\ExtensionPointInterface.
*
* @param string $type The type of the identifier (xsi:type of a BaseID element).
*
* @param \SimpleSAML\XML\Type\QNameValue $qName The qualified name of the extension.
* @return string|null The fully-qualified name of a class implementing
* \SimpleSAML\SAML11\XML\saml\ExtensionPointInterface or null if no such class has been registered before.
* @psalm-return class-string|null
*/
public function getExtensionHandler(string $type): ?string
public function getExtensionHandler(QNameValue $qName): ?string
{
Assert::notEmpty($type, 'Cannot search for identifier handlers with an empty type.');
$type = implode(':', [self::XSI_TYPE_PREFIX, $type]);
if (!array_key_exists($type, $this->registry)) {
$prefix = $qName->getNamespacePrefix()->getValue();
$namespaceURI = $qName->getNamespaceURI()->getValue();

if ($namespaceURI !== null) {
$localName = $qName->getLocalName()->getValue();
$key = $qName->getRawValue();
if (array_key_exists($key, $this->registry) === true) {
Assert::implementsInterface($this->registry[$key], ExtensionPointInterface::class);
return $this->registry[$key];
}
return null;
}
Assert::implementsInterface($this->registry[$type], ExtensionPointInterface::class);
return $this->registry[$type];

return null;
}


Expand Down
14 changes: 14 additions & 0 deletions src/SAML11/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,18 @@ class Constants extends \SimpleSAML\XMLSecurity\Constants
* Top-level status code.
*/
public const STATUS_VERSION_MISMATCH = 'samlp:VersionMismatch';

/** @var string[] */
public static array $STATUS_CODES = [
self::STATUS_REQUEST_DENIED,
self::STATUS_REQUEST_VERSION_DEPRECATED,
self::STATUS_REQUEST_VERSION_TOO_HIGH,
self::STATUS_REQUEST_VERSION_TOO_LOW,
self::STATUS_REQUESTER,
self::STATUS_RESOURCE_NOT_RECOGNIZED,
self::STATUS_RESPONDER,
self::STATUS_SUCCESS,
self::STATUS_TOO_MANY_RESPONSES,
self::STATUS_VERSION_MISMATCH,
];
}
4 changes: 4 additions & 0 deletions src/SAML11/Type/DateTimeValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
*/
class DateTimeValue extends BaseDateTimeValue
{
// Lowercase p as opposed to the base-class to covert the timestamp to UTC as demanded by the SAML specifications
public const string DATETIME_FORMAT = 'Y-m-d\\TH:i:sp';


/**
* Validate the value.
*
Expand Down
18 changes: 10 additions & 8 deletions src/SAML11/XML/ExtensionPointInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace SimpleSAML\SAML11\XML;

use SimpleSAML\XML\Type\{AnyURIValue, NCNameValue, QNameValue};

/**
* Interface for several extension points objects.
*
Expand All @@ -14,31 +16,31 @@ interface ExtensionPointInterface
/**
* Get the local name for the element's xsi:type.
*
* @return string
* @return \SimpleSAML\XML\Type\NCNameValue
*/
public static function getXsiTypeName(): string;
public static function getXsiTypeName(): NCNameValue;


/**
* Get the namespace for the element's xsi:type.
*
* @return string
* @return \SimpleSAML\XML\Type\AnyURIValue
*/
public static function getXsiTypeNamespaceURI(): string;
public static function getXsiTypeNamespaceURI(): AnyURIValue;


/**
* Get the namespace-prefix for the element's xsi:type.
*
* @return string
* @return \SimpleSAML\XML\Type\NCNameValue
*/
public static function getXsiTypePrefix(): string;
public static function getXsiTypePrefix(): NCNameValue;


/**
* Return the xsi:type value corresponding this element.
*
* @return string
* @return \SimpleSAML\XML\Type\QNameValue
*/
public function getXsiType(): string;
public function getXsiType(): QNameValue;
}
30 changes: 15 additions & 15 deletions src/SAML11/XML/ExtensionPointTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

use RuntimeException;
use SimpleSAML\SAML11\Assert\Assert;
use SimpleSAML\XML\Exception\SchemaViolationException;
use SimpleSAML\XML\Type\{AnyURIValue, NCNameValue, QNameValue};

use function constant;
use function defined;

/**
* Trait for several extension points objects.
Expand All @@ -16,9 +19,9 @@
trait ExtensionPointTrait
{
/**
* @inheritDoc
* @return \SimpleSAML\XML\Type\QNameValue
*/
public function getXsiType(): string
public function getXsiType(): QNameValue
{
return $this->type;
}
Expand All @@ -27,9 +30,9 @@ public function getXsiType(): string
/**
* Get the local name for the element's xsi:type.
*
* @return string
* @return \SimpleSAML\XML\Type\NCNameValue
*/
public static function getXsiTypeName(): string
public static function getXsiTypeName(): NCNameValue
{
Assert::true(
defined('static::XSI_TYPE_NAME'),
Expand All @@ -38,17 +41,16 @@ public static function getXsiTypeName(): string
RuntimeException::class,
);

Assert::validNCName(static::XSI_TYPE_NAME, SchemaViolationException::class);
return static::XSI_TYPE_NAME;
return NCNameValue::fromString(constant('static::XSI_TYPE_NAME'));
}


/**
* Get the namespace for the element's xsi:type.
*
* @return string
* @return \SimpleSAML\XML\Type\AnyURIValue
*/
public static function getXsiTypeNamespaceURI(): string
public static function getXsiTypeNamespaceURI(): AnyURIValue
{
Assert::true(
defined('static::XSI_TYPE_NAMESPACE'),
Expand All @@ -57,17 +59,16 @@ public static function getXsiTypeNamespaceURI(): string
RuntimeException::class,
);

Assert::validURI(static::XSI_TYPE_NAMESPACE, SchemaViolationException::class);
return static::XSI_TYPE_NAMESPACE;
return AnyURIValue::fromString(constant('static::XSI_TYPE_NAMESPACE'));
}


/**
* Get the namespace-prefix for the element's xsi:type.
*
* @return string
* @return \SimpleSAML\XML\Type\NCNameValue
*/
public static function getXsiTypePrefix(): string
public static function getXsiTypePrefix(): NCNameValue
{
Assert::true(
defined('static::XSI_TYPE_PREFIX'),
Expand All @@ -78,7 +79,6 @@ public static function getXsiTypePrefix(): string
RuntimeException::class,
);

Assert::validNCName(static::XSI_TYPE_PREFIX, SchemaViolationException::class);
return static::XSI_TYPE_PREFIX;
return NCNameValue::fromString(constant('static::XSI_TYPE_PREFIX'));
}
}
Loading

0 comments on commit 1239661

Please sign in to comment.