Passed
Push — master ( f2daf3...fc29ab )
by Tim
03:13
created

Cas20::setProxyGrantingTicketIOU()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 *    simpleSAMLphp-casserver is a CAS 1.0 and 2.0 compliant CAS server in the form of a simpleSAMLphp module
5
 *
6
 *    Copyright (C) 2013  Bjorn R. Jensen
7
 *
8
 *    This library is free software; you can redistribute it and/or
9
 *    modify it under the terms of the GNU Lesser General Public
10
 *    License as published by the Free Software Foundation; either
11
 *    version 2.1 of the License, or (at your option) any later version.
12
 *
13
 *    This library is distributed in the hope that it will be useful,
14
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 *    Lesser General Public License for more details.
17
 *
18
 *    You should have received a copy of the GNU Lesser General Public
19
 *    License along with this library; if not, write to the Free Software
20
 *    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21
 *
22
 */
23
24
namespace SimpleSAML\Module\casserver\Cas\Protocol;
25
26
class Cas20
27
{
28
    private $sendAttributes;
29
    private $base64EncodeAttributes;
30
    private $base64IndicatorAttribute;
31
    private $attributes = [];
32
    private $proxyGrantingTicketIOU = null;
33
34
    public function __construct($config)
35
    {
36
        $this->sendAttributes = $config->getValue('attributes', false);
37
        $this->base64EncodeAttributes = $config->getValue('base64attributes', false);
38
        $this->base64IndicatorAttribute = $config->getValue('base64_attributes_indicator_attribute', null);
39
    }
40
41
    public function setAttributes($attributes)
42
    {
43
        $this->attributes = $attributes;
44
    }
45
46
    public function getAttributes()
47
    {
48
        return $this->attributes;
49
    }
50
51
    public function setProxyGrantingTicketIOU($proxyGrantingTicketIOU)
52
    {
53
        $this->proxyGrantingTicketIOU = $proxyGrantingTicketIOU;
54
    }
55
56
    public function getProxyGrantingTicketIOU()
57
    {
58
        return $this->proxyGrantingTicketIOU;
59
    }
60
61
    public function getValidateSuccessResponse($username)
62
    {
63
        $xmlDocument = new DOMDocument("1.0");
0 ignored issues
show
Bug introduced by
The type SimpleSAML\Module\casser...as\Protocol\DOMDocument was not found. Did you mean DOMDocument? If so, make sure to prefix the type with \.
Loading history...
64
65
        $root = $xmlDocument->createElement("cas:serviceResponse");
66
        $root->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:cas', 'http://www.yale.edu/tp/cas');
67
68
        $usernameNode = $xmlDocument->createTextNode($username);
69
        $casUser = $xmlDocument->createElement('cas:user');
70
        $casUser->appendChild($usernameNode);
71
72
        $casSuccess = $xmlDocument->createElement('cas:authenticationSuccess');
73
        $casSuccess->appendChild($casUser);
74
75
        if (is_string($this->proxyGrantingTicketIOU)) {
76
            $iouNode = $xmlDocument->createTextNode($this->proxyGrantingTicketIOU);
77
            $iouElement = $xmlDocument->createElement("cas:proxyGrantingTicket");
78
            $iouElement->appendChild($iouNode);
79
            $casSuccess->appendChild($iouElement);
80
        }
81
82
        if ($this->sendAttributes && count($this->attributes) > 0) {
83
            $casAttributes = $xmlDocument->createElement('cas:attributes');
84
85
            foreach ($this->attributes as $name => $values) {
86
                foreach ($values as $value) {
87
                    $casAttributes->appendChild(
88
                        $this->generateCas20Attribute($xmlDocument, str_replace(':', '_', $name), $value)
89
                    );
90
                }
91
            }
92
93
            if (!is_null($this->base64IndicatorAttribute)) {
94
                $casAttributes->appendChild(
95
                    $this->generateCas20Attribute(
96
                        $xmlDocument,
97
                        $this->base64IndicatorAttribute,
98
                        $this->base64EncodeAttributes ? "true" : "false"
99
                    )
100
                );
101
            }
102
103
            $casSuccess->appendChild($casAttributes);
104
        }
105
106
        $root->appendChild($casSuccess);
107
        $xmlDocument->appendChild($root);
108
109
        return $this->workAroundForBuggyJasigXmlParser($xmlDocument->saveXML());
110
    }
111
112
    public function getValidateFailureResponse($errorCode, $explanation)
113
    {
114
        $xmlDocument = new DOMDocument("1.0");
115
116
        $root = $xmlDocument->createElement("cas:serviceResponse");
117
        $root->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:cas', 'http://www.yale.edu/tp/cas');
118
119
        $casFailureCode = $xmlDocument->createAttribute('code');
120
        $casFailureCode->value = $errorCode;
121
122
        $casFailureNode = $xmlDocument->createTextNode($explanation);
123
        $casFailure = $xmlDocument->createElement('cas:authenticationFailure');
124
        $casFailure->appendChild($casFailureNode);
125
        $casFailure->appendChild($casFailureCode);
126
127
        $root->appendChild($casFailure);
128
129
        $xmlDocument->appendChild($root);
130
131
        return $this->workAroundForBuggyJasigXmlParser($xmlDocument->saveXML());
132
    }
133
134
    public function getProxySuccessResponse($proxyTicketId)
135
    {
136
        $xmlDocument = new DOMDocument("1.0");
137
138
        $root = $xmlDocument->createElement("cas:serviceResponse");
139
        $root->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:cas', 'http://www.yale.edu/tp/cas');
140
141
        $casProxyTicketIdNode = $xmlDocument->createTextNode($proxyTicketId);
142
        $casProxyTicketId = $xmlDocument->createElement('cas:proxyTicket');
143
        $casProxyTicketId->appendChild($casProxyTicketIdNode);
144
145
        $casProxySuccess = $xmlDocument->createElement('cas:proxySuccess');
146
        $casProxySuccess->appendChild($casProxyTicketId);
147
148
        $root->appendChild($casProxySuccess);
149
        $xmlDocument->appendChild($root);
150
151
        return $this->workAroundForBuggyJasigXmlParser($xmlDocument->saveXML());
152
    }
153
154
    public function getProxyFailureResponse($errorCode, $explanation)
155
    {
156
        $xmlDocument = new DOMDocument("1.0");
157
158
        $root = $xmlDocument->createElement("cas:serviceResponse");
159
        $root->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:cas', 'http://www.yale.edu/tp/cas');
160
161
        $casFailureCode = $xmlDocument->createAttribute('code');
162
        $casFailureCode->value = $errorCode;
163
164
        $casFailureNode = $xmlDocument->createTextNode($explanation);
165
        $casFailure = $xmlDocument->createElement('cas:proxyFailure');
166
        $casFailure->appendChild($casFailureNode);
167
        $casFailure->appendChild($casFailureCode);
168
169
        $root->appendChild($casFailure);
170
171
        $xmlDocument->appendChild($root);
172
173
        return $this->workAroundForBuggyJasigXmlParser($xmlDocument->saveXML());
174
    }
175
176
    private function workAroundForBuggyJasigXmlParser($xmlString)
177
    {
178
        // when will people stop hand coding xml handling....?
179
        return str_replace('><', '>' . PHP_EOL . '<', str_replace(PHP_EOL, '', $xmlString));
180
    }
181
182
    private function generateCas20Attribute($xmlDocument, $attributeName, $attributeValue)
183
    {
184
        $attributeValueNode = $xmlDocument->createTextNode($this->base64EncodeAttributes ?
185
            base64_encode($attributeValue) : $attributeValue);
186
187
        $attributeElement = $xmlDocument->createElement('cas:' . $attributeName);
188
189
        $attributeElement->appendChild($attributeValueNode);
190
191
        return $attributeElement;
192
    }
193
}
194