-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathCas20.php
More file actions
239 lines (198 loc) · 7.5 KB
/
Cas20.php
File metadata and controls
239 lines (198 loc) · 7.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
/*
* simpleSAMLphp-casserver is a CAS 1.0 and 2.0 compliant CAS server in the form of a simpleSAMLphp module
*
* Copyright (C) 2013 Bjorn R. Jensen
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
declare(strict_types=1);
namespace SimpleSAML\Module\casserver\Cas\Protocol;
use Beste\Clock\LocalizedClock;
use DateTimeZone;
use SimpleSAML\Assert\AssertionFailedException;
use SimpleSAML\CAS\Type\CodeValue;
use SimpleSAML\CAS\XML\Attributes;
use SimpleSAML\CAS\XML\AuthenticationDate;
use SimpleSAML\CAS\XML\AuthenticationFailure;
use SimpleSAML\CAS\XML\AuthenticationSuccess;
use SimpleSAML\CAS\XML\IsFromNewLogin;
use SimpleSAML\CAS\XML\LongTermAuthenticationRequestTokenUsed;
use SimpleSAML\CAS\XML\ProxyFailure;
use SimpleSAML\CAS\XML\ProxyGrantingTicket;
use SimpleSAML\CAS\XML\ProxySuccess;
use SimpleSAML\CAS\XML\ProxyTicket;
use SimpleSAML\CAS\XML\ServiceResponse;
use SimpleSAML\CAS\XML\User;
use SimpleSAML\Configuration;
use SimpleSAML\Logger;
use SimpleSAML\XML\Assert\Assert;
use SimpleSAML\XML\Chunk;
use SimpleSAML\XML\DOMDocumentFactory;
use SimpleSAML\XMLSchema\Type\BooleanValue;
use SimpleSAML\XMLSchema\Type\DateTimeValue;
use SimpleSAML\XMLSchema\Type\StringValue;
use function base64_encode;
use function count;
use function is_null;
use function is_string;
use function str_replace;
class Cas20
{
/** @var bool $sendAttributes */
private bool $sendAttributes;
/** @var bool $base64EncodeAttributes */
private bool $base64EncodeAttributes;
/** @var string|null $base64IndicatorAttribute */
private ?string $base64IndicatorAttribute;
/** @var array $attributes */
private array $attributes = [];
/** @var string|null $proxyGrantingTicketIOU */
private ?string $proxyGrantingTicketIOU = null;
/**
* @param \SimpleSAML\Configuration $config
*/
public function __construct(Configuration $config)
{
$this->sendAttributes = $config->getOptionalValue('attributes', false);
$this->base64EncodeAttributes = $config->getOptionalValue('base64attributes', false);
$this->base64IndicatorAttribute = $config->getOptionalValue('base64_attributes_indicator_attribute', null);
}
/**
* @param array $attributes
*/
public function setAttributes(array $attributes): void
{
$this->attributes = $attributes;
}
/**
* @return array
*/
public function getAttributes(): array
{
return $this->attributes;
}
/**
* @param string $proxyGrantingTicketIOU
*/
public function setProxyGrantingTicketIOU(string $proxyGrantingTicketIOU): void
{
$this->proxyGrantingTicketIOU = $proxyGrantingTicketIOU;
}
/**
* @return string|null
*/
public function getProxyGrantingTicketIOU(): ?string
{
return $this->proxyGrantingTicketIOU;
}
/**
* @param string $username
* @return \SimpleSAML\CAS\XML\ServiceResponse
*/
public function getValidateSuccessResponse(string $username): ServiceResponse
{
$user = new User(StringValue::fromString($username));
$proxyGrantingTicket = null;
if (is_string($this->proxyGrantingTicketIOU)) {
$proxyGrantingTicket = new ProxyGrantingTicket(StringValue::fromString($this->proxyGrantingTicketIOU));
}
$attr = [];
if ($this->sendAttributes && count($this->attributes) > 0) {
foreach ($this->attributes as $name => $values) {
// Fix the most common cause of invalid XML elements
$_name = str_replace(':', '_', $name);
try {
Assert::validNCName($_name);
foreach ($values as $value) {
$attr[] = $this->generateCas20Attribute($_name, $value);
}
} catch (AssertionFailedException) {
Logger::warning("DOMException creating attribute '$_name'. Continuing without attribute'");
}
}
if (!is_null($this->base64IndicatorAttribute)) {
$attr[] = $this->generateCas20Attribute(
$this->base64IndicatorAttribute,
$this->base64EncodeAttributes ? "true" : "false",
);
}
}
$systemClock = LocalizedClock::in(new DateTimeZone('Z'));
$attributes = new Attributes(
new AuthenticationDate(DateTimeValue::now($systemClock)),
new LongTermAuthenticationRequestTokenUsed(BooleanValue::fromBoolean(true)),
new IsFromNewLogin(BooleanValue::fromBoolean(true)),
$attr,
);
$authenticationSuccess = new AuthenticationSuccess($user, $attributes, $proxyGrantingTicket);
$serviceResponse = new ServiceResponse($authenticationSuccess);
return $serviceResponse;
}
/**
* @param string $errorCode
* @param string $explanation
* @return \SimpleSAML\CAS\XML\ServiceResponse
*/
public function getValidateFailureResponse(string $errorCode, string $explanation): ServiceResponse
{
$authenticationFailure = new AuthenticationFailure(
StringValue::fromString($explanation),
CodeValue::fromString($errorCode),
);
$serviceResponse = new ServiceResponse($authenticationFailure);
return $serviceResponse;
}
/**
* @param string $proxyTicketId
* @return \SimpleSAML\CAS\XML\ServiceResponse
*/
public function getProxySuccessResponse(string $proxyTicketId): ServiceResponse
{
$proxyTicket = new ProxyTicket(StringValue::fromString($proxyTicketId));
$proxySuccess = new ProxySuccess($proxyTicket);
$serviceResponse = new ServiceResponse($proxySuccess);
return $serviceResponse;
}
/**
* @param string $errorCode
* @param string $explanation
* @return \SimpleSAML\CAS\XML\ServiceResponse
*/
public function getProxyFailureResponse(string $errorCode, string $explanation): ServiceResponse
{
$proxyFailure = new ProxyFailure(
StringValue::fromString($explanation),
CodeValue::fromString($errorCode),
);
$serviceResponse = new ServiceResponse($proxyFailure);
return $serviceResponse;
}
/**
* @param string $attributeName
* @param string $attributeValue
* @return \SimpleSAML\XML\Chunk
*/
private function generateCas20Attribute(
string $attributeName,
string $attributeValue,
): Chunk {
$xmlDocument = DOMDocumentFactory::create();
$attributeValue = $this->base64EncodeAttributes ? base64_encode($attributeValue) : $attributeValue;
$attributeElement = $xmlDocument->createElementNS(Attributes::NS, 'cas:' . $attributeName, $attributeValue);
return new Chunk($attributeElement);
}
}