ARP::getXML()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 13
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 18
rs 9.8333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\metarefresh;
6
7
/*
8
 * @package SimpleSAMLphp
9
 */
10
class ARP
11
{
12
    /** @var array */
13
    private array $metadata;
14
15
    /** @var array */
16
    private array $attributes = [];
17
18
    /** @var string */
19
    private string $prefix;
20
21
    /** @var string */
22
    private string $suffix;
23
24
25
    /**
26
     * Constructor
27
     *
28
     * @param array $metadata
29
     * @param string|null $attributemap_filename
30
     * @param string $prefix
31
     * @param string $suffix
32
     */
33
    public function __construct(
34
        array $metadata,
35
        ?string $attributemap_filename = null,
36
        ?string $prefix = '',
37
        ?string $suffix = '',
38
    ) {
39
        $this->metadata = $metadata;
40
        $this->prefix = $prefix;
41
        $this->suffix = $suffix;
42
43
        if (isset($attributemap_filename)) {
44
            $this->loadAttributeMap($attributemap_filename);
45
        }
46
    }
47
48
49
    /**
50
     * @param string $attributemap_filename
51
     *
52
     */
53
    private function loadAttributeMap(string $attributemap_filename): void
54
    {
55
        $config = \SimpleSAML\Configuration::getInstance();
56
57
        include($config->getPathValue('attributemap', 'attributemap/') . $attributemap_filename . '.php');
58
59
        // Note that $attributemap is defined in the included attributemap-file!
60
        // @phpstan-ignore variable.undefined
61
        $this->attributes = $attributemap;
62
    }
63
64
65
    /**
66
     * @param string $name
67
     *
68
     * @return string
69
     */
70
    private function surround(string $name): string
71
    {
72
        $ret = '';
73
        if (!empty($this->prefix)) {
74
            $ret .= $this->prefix;
75
        }
76
        $ret .= $name;
77
        if (!empty($this->suffix)) {
78
            $ret .= $this->suffix;
79
        }
80
        return $ret;
81
    }
82
83
84
    /**
85
     * @param string $name
86
     *
87
     * @return string
88
     */
89
    private function getAttributeID(string $name): string
90
    {
91
        if (empty($this->attributes)) {
92
            return $this->surround($name);
93
        }
94
        if (array_key_exists($name, $this->attributes)) {
95
            return $this->surround($this->attributes[$name]);
96
        }
97
        return $this->surround($name);
98
    }
99
100
101
    /**
102
     * @return string
103
     */
104
    public function getXML(): string
105
    {
106
        $xml = <<<MSG
107
        <?xml version="1.0" encoding="UTF-8"?>
108
        <AttributeFilterPolicyGroup id="urn:mace:funet.fi:haka:kalmar" xmlns="urn:mace:shibboleth:2.0:afp"
109
    xmlns:basic="urn:mace:shibboleth:2.0:afp:mf:basic" xmlns:saml="urn:mace:shibboleth:2.0:afp:mf:saml"
110
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
111
    xsi:schemaLocation="urn:mace:shibboleth:2.0:afp classpath:/schema/shibboleth-2.0-afp.xsd
112
                        urn:mace:shibboleth:2.0:afp:mf:basic classpath:/schema/shibboleth-2.0-afp-mf-basic.xsd
113
                        urn:mace:shibboleth:2.0:afp:mf:saml classpath:/schema/shibboleth-2.0-afp-mf-saml.xsd">
114
MSG;
115
116
        foreach ($this->metadata as $metadata) {
117
            $xml .= $this->getEntryXML($metadata['metadata']);
118
        }
119
120
        $xml .= '</AttributeFilterPolicyGroup>';
121
        return $xml;
122
    }
123
124
125
    /**
126
     * @param array $entry
127
     *
128
     * @return string
129
     */
130
    private function getEntryXML(array $entry): string
131
    {
132
        $entityid = $entry['entityid'];
133
        return '    <AttributeFilterPolicy id="' . $entityid .
134
            '"><PolicyRequirementRule xsi:type="basic:AttributeRequesterString" value="' . $entityid .
135
            '" />' . $this->getEntryXMLcontent($entry) . '</AttributeFilterPolicy>';
136
    }
137
138
139
    /**
140
     * @param array $entry
141
     *
142
     * @return string
143
     */
144
    private function getEntryXMLcontent(array $entry): string
145
    {
146
        if (!array_key_exists('attributes', $entry)) {
147
            return '';
148
        }
149
150
        $ret = '';
151
        foreach ($entry['attributes'] as $a) {
152
            $ret .= '            <AttributeRule attributeID="' . $this->getAttributeID($a) .
153
                '"><PermitValueRule xsi:type="basic:ANY" /></AttributeRule>';
154
        }
155
        return $ret;
156
    }
157
}
158