Total Complexity | 20 |
Total Lines | 193 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
53 | class Cas20 |
||
54 | { |
||
55 | /** @var bool $sendAttributes */ |
||
56 | private bool $sendAttributes; |
||
57 | |||
58 | /** @var bool $base64EncodeAttributes */ |
||
59 | private bool $base64EncodeAttributes; |
||
60 | |||
61 | /** @var string|null $base64IndicatorAttribute */ |
||
62 | private ?string $base64IndicatorAttribute; |
||
63 | |||
64 | /** @var array $attributes */ |
||
65 | private array $attributes = []; |
||
66 | |||
67 | /** @var string|null $proxyGrantingTicketIOU */ |
||
68 | private ?string $proxyGrantingTicketIOU = null; |
||
69 | |||
70 | |||
71 | /** |
||
72 | * @param \SimpleSAML\Configuration $config |
||
73 | */ |
||
74 | public function __construct(Configuration $config) |
||
75 | { |
||
76 | $this->sendAttributes = $config->getOptionalValue('attributes', false); |
||
77 | $this->base64EncodeAttributes = $config->getOptionalValue('base64attributes', false); |
||
78 | $this->base64IndicatorAttribute = $config->getOptionalValue('base64_attributes_indicator_attribute', null); |
||
79 | } |
||
80 | |||
81 | |||
82 | /** |
||
83 | * @param array $attributes |
||
84 | */ |
||
85 | public function setAttributes(array $attributes): void |
||
86 | { |
||
87 | $this->attributes = $attributes; |
||
88 | } |
||
89 | |||
90 | |||
91 | /** |
||
92 | * @return array |
||
93 | */ |
||
94 | public function getAttributes(): array |
||
95 | { |
||
96 | return $this->attributes; |
||
97 | } |
||
98 | |||
99 | |||
100 | /** |
||
101 | * @param string $proxyGrantingTicketIOU |
||
102 | */ |
||
103 | public function setProxyGrantingTicketIOU(string $proxyGrantingTicketIOU): void |
||
104 | { |
||
105 | $this->proxyGrantingTicketIOU = $proxyGrantingTicketIOU; |
||
106 | } |
||
107 | |||
108 | |||
109 | /** |
||
110 | * @return string|null |
||
111 | */ |
||
112 | public function getProxyGrantingTicketIOU(): ?string |
||
115 | } |
||
116 | |||
117 | |||
118 | /** |
||
119 | * @param string $username |
||
120 | * @return \SimpleSAML\CAS\XML\cas\ServiceResponse |
||
121 | */ |
||
122 | public function getValidateSuccessResponse(string $username): ServiceResponse |
||
123 | { |
||
124 | $user = new User($username); |
||
125 | |||
126 | $proxyGrantingTicket = null; |
||
127 | if (is_string($this->proxyGrantingTicketIOU)) { |
||
128 | $proxyGrantingTicket = new ProxyGrantingTicket($this->proxyGrantingTicketIOU); |
||
129 | } |
||
130 | |||
131 | $attr = []; |
||
132 | if ($this->sendAttributes && count($this->attributes) > 0) { |
||
133 | foreach ($this->attributes as $name => $values) { |
||
134 | // Fix the most common cause of invalid XML elements |
||
135 | $_name = str_replace(':', '_', $name); |
||
136 | if ($this->isValidXmlName($_name) === true) { |
||
137 | foreach ($values as $value) { |
||
138 | $attr[] = $this->generateCas20Attribute($_name, $value); |
||
139 | } |
||
140 | } else { |
||
141 | Logger::warning("DOMException creating attribute '$_name'. Continuing without attribute'"); |
||
142 | } |
||
143 | } |
||
144 | |||
145 | if (!is_null($this->base64IndicatorAttribute)) { |
||
146 | $attr[] = $this->generateCas20Attribute( |
||
147 | $this->base64IndicatorAttribute, |
||
148 | $this->base64EncodeAttributes ? "true" : "false", |
||
149 | ); |
||
150 | } |
||
151 | } |
||
152 | |||
153 | $attributes = new Attributes( |
||
154 | new AuthenticationDate(new DateTimeImmutable('now')), |
||
155 | new LongTermAuthenticationRequestTokenUsed('true'), |
||
156 | new IsFromNewLogin('true'), |
||
157 | $attr, |
||
158 | ); |
||
159 | |||
160 | $authenticationSuccess = new AuthenticationSuccess($user, $attributes, $proxyGrantingTicket); |
||
161 | $serviceResponse = new ServiceResponse($authenticationSuccess); |
||
162 | |||
163 | return $serviceResponse; |
||
164 | } |
||
165 | |||
166 | |||
167 | /** |
||
168 | * @param string $errorCode |
||
169 | * @param string $explanation |
||
170 | * @return \SimpleSAML\CAS\XML\cas\ServiceResponse |
||
171 | */ |
||
172 | public function getValidateFailureResponse(string $errorCode, string $explanation): ServiceResponse |
||
173 | { |
||
174 | $authenticationFailure = new AuthenticationFailure($explanation, $errorCode); |
||
175 | $serviceResponse = new ServiceResponse($authenticationFailure); |
||
176 | |||
177 | return $serviceResponse; |
||
178 | } |
||
179 | |||
180 | |||
181 | /** |
||
182 | * @param string $proxyTicketId |
||
183 | * @return \SimpleSAML\CAS\XML\cas\ServiceResponse |
||
184 | */ |
||
185 | public function getProxySuccessResponse(string $proxyTicketId): ServiceResponse |
||
192 | } |
||
193 | |||
194 | |||
195 | /** |
||
196 | * @param string $errorCode |
||
197 | * @param string $explanation |
||
198 | * @return \SimpleSAML\CAS\XML\cas\ServiceResponse |
||
199 | */ |
||
200 | public function getProxyFailureResponse(string $errorCode, string $explanation): ServiceResponse |
||
201 | { |
||
202 | $proxyFailure = new ProxyFailure($explanation, $errorCode); |
||
203 | $serviceResponse = new ServiceResponse($proxyFailure); |
||
204 | |||
205 | return $serviceResponse; |
||
206 | } |
||
207 | |||
208 | |||
209 | /** |
||
210 | * @param string $attributeName |
||
211 | * @param string $attributeValue |
||
212 | * @return \SimpleSAML\XML\Chunk |
||
213 | */ |
||
214 | private function generateCas20Attribute( |
||
215 | string $attributeName, |
||
216 | string $attributeValue, |
||
217 | ): Chunk { |
||
218 | $xmlDocument = DOMDocumentFactory::create(); |
||
219 | |||
220 | $attributeValue = $this->base64EncodeAttributes ? base64_encode($attributeValue) : $attributeValue; |
||
221 | $attributeElement = $xmlDocument->createElementNS(Attributes::NS, 'cas:' . $attributeName, $attributeValue); |
||
222 | |||
223 | return new Chunk($attributeElement); |
||
224 | } |
||
225 | |||
226 | |||
227 | /** |
||
228 | * XML element names have a lot of rules and not every SAML attribute name can be converted. |
||
229 | * Ref: https://www.w3.org/TR/REC-xml/#NT-NameChar |
||
230 | * https://stackoverflow.com/q/2519845/54396 |
||
231 | * must only start with letter or underscore |
||
232 | * cannot start with 'xml' (or maybe it can - stackoverflow commenters don't agree) |
||
233 | * cannot contain a ':' since those are for namespaces |
||
234 | * cannot contains space |
||
235 | * can only contain letters, digits, hyphens, underscores, and periods |
||
236 | * @param string $name The attribute name to be used as an element |
||
237 | * @return bool true if $name would make a valid xml element. |
||
238 | */ |
||
239 | private function isValidXmlName(string $name): bool |
||
246 | } |
||
247 | } |
||
248 |