Skip to content

Commit

Permalink
HfdTagService ready without facade and testing
Browse files Browse the repository at this point in the history
  • Loading branch information
temaotl committed Aug 6, 2024
1 parent 0f338b0 commit f1bc624
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 3 deletions.
109 changes: 109 additions & 0 deletions app/Services/HfdTagService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace App\Services;

use App\Models\Entity;
use App\Traits\EntitiesXML\TagTrait;
use App\Traits\ValidatorTrait;
use DOMXPath;

class HfdTagService
{
use TagTrait,ValidatorTrait;

private string $value = 'http://refeds.org/category/hide-from-discovery';

public function create(Entity $entity): false|string
{
$mdURI = config('xmlNameSpace.md');
$mdattrURI = config('xmlNameSpace.mdattr');
$samlURI = config('xmlNameSpace.saml');

$xml_document = $entity->xml_file;
$isIdp = $entity->type == 'idp';

$dom = $this->createDOM($xml_document);
$xPath = $this->createXPath($dom);

$rootTag = $this->getRootTag($xPath);
$extensions = $this->getOrCreateExtensions($xPath, $dom, $rootTag, $mdURI);
$entityAttributes = $this->getOrCreateEntityAttributes($xPath, $dom, $extensions, $mdattrURI);
$attribute = $this->getOrCreateAttribute($xPath, $dom, $entityAttributes, $samlURI);

$attributeValue = $dom->createElementNS($samlURI, 'saml:AttributeValue', $this->value);
$attribute->appendChild($attributeValue);

$dom->normalize();

return $dom->saveXML();
}

public function delete(Entity $entity): void
{
$dom = $this->createDOM($entity->xml_file);
$xPath = $this->createXPath($dom);
$this->deleteByXpath($xPath);
}

public function deleteByXpath(DOMXPath $xPath): void
{
$xpathQuery = $this->buildXPathQuery();
$this->DeleteAllTags($xpathQuery, $xPath);
}

private function getRootTag(DOMXPath $xPath): \DOMNode
{
$rootTag = $xPath->query("//*[local-name()='EntityDescriptor']")->item(0);
if (! $rootTag) {
throw new \RuntimeException('Root tag EntityDescriptor not found');
}

return $rootTag;
}

private function getOrCreateAttribute(DOMXPath $xPath, \DOMDocument $dom, \DOMNode $entityAttributes, string $samlURI): \DOMNode
{
$attribute = $xPath->query('//mdattr:EntityAttributes/saml:Attribute', $entityAttributes);
if ($attribute->length === 0) {
$attribute = $dom->createElementNS($samlURI, 'saml:Attribute');
$attribute->setAttribute('NameFormat', 'urn:oasis:names:tc:SAML:2.0:attrname-format:uri');
$attribute->setAttribute('Name', 'http://macedir.org/entity-category');
$entityAttributes->appendChild($attribute);
} else {
$attribute = $attribute->item(0);
}

return $attribute;
}

private function getOrCreateEntityAttributes(DOMXPath $xPath, \DOMDocument $dom, \DOMNode $extensions, string $mdattrURI): \DOMNode
{
$entityAttributes = $xPath->query('//mdattr:EntityAttributes');
if ($entityAttributes->length === 0) {
$entityAttributes = $dom->createElementNS($mdattrURI, 'mdattr:EntityAttributes');
$extensions->appendChild($entityAttributes);
} else {
$entityAttributes = $entityAttributes->item(0);
}

return $entityAttributes;
}

private function getOrCreateExtensions(DOMXPath $xPath, \DOMDocument $dom, \DOMNode $rootTag, string $mdURI): \DOMNode
{
$extensions = $xPath->query('//md:Extensions');
if ($extensions->length === 0) {
$extensions = $dom->createElementNS($mdURI, 'md:Extensions');
$rootTag->appendChild($extensions);
} else {
$extensions = $extensions->item(0);
}

return $extensions;
}

private function buildXPathQuery(): string
{
return "//saml:AttributeValue[text()='$this->value']";
}
}
6 changes: 3 additions & 3 deletions app/Services/RsTagService.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public function create(Entity $entity): false|string
$dom = $this->createDOM($xml_document);
$xPath = $this->createXPath($dom);

$rootTag = $this->getRootTag($xPath, $dom);
$rootTag = $this->getRootTag($xPath);
$extensions = $this->getOrCreateExtensions($xPath, $dom, $rootTag, $mdURI);
$entityAttributes = $this->getOrCreateEntityAttributes($xPath, $dom, $extensions, $mdattrURI);
$attribute = $this->getOrCreateAttribute($xPath, $dom, $entityAttributes, $samlURI, $isIdp);

$attributeValue = $dom->createElementNS($samlURI, 'saml:AttributeValue', 'http://refeds.org/category/research-and-scholarship');
$attributeValue = $dom->createElementNS($samlURI, 'saml:AttributeValue', $this->value);
$attribute->appendChild($attributeValue);

$dom->normalize();
Expand Down Expand Up @@ -92,7 +92,7 @@ private function buildXPathQuery(): string
return "//saml:AttributeValue[text()='$this->value']";
}

private function getRootTag(\DOMXPath $xPath, \DOMDocument $dom): \DOMNode
private function getRootTag(\DOMXPath $xPath): \DOMNode
{
$rootTag = $xPath->query("//*[local-name()='EntityDescriptor']")->item(0);
if (! $rootTag) {
Expand Down

0 comments on commit f1bc624

Please sign in to comment.