simplesamlphp /
saml2
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace SimpleSAML\SAML2\XML\mdui; |
||
| 6 | |||
| 7 | use DOMElement; |
||
| 8 | use SimpleSAML\SAML2\Assert\Assert; |
||
| 9 | use SimpleSAML\SAML2\Exception\ArrayValidationException; |
||
| 10 | use SimpleSAML\SAML2\Exception\ProtocolViolationException; |
||
| 11 | use SimpleSAML\SAML2\Type\SAMLAnyURIValue; |
||
| 12 | use SimpleSAML\XML\ArrayizableElementInterface; |
||
| 13 | use SimpleSAML\XML\SchemaValidatableElementInterface; |
||
| 14 | use SimpleSAML\XML\SchemaValidatableElementTrait; |
||
| 15 | use SimpleSAML\XML\TypedTextContentTrait; |
||
| 16 | use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException; |
||
| 17 | use SimpleSAML\XMLSchema\Exception\SchemaViolationException; |
||
| 18 | use SimpleSAML\XMLSchema\Type\LanguageValue; |
||
| 19 | use SimpleSAML\XMLSchema\Type\PositiveIntegerValue; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Class for handling the Logo metadata extensions for login and discovery user interface |
||
| 23 | * |
||
| 24 | * @link: http://docs.oasis-open.org/security/saml/Post2.0/sstc-saml-metadata-ui/v1.0/sstc-saml-metadata-ui-v1.0.pdf |
||
| 25 | * @package simplesamlphp/saml2 |
||
| 26 | */ |
||
| 27 | final class Logo extends AbstractMduiElement implements |
||
| 28 | ArrayizableElementInterface, |
||
| 29 | SchemaValidatableElementInterface |
||
| 30 | { |
||
| 31 | use SchemaValidatableElementTrait; |
||
| 32 | use TypedTextContentTrait; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 33 | |||
| 34 | |||
| 35 | /** @var string */ |
||
| 36 | public const TEXTCONTENT_TYPE = SAMLAnyURIValue::class; |
||
| 37 | |||
| 38 | |||
| 39 | /** @var string */ |
||
| 40 | private static string $scheme_regex = '/^(data|http[s]?[:])/i'; |
||
| 41 | |||
| 42 | |||
| 43 | /** |
||
| 44 | * Initialize a Logo. |
||
| 45 | * |
||
| 46 | * @param \SimpleSAML\SAML2\Type\SAMLAnyURIValue $url |
||
| 47 | * @param \SimpleSAML\XMLSchema\Type\PositiveIntegerValue $height |
||
| 48 | * @param \SimpleSAML\XMLSchema\Type\PositiveIntegerValue $width |
||
| 49 | * @param \SimpleSAML\XMLSchema\Type\LanguageValue|null $lang |
||
| 50 | */ |
||
| 51 | public function __construct( |
||
| 52 | SAMLAnyURIValue $url, |
||
| 53 | protected PositiveIntegerValue $height, |
||
| 54 | protected PositiveIntegerValue $width, |
||
| 55 | protected ?LanguageValue $lang = null, |
||
| 56 | ) { |
||
| 57 | $this->setContent($url); |
||
| 58 | } |
||
| 59 | |||
| 60 | |||
| 61 | /** |
||
| 62 | * Validate the content of the element. |
||
| 63 | * |
||
| 64 | * @param string $content The value to go in the XML textContent |
||
| 65 | * @throws \InvalidArgumentException on failure |
||
| 66 | * @return void |
||
| 67 | */ |
||
| 68 | protected function validateContent(string $content): void |
||
| 69 | { |
||
| 70 | Assert::validURI($content, SchemaViolationException::class); |
||
| 71 | Assert::regex(self::$scheme_regex, $content, ProtocolViolationException::class); |
||
| 72 | } |
||
| 73 | |||
| 74 | |||
| 75 | /** |
||
| 76 | * Collect the value of the lang-property |
||
| 77 | * |
||
| 78 | * @return \SimpleSAML\XMLSchema\Type\LanguageValue|null |
||
| 79 | */ |
||
| 80 | public function getLanguage(): ?LanguageValue |
||
| 81 | { |
||
| 82 | return $this->lang; |
||
| 83 | } |
||
| 84 | |||
| 85 | |||
| 86 | /** |
||
| 87 | * Collect the value of the height-property |
||
| 88 | * |
||
| 89 | * @return \SimpleSAML\XMLSchema\Type\PositiveIntegerValue |
||
| 90 | */ |
||
| 91 | public function getHeight(): PositiveIntegerValue |
||
| 92 | { |
||
| 93 | return $this->height; |
||
| 94 | } |
||
| 95 | |||
| 96 | |||
| 97 | /** |
||
| 98 | * Collect the value of the width-property |
||
| 99 | * |
||
| 100 | * @return \SimpleSAML\XMLSchema\Type\PositiveIntegerValue |
||
| 101 | */ |
||
| 102 | public function getWidth(): PositiveIntegerValue |
||
| 103 | { |
||
| 104 | return $this->width; |
||
| 105 | } |
||
| 106 | |||
| 107 | |||
| 108 | /** |
||
| 109 | * Convert XML into a Logo |
||
| 110 | * |
||
| 111 | * @param \DOMElement $xml The XML element we should load |
||
| 112 | * @return static |
||
| 113 | * |
||
| 114 | * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException |
||
| 115 | * if the qualified name of the supplied element is wrong |
||
| 116 | * @throws \SimpleSAML\XMLSchema\Exception\MissingAttributeException |
||
| 117 | * if the supplied element is missing one of the mandatory attributes |
||
| 118 | */ |
||
| 119 | public static function fromXML(DOMElement $xml): static |
||
| 120 | { |
||
| 121 | Assert::same($xml->localName, 'Logo', InvalidDOMElementException::class); |
||
| 122 | Assert::same($xml->namespaceURI, Logo::NS, InvalidDOMElementException::class); |
||
| 123 | Assert::stringNotEmpty($xml->textContent, 'Missing url value for Logo.'); |
||
| 124 | |||
| 125 | $Url = SAMLAnyURIValue::fromString($xml->textContent); |
||
| 126 | $Width = self::getAttribute($xml, 'width', PositiveIntegerValue::class); |
||
| 127 | $Height = self::getAttribute($xml, 'height', PositiveIntegerValue::class); |
||
| 128 | $lang = self::getOptionalAttribute($xml, 'xml:lang', LanguageValue::class, null); |
||
| 129 | |||
| 130 | return new static($Url, $Height, $Width, $lang); |
||
| 131 | } |
||
| 132 | |||
| 133 | |||
| 134 | /** |
||
| 135 | * Convert this Logo to XML. |
||
| 136 | * |
||
| 137 | * @param \DOMElement|null $parent The element we should append this Logo to. |
||
| 138 | * @return \DOMElement |
||
| 139 | */ |
||
| 140 | public function toXML(?DOMElement $parent = null): DOMElement |
||
| 141 | { |
||
| 142 | $e = $this->instantiateParentElement($parent); |
||
| 143 | $e->textContent = $this->getContent()->getValue(); |
||
| 144 | $e->setAttribute('height', $this->getHeight()->getValue()); |
||
| 145 | $e->setAttribute('width', $this->getWidth()->getValue()); |
||
| 146 | |||
| 147 | if ($this->getLanguage() !== null) { |
||
| 148 | $e->setAttribute('xml:lang', $this->getLanguage()->getValue()); |
||
| 149 | } |
||
| 150 | |||
| 151 | return $e; |
||
| 152 | } |
||
| 153 | |||
| 154 | |||
| 155 | /** |
||
| 156 | * Create a class from an array |
||
| 157 | * |
||
| 158 | * @param array $data |
||
| 159 | * @return static |
||
| 160 | */ |
||
| 161 | public static function fromArray(array $data): static |
||
| 162 | { |
||
| 163 | $data = self::processArrayContents($data); |
||
| 164 | |||
| 165 | return new static( |
||
| 166 | SAMLAnyURIValue::fromString($data['url']), |
||
| 167 | PositiveIntegerValue::fromInteger($data['height']), |
||
| 168 | PositiveIntegerValue::fromInteger($data['width']), |
||
| 169 | $data['lang'] !== null ? LanguageValue::fromString($data['lang']) : null, |
||
| 170 | ); |
||
| 171 | } |
||
| 172 | |||
| 173 | |||
| 174 | /** |
||
| 175 | * Validates an array representation of this object and returns the same array with |
||
| 176 | * rationalized keys (casing) and parsed sub-elements. |
||
| 177 | * |
||
| 178 | * @param array $data |
||
| 179 | * @return array $data |
||
| 180 | */ |
||
| 181 | private static function processArrayContents(array $data): array |
||
| 182 | { |
||
| 183 | $data = array_change_key_case($data, CASE_LOWER); |
||
| 184 | |||
| 185 | // Make sure the array keys are known for this kind of object |
||
| 186 | Assert::allOneOf( |
||
| 187 | array_keys($data), |
||
| 188 | [ |
||
| 189 | 'url', |
||
| 190 | 'height', |
||
| 191 | 'width', |
||
| 192 | 'lang', |
||
| 193 | ], |
||
| 194 | ArrayValidationException::class, |
||
| 195 | ); |
||
| 196 | |||
| 197 | Assert::keyExists($data, 'url', ArrayValidationException::class); |
||
| 198 | Assert::keyExists($data, 'height', ArrayValidationException::class); |
||
| 199 | Assert::keyExists($data, 'width', ArrayValidationException::class); |
||
| 200 | |||
| 201 | Assert::integer($data['height'], ArrayValidationException::class); |
||
| 202 | Assert::integer($data['width'], ArrayValidationException::class); |
||
| 203 | |||
| 204 | $retval = [ |
||
| 205 | 'url' => $data['url'], |
||
| 206 | 'height' => $data['height'], |
||
| 207 | 'width' => $data['width'], |
||
| 208 | ]; |
||
| 209 | |||
| 210 | if (array_key_exists('lang', $data)) { |
||
| 211 | Assert::string($data['lang'], ArrayValidationException::class); |
||
| 212 | $retval['lang'] = $data['lang']; |
||
| 213 | } |
||
| 214 | |||
| 215 | return $retval; |
||
| 216 | } |
||
| 217 | |||
| 218 | |||
| 219 | /** |
||
| 220 | * Create an array from this class |
||
| 221 | * |
||
| 222 | * @return array |
||
| 223 | */ |
||
| 224 | public function toArray(): array |
||
| 225 | { |
||
| 226 | $lang = $this->getLanguage()?->getValue(); |
||
| 227 | |||
| 228 | return [ |
||
| 229 | 'url' => $this->getContent()->getValue(), |
||
| 230 | 'width' => $this->getWidth()->toInteger(), |
||
| 231 | 'height' => $this->getHeight()->toInteger(), |
||
| 232 | ] + (isset($lang) ? ['lang' => $lang] : []); |
||
| 233 | } |
||
| 234 | } |
||
| 235 |