vendor/lightsaml2/lightsaml/src/LightSaml/Model/Metadata/EntityDescriptor.php line 53

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the LightSAML-Core package.
  4.  *
  5.  * (c) Milos Tomic <tmilos@lightsaml.com>
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace LightSaml\Model\Metadata;
  11. use LightSaml\Helper;
  12. use LightSaml\Model\Context\DeserializationContext;
  13. use LightSaml\Model\Context\SerializationContext;
  14. use LightSaml\Model\XmlDSig\Signature;
  15. use LightSaml\SamlConstants;
  16. class EntityDescriptor extends Metadata
  17. {
  18.     /** @var string */
  19.     protected $entityID;
  20.     /** @var int|null */
  21.     protected $validUntil;
  22.     /** @var string|null */
  23.     protected $cacheDuration;
  24.     /** @var string|null */
  25.     protected $id;
  26.     /** @var Signature|null */
  27.     protected $signature;
  28.     /** @var IdpSsoDescriptor[]|SpSsoDescriptor[] */
  29.     protected $items;
  30.     /** @var Organization[]|null */
  31.     protected $organizations;
  32.     /** @var ContactPerson[]|null */
  33.     protected $contactPersons;
  34.     /**
  35.      * @param string $filename
  36.      *
  37.      * @return EntityDescriptor
  38.      */
  39.     public static function load($filename)
  40.     {
  41.         return self::loadXml(file_get_contents($filename));
  42.     }
  43.     /**
  44.      * @param string $xml
  45.      *
  46.      * @return EntityDescriptor
  47.      */
  48.     public static function loadXml($xml)
  49.     {
  50.         $context = new DeserializationContext();
  51.         $context->getDocument()->loadXML($xml);
  52.         $ed = new self();
  53.         $ed->deserialize($context->getDocument(), $context);
  54.         return $ed;
  55.     }
  56.     /**
  57.      * @param string|null $entityId
  58.      */
  59.     public function __construct($entityId null, array $items = [])
  60.     {
  61.         $this->entityID $entityId;
  62.         $this->items $items;
  63.     }
  64.     /**
  65.      * @return EntityDescriptor
  66.      */
  67.     public function addContactPerson(ContactPerson $contactPerson)
  68.     {
  69.         if (false == is_array($this->contactPersons)) {
  70.             $this->contactPersons = [];
  71.         }
  72.         $this->contactPersons[] = $contactPerson;
  73.         return $this;
  74.     }
  75.     /**
  76.      * @return ContactPerson[]|null
  77.      */
  78.     public function getAllContactPersons()
  79.     {
  80.         return $this->contactPersons;
  81.     }
  82.     /**
  83.      * @return ContactPerson|null
  84.      */
  85.     public function getFirstContactPerson()
  86.     {
  87.         if (is_array($this->contactPersons) && isset($this->contactPersons[0])) {
  88.             return $this->contactPersons[0];
  89.         }
  90.         return null;
  91.     }
  92.     /**
  93.      * @param \LightSaml\Model\Metadata\Organization $organization
  94.      *
  95.      * @return EntityDescriptor
  96.      */
  97.     public function addOrganization(Organization $organization)
  98.     {
  99.         if (false == is_array($this->organizations)) {
  100.             $this->organizations = [];
  101.         }
  102.         $this->organizations[] = $organization;
  103.         return $this;
  104.     }
  105.     /**
  106.      * @return Organization[]|null
  107.      */
  108.     public function getAllOrganizations()
  109.     {
  110.         return $this->organizations;
  111.     }
  112.     /**
  113.      * @return \LightSaml\Model\Metadata\Organization|null
  114.      */
  115.     public function getFirstOrganization()
  116.     {
  117.         if (is_array($this->organizations) && isset($this->organizations[0])) {
  118.             return $this->organizations[0];
  119.         }
  120.         return null;
  121.     }
  122.     /**
  123.      * @param string|null $cacheDuration
  124.      *
  125.      * @throws \InvalidArgumentException
  126.      *
  127.      * @return EntityDescriptor
  128.      */
  129.     public function setCacheDuration($cacheDuration)
  130.     {
  131.         Helper::validateDurationString($cacheDuration);
  132.         $this->cacheDuration $cacheDuration;
  133.         return $this;
  134.     }
  135.     /**
  136.      * @return string|null
  137.      */
  138.     public function getCacheDuration()
  139.     {
  140.         return $this->cacheDuration;
  141.     }
  142.     /**
  143.      * @param string $entityID
  144.      *
  145.      * @return EntityDescriptor
  146.      */
  147.     public function setEntityID($entityID)
  148.     {
  149.         $this->entityID = (string) $entityID;
  150.         return $this;
  151.     }
  152.     /**
  153.      * @return string
  154.      */
  155.     public function getEntityID()
  156.     {
  157.         return $this->entityID;
  158.     }
  159.     /**
  160.      * @param string|null $id
  161.      *
  162.      * @return EntityDescriptor
  163.      */
  164.     public function setID($id)
  165.     {
  166.         $this->id null !== $id ? (string) $id null;
  167.         return $this;
  168.     }
  169.     /**
  170.      * @return string|null
  171.      */
  172.     public function getID()
  173.     {
  174.         return $this->id;
  175.     }
  176.     /**
  177.      * @param \LightSaml\Model\Metadata\IdpSsoDescriptor|\LightSaml\Model\Metadata\SpSsoDescriptor $item
  178.      *
  179.      * @throws \InvalidArgumentException
  180.      *
  181.      * @return EntityDescriptor
  182.      */
  183.     public function addItem($item)
  184.     {
  185.         if (false == $item instanceof IdpSsoDescriptor &&
  186.             false == $item instanceof SpSsoDescriptor
  187.         ) {
  188.             throw new \InvalidArgumentException('EntityDescriptor item must be IdpSsoDescriptor or SpSsoDescriptor');
  189.         }
  190.         if (false == is_array($this->items)) {
  191.             $this->items = [];
  192.         }
  193.         $this->items[] = $item;
  194.         return $this;
  195.     }
  196.     /**
  197.      * @return IdpSsoDescriptor[]|SpSsoDescriptor[]|SSODescriptor[]
  198.      */
  199.     public function getAllItems()
  200.     {
  201.         return $this->items;
  202.     }
  203.     /**
  204.      * @return IdpSsoDescriptor[]
  205.      */
  206.     public function getAllIdpSsoDescriptors()
  207.     {
  208.         $result = [];
  209.         foreach ($this->getAllItems() as $item) {
  210.             if ($item instanceof IdpSsoDescriptor) {
  211.                 $result[] = $item;
  212.             }
  213.         }
  214.         return $result;
  215.     }
  216.     /**
  217.      * @return SpSsoDescriptor[]
  218.      */
  219.     public function getAllSpSsoDescriptors()
  220.     {
  221.         $result = [];
  222.         foreach ($this->getAllItems() as $item) {
  223.             if ($item instanceof SpSsoDescriptor) {
  224.                 $result[] = $item;
  225.             }
  226.         }
  227.         return $result;
  228.     }
  229.     /**
  230.      * @return IdpSsoDescriptor|null
  231.      */
  232.     public function getFirstIdpSsoDescriptor()
  233.     {
  234.         foreach ($this->getAllItems() as $item) {
  235.             if ($item instanceof IdpSsoDescriptor) {
  236.                 return $item;
  237.             }
  238.         }
  239.         return null;
  240.     }
  241.     /**
  242.      * @return SpSsoDescriptor|null
  243.      */
  244.     public function getFirstSpSsoDescriptor()
  245.     {
  246.         foreach ($this->getAllItems() as $item) {
  247.             if ($item instanceof SpSsoDescriptor) {
  248.                 return $item;
  249.             }
  250.         }
  251.         return null;
  252.     }
  253.     /**
  254.      * @param Signature|null $signature
  255.      *
  256.      * @return EntityDescriptor
  257.      */
  258.     public function setSignature(Signature $signature)
  259.     {
  260.         $this->signature $signature;
  261.         return $this;
  262.     }
  263.     /**
  264.      * @return Signature|null
  265.      */
  266.     public function getSignature()
  267.     {
  268.         return $this->signature;
  269.     }
  270.     /**
  271.      * @param int $validUntil
  272.      *
  273.      * @return EntityDescriptor
  274.      */
  275.     public function setValidUntil($validUntil)
  276.     {
  277.         $this->validUntil Helper::getTimestampFromValue($validUntil);
  278.         return $this;
  279.     }
  280.     /**
  281.      * @return int|null
  282.      */
  283.     public function getValidUntilTimestamp()
  284.     {
  285.         return $this->validUntil;
  286.     }
  287.     /**
  288.      * @return string|null
  289.      */
  290.     public function getValidUntilString()
  291.     {
  292.         if ($this->validUntil) {
  293.             return Helper::time2string($this->validUntil);
  294.         }
  295.         return null;
  296.     }
  297.     /**
  298.      * @return \DateTime|null
  299.      */
  300.     public function getValidUntilDateTime()
  301.     {
  302.         if ($this->validUntil) {
  303.             return new \DateTime('@'.$this->validUntil);
  304.         }
  305.         return null;
  306.     }
  307.     /**
  308.      * @return array|KeyDescriptor[]
  309.      */
  310.     public function getAllIdpKeyDescriptors()
  311.     {
  312.         $result = [];
  313.         foreach ($this->getAllIdpSsoDescriptors() as $idp) {
  314.             foreach ($idp->getAllKeyDescriptors() as $key) {
  315.                 $result[] = $key;
  316.             }
  317.         }
  318.         return $result;
  319.     }
  320.     /**
  321.      * @return array|KeyDescriptor[]
  322.      */
  323.     public function getAllSpKeyDescriptors()
  324.     {
  325.         $result = [];
  326.         foreach ($this->getAllSpSsoDescriptors() as $sp) {
  327.             foreach ($sp->getAllKeyDescriptors() as $key) {
  328.                 $result[] = $key;
  329.             }
  330.         }
  331.         return $result;
  332.     }
  333.     /**
  334.      * @return EndpointReference[]
  335.      */
  336.     public function getAllEndpoints()
  337.     {
  338.         $result = [];
  339.         foreach ($this->getAllIdpSsoDescriptors() as $idpSsoDescriptor) {
  340.             foreach ($idpSsoDescriptor->getAllSingleSignOnServices() as $sso) {
  341.                 $result[] = new EndpointReference($this$idpSsoDescriptor$sso);
  342.             }
  343.             foreach ($idpSsoDescriptor->getAllSingleLogoutServices() as $slo) {
  344.                 $result[] = new EndpointReference($this$idpSsoDescriptor$slo);
  345.             }
  346.         }
  347.         foreach ($this->getAllSpSsoDescriptors() as $spSsoDescriptor) {
  348.             foreach ($spSsoDescriptor->getAllAssertionConsumerServices() as $acs) {
  349.                 $result[] = new EndpointReference($this$spSsoDescriptor$acs);
  350.             }
  351.             foreach ($spSsoDescriptor->getAllSingleLogoutServices() as $slo) {
  352.                 $result[] = new EndpointReference($this$spSsoDescriptor$slo);
  353.             }
  354.         }
  355.         return $result;
  356.     }
  357.     /**
  358.      * @return void
  359.      */
  360.     public function serialize(\DOMNode $parentSerializationContext $context)
  361.     {
  362.         $result $this->createElement('EntityDescriptor'SamlConstants::NS_METADATA$parent$context);
  363.         $this->attributesToXml(['entityID''validUntil''cacheDuration''ID'], $result);
  364.         $this->manyElementsToXml($this->getAllItems(), $result$contextnull);
  365.         if ($this->organizations) {
  366.             $this->manyElementsToXml($this->organizations$result$contextnull);
  367.         }
  368.         if ($this->contactPersons) {
  369.             $this->manyElementsToXml($this->contactPersons$result$contextnull);
  370.         }
  371.         $this->singleElementsToXml(['Signature'], $result$context);
  372.     }
  373.     public function deserialize(\DOMNode $nodeDeserializationContext $context)
  374.     {
  375.         $this->checkXmlNodeName($node'EntityDescriptor'SamlConstants::NS_METADATA);
  376.         $this->attributesFromXml($node, ['entityID''validUntil''cacheDuration''ID']);
  377.         $this->items = [];
  378.         $this->manyElementsFromXml(
  379.             $node,
  380.             $context,
  381.             'IDPSSODescriptor',
  382.             'md',
  383.             'LightSaml\Model\Metadata\IdpSsoDescriptor',
  384.             'addItem'
  385.         );
  386.         $this->manyElementsFromXml(
  387.             $node,
  388.             $context,
  389.             'SPSSODescriptor',
  390.             'md',
  391.             'LightSaml\Model\Metadata\SpSsoDescriptor',
  392.             'addItem'
  393.         );
  394.         $this->manyElementsFromXml(
  395.             $node,
  396.             $context,
  397.             'Organization',
  398.             'md',
  399.             'LightSaml\Model\Metadata\Organization',
  400.             'addOrganization'
  401.         );
  402.         $this->manyElementsFromXml(
  403.             $node,
  404.             $context,
  405.             'ContactPerson',
  406.             'md',
  407.             'LightSaml\Model\Metadata\ContactPerson',
  408.             'addContactPerson'
  409.         );
  410.         $this->singleElementsFromXml($node$context, [
  411.             'Signature' => ['ds''LightSaml\Model\XmlDSig\SignatureXmlReader'],
  412.         ]);
  413.     }
  414. }