Skip to content

Commit

Permalink
Start using PHP 8.1 features
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Dec 11, 2023
1 parent b74bd85 commit 5dd9719
Show file tree
Hide file tree
Showing 22 changed files with 156 additions and 91 deletions.
12 changes: 3 additions & 9 deletions src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,10 @@ class Constants extends \SimpleSAML\XML\Constants
/**
* The namespace for the CAS protocol.
*/
public const NS_CAS = 'http://www.yale.edu/tp/cas';
final public const NS_CAS = 'http://www.yale.edu/tp/cas';

/**
* The error codes defined by the CAS protocol specification
* The format to express a timestamp in CAS
*/
public const ERR_INVALID_REQUEST = 'INVALID_REQUEST';
public const ERR_INVALID_TICKET_SPEC = 'INVALID_TICKET_SPEC';
public const ERR_UNAUTHORIZED_SERVICE_PROXY = 'UNAUTHORIZED_SERVICE_PROXY';
public const ERR_INVALID_PROXY_CALLBACK = 'INVALID_PROXY_CALLBACK';
public const ERR_INVALID_TICKET = 'INVALID_TICKET';
public const ERR_INVALID_SERVICE = 'INVALID_SERVICE';
public const ERR_INTERNAL_ERROR = 'INTERNAL_ERROR';
final public const DATETIME_FORMAT = 'Y-m-d\\TH:i:sp';
}
19 changes: 19 additions & 0 deletions src/Error.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\CAS;

/**
* The error codes defined by the CAS protocol specification
*/
enum Error: string
{
case INVALID_REQUEST = 'INVALID_REQUEST';
case INVALID_TICKET_SPEC = 'INVALID_TICKET_SPEC';
case UNAUTHORIZED_SERVICE_PROXY = 'UNAUTHORIZED_SERVICE_PROXY';
case INVALID_PROXY_CALLBACK = 'INVALID_PROXY_CALLBACK';
case INVALID_TICKET = 'INVALID_TICKET';
case INVALID_SERVICE = 'INVALID_SERVICE';
case INTERNAL_ERROR = 'INTERNAL_ERROR';
}
10 changes: 6 additions & 4 deletions src/XML/cas/Attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use SimpleSAML\XML\Exception\MissingElementException;
use SimpleSAML\XML\ExtendableElementTrait;

use function array_pop;

/**
* Class for CAS attributes
*
Expand All @@ -22,10 +24,10 @@ final class Attributes extends AbstractCasElement
use ExtendableElementTrait;

/** @var string */
public const LOCALNAME = 'attributes';
final public const LOCALNAME = 'attributes';

/** The namespace-attribute for the xs:any element */
public const XS_ANY_ELT_NAMESPACE = C::XS_ANY_NS_ANY;
final public const XS_ANY_ELT_NAMESPACE = C::XS_ANY_NS_ANY;


/**
Expand Down Expand Up @@ -86,8 +88,8 @@ public function getIsFromNewLogin(): IsFromNewLogin
*/
public static function fromXML(DOMElement $xml): static
{
Assert::same($xml->localName, 'attributes', InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, Attributes::NS, InvalidDOMElementException::class);
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, static::getNamespaceURI(), InvalidDOMElementException::class);

$authenticationDate = AuthenticationDate::getChildrenOfClass($xml);
Assert::count(
Expand Down
43 changes: 27 additions & 16 deletions src/XML/cas/AuthenticationDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

namespace SimpleSAML\CAS\XML\cas;

use DateTimeImmutable;
use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\CAS\Constants as C;
use SimpleSAML\CAS\Exception\ProtocolViolationException;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\StringElementTrait;

/**
* Class for CAS authenticationDate
Expand All @@ -17,31 +18,41 @@
*/
final class AuthenticationDate extends AbstractCasElement
{
use StringElementTrait;

/** @var string */
public const LOCALNAME = 'authenticationDate';
final public const LOCALNAME = 'authenticationDate';


/**
* @param \DateTimeImmutable $timestamp
*/
final public function __construct(
protected DateTimeImmutable $timestamp
) {
}


/**
* @param string $content
* Retrieve the issue timestamp of this message.
*
* @return \DateTimeImmutable The issue timestamp of this message, as an UNIX timestamp
*/
final public function __construct(string $content)
public function getTimestamp(): DateTimeImmutable
{
$this->setContent($content);
return $this->timestamp;
}


/**
* Validate the content of the element.
* Convert this element into an XML document.
*
* @param string $content The value to go in the XML textContent
* @throws \Exception on failure
* @return void
* @return \DOMElement The root element of the DOM tree
*/
protected function validateContent(string $content): void
public function toXML(?DOMElement $parent = null): DOMElement
{
Assert::validDateTimeZulu($content, ProtocolViolationException::class);
$root = $this->instantiateParentElement($parent);
$root->textContent = $this->getTimestamp()->format(C::DATETIME_FORMAT);

return $root;
}


Expand All @@ -56,9 +67,9 @@ protected function validateContent(string $content): void
*/
public static function fromXML(DOMElement $xml): static
{
Assert::same($xml->localName, 'authenticationDate', InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, AuthenticationDate::NS, InvalidDOMElementException::class);
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, static::getNamespaceURI(), InvalidDOMElementException::class);

return new static($xml->textContent);
return new static(new DateTimeImmutable($xml->textContent));
}
}
22 changes: 12 additions & 10 deletions src/XML/cas/AuthenticationFailure.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\CAS\Error;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\MissingAttributeException;
use SimpleSAML\XML\StringElementTrait;

use function trim;

/**
* Class for CAS authenticationFailure
*
Expand All @@ -20,30 +23,29 @@ final class AuthenticationFailure extends AbstractResponse
use StringElementTrait;

/** @var string */
public const LOCALNAME = 'authenticationFailure';
final public const LOCALNAME = 'authenticationFailure';


/**
* Create a new instance of AuthenticationFailure
*
* @param string $content
* @param string $code
* @param \SimpleSAML\CAS\Error $code
*/
final public function __construct(
string $content,
protected string $code,
protected Error $code,
) {
Assert::notEmpty($code, 'The code in AuthenticationFailure must not be a empty.');
$this->setContent($content);
}


/**
* Collect the value of the code-property
*
* @return string
* @return \SimpleSAML\CAS\Error
*/
public function getCode(): string
public function getCode(): Error
{
return $this->code;
}
Expand Down Expand Up @@ -75,15 +77,15 @@ protected function validateContent(string $content): void
*/
public static function fromXML(DOMElement $xml): static
{
Assert::same($xml->localName, 'authenticationFailure', InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, ProxyFailure::NS, InvalidDOMElementException::class);
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, static::getNamespaceURI(), InvalidDOMElementException::class);
Assert::true(
$xml->hasAttribute('code'),
'Missing code from ' . static::getLocalName(),
MissingAttributeException::class,
);

$code = self::getAttribute($xml, 'code');
$code = Error::from(self::getAttribute($xml, 'code'));
return new static(trim($xml->textContent), $code);
}

Expand All @@ -98,7 +100,7 @@ public function toXML(DOMElement $parent = null): DOMElement
{
$e = $this->instantiateParentElement($parent);
$e->textContent = $this->getContent();
$e->setAttribute('code', $this->getCode());
$e->setAttribute('code', $this->getCode()->value);

return $e;
}
Expand Down
8 changes: 5 additions & 3 deletions src/XML/cas/AuthenticationSuccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\MissingElementException;

use function array_pop;

/**
* Class for CAS authenticationSuccess
*
Expand All @@ -18,7 +20,7 @@
final class AuthenticationSuccess extends AbstractResponse
{
/** @var string */
public const LOCALNAME = 'authenticationSuccess';
final public const LOCALNAME = 'authenticationSuccess';


/**
Expand Down Expand Up @@ -87,8 +89,8 @@ public function getProxies(): ?Proxies
*/
public static function fromXML(DOMElement $xml): static
{
Assert::same($xml->localName, 'authenticationSuccess', InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, AuthenticationSuccess::NS, InvalidDOMElementException::class);
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, static::getNamespaceURI(), InvalidDOMElementException::class);

$user = User::getChildrenOfClass($xml);
Assert::count(
Expand Down
8 changes: 4 additions & 4 deletions src/XML/cas/IsFromNewLogin.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class IsFromNewLogin extends AbstractCasElement
use StringElementTrait;

/** @var string */
public const LOCALNAME = 'isFromNewLogin';
final public const LOCALNAME = 'isFromNewLogin';


/**
Expand All @@ -45,7 +45,7 @@ protected function validateContent(string $content): void
Assert::oneOf(
$content,
['true', 'false', '0', '1'],
'The value of ' . static::getNamespacePrefix() . ':' . self::getLocalName() . ' must be boolean.',
'The value of ' . static::getNamespacePrefix() . ':' . static::getLocalName() . ' must be boolean.',
ProtocolViolationException::class,
);
}
Expand All @@ -62,8 +62,8 @@ protected function validateContent(string $content): void
*/
public static function fromXML(DOMElement $xml): static
{
Assert::same($xml->localName, 'isFromNewLogin', InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, IsFromNewLogin::NS, InvalidDOMElementException::class);
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, static::getNamespaceURI(), InvalidDOMElementException::class);

return new static($xml->textContent);
}
Expand Down
8 changes: 4 additions & 4 deletions src/XML/cas/LongTermAuthenticationRequestTokenUsed.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class LongTermAuthenticationRequestTokenUsed extends AbstractCasElement
use StringElementTrait;

/** @var string */
public const LOCALNAME = 'longTermAuthenticationRequestTokenUsed';
final public const LOCALNAME = 'longTermAuthenticationRequestTokenUsed';


/**
Expand All @@ -45,7 +45,7 @@ protected function validateContent(string $content): void
Assert::oneOf(
$content,
['true', 'false', '0', '1'],
'The value of ' . static::getNamespacePrefix() . ':' . self::getLocalName() . ' must be boolean.',
'The value of ' . static::getNamespacePrefix() . ':' . static::getLocalName() . ' must be boolean.',
ProtocolViolationException::class,
);
}
Expand All @@ -62,8 +62,8 @@ protected function validateContent(string $content): void
*/
public static function fromXML(DOMElement $xml): static
{
Assert::same($xml->localName, 'longTermAuthenticationRequestTokenUsed', InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, LongTermAuthenticationRequestTokenUsed::NS, InvalidDOMElementException::class);
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, static::getNamespaceURI(), InvalidDOMElementException::class);

return new static($xml->textContent);
}
Expand Down
7 changes: 4 additions & 3 deletions src/XML/cas/Proxies.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use DOMElement;
use SimpleSAML\Assert\Assert;
use SimpleSAML\CAS\Constants as C;
use SimpleSAML\XML\Exception\InvalidDOMElementException;
use SimpleSAML\XML\Exception\MissingElementException;

Expand All @@ -17,7 +18,7 @@
final class Proxies extends AbstractCasElement
{
/** @var string */
public const LOCALNAME = 'proxies';
final public const LOCALNAME = 'proxies';


/**
Expand Down Expand Up @@ -56,8 +57,8 @@ public function getProxy(): array
*/
public static function fromXML(DOMElement $xml): static
{
Assert::same($xml->localName, 'proxies', InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, Proxies::NS, InvalidDOMElementException::class);
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, static::getNamespaceURI(), InvalidDOMElementException::class);

$proxy = Proxy::getChildrenOfClass($xml);
Assert::minCount($proxy, 1, 'Missing at least one Proxy in Proxies.', MissingElementException::class);
Expand Down
6 changes: 3 additions & 3 deletions src/XML/cas/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class Proxy extends AbstractCasElement
use StringElementTrait;

/** @var string */
public const LOCALNAME = 'proxy';
final public const LOCALNAME = 'proxy';


/**
Expand Down Expand Up @@ -55,8 +55,8 @@ protected function validateContent(string $content): void
*/
public static function fromXML(DOMElement $xml): static
{
Assert::same($xml->localName, 'proxy', InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, Proxy::NS, InvalidDOMElementException::class);
Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
Assert::same($xml->namespaceURI, static::getNamespaceURI(), InvalidDOMElementException::class);

return new static($xml->textContent);
}
Expand Down
Loading

0 comments on commit 5dd9719

Please sign in to comment.