Skip to content

Commit ae2ce00

Browse files
authored
Merge pull request #13 from ioigoume/symfony-http-client
Replace legacy HTTP/Utils fetch with Symfony HTTP Client
2 parents f3d6579 + afaa39e commit ae2ce00

File tree

2 files changed

+52
-18
lines changed

2 files changed

+52
-18
lines changed

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@
4444
"simplesamlphp/simplesamlphp-module-ldap": "~1.2",
4545
"simplesamlphp/xml-cas-module-slate": "~1.1.0",
4646
"simplesamlphp/xml-cas": "^v2.2.0",
47-
"simplesamlphp/xml-common": "~2.4.0",
48-
"symfony/http-foundation": "~7.4.0"
47+
"simplesamlphp/xml-common": "~2.4",
48+
"symfony/http-foundation": "~7.4",
49+
"symfony/http-client": "~7.4",
50+
"symfony/http-client-contracts": "^3.5"
4951
},
5052
"require-dev": {
5153
"simplesamlphp/simplesamlphp-test-framework": "^1.10",

src/Auth/Source/CAS.php

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
use SimpleSAML\Utils;
2222
use SimpleSAML\XML\Chunk;
2323
use SimpleSAML\XML\DOMDocumentFactory;
24+
use Symfony\Component\HttpClient\HttpClient;
25+
use Symfony\Contracts\HttpClient\HttpClientInterface;
2426

2527
use function array_merge_recursive;
2628
use function preg_split;
@@ -76,11 +78,15 @@ class CAS extends Auth\Source
7678
private bool $useSlate;
7779

7880
/**
79-
* HTTP utility class for making requests and handling redirects.
80-
* @var \SimpleSAML\Utils\HTTP
81+
* HTTP utilities instance for handling redirects and URLs.
8182
*/
8283
private Utils\HTTP $httpUtils;
8384

85+
/**
86+
* Symfony HTTP client for CAS requests.
87+
*/
88+
private HttpClientInterface $httpClient;
89+
8490

8591
/**
8692
* Constructor for this authentication source.
@@ -116,11 +122,30 @@ public function __construct(array $info, array $config)
116122
}
117123

118124

125+
/**
126+
* Initialize HttpClient instance
127+
*
128+
* @param \Symfony\Contracts\HttpClient\HttpClientInterface|null $httpClient Optional HTTP client instance to use
129+
*/
130+
protected function initHttpClient(?HttpClientInterface $httpClient = null): void
131+
{
132+
if ($httpClient !== null) {
133+
$this->httpClient = $httpClient;
134+
} else {
135+
$this->httpClient = $this->httpClient ?? HttpClient::create();
136+
}
137+
}
138+
139+
119140
/**
120141
* Initialize HTTP utilities instance
121142
*
122143
* @param \SimpleSAML\Utils\HTTP|null $httpUtils Optional HTTP utilities instance to use
123144
* @return void
145+
* @deprecated This helper is kept only for the legacy authenticate(array &$state): void
146+
* flow. Once the Request-based authenticate(Request, array &$state): ?Response
147+
* API is active in SimpleSAMLphp, this method will be removed and HTTP
148+
* handling should be done via Symfony responses instead.
124149
*/
125150
protected function initHttpUtils(?Utils\HTTP $httpUtils = null): void
126151
{
@@ -142,14 +167,16 @@ protected function initHttpUtils(?Utils\HTTP $httpUtils = null): void
142167
*/
143168
private function casValidate(string $ticket, string $service): array
144169
{
145-
$this->initHttpUtils();
146-
$url = $this->httpUtils->addURLParameters($this->casConfig['validate'], [
147-
'ticket' => $ticket,
148-
'service' => $service,
170+
$this->initHttpClient();
171+
172+
$response = $this->httpClient->request('GET', $this->casConfig['validate'], [
173+
'query' => [
174+
'ticket' => $ticket,
175+
'service' => $service,
176+
],
149177
]);
150178

151-
/** @var string $result */
152-
$result = $this->httpUtils->fetch($url);
179+
$result = $response->getContent();
153180

154181
/** @var list<string> $res */
155182
$res = preg_split("/\r?\n/", $result) ?: [];
@@ -172,19 +199,24 @@ private function casValidate(string $ticket, string $service): array
172199
*/
173200
private function casServiceValidate(string $ticket, string $service): array
174201
{
175-
$this->initHttpUtils();
176-
$url = $this->httpUtils->addURLParameters(
177-
$this->casConfig['serviceValidate'],
178-
[
179-
'ticket' => $ticket,
202+
$this->initHttpClient();
203+
204+
$response = $this->httpClient->request('GET', $this->casConfig['serviceValidate'], [
205+
'query' => [
206+
'ticket' => $ticket,
180207
'service' => $service,
181208
],
182-
);
183-
$result = $this->httpUtils->fetch($url);
209+
]);
210+
211+
$result = $response->getContent();
184212

185213
/** @var string $result */
186214
$dom = DOMDocumentFactory::fromString($result);
187215

216+
// In practice that `if (...) return [];` branch is unreachable with the current behavior.
217+
// `DOMDocumentFactory::fromString()`
218+
// PHPStan still flags / cares about it because it only sees
219+
// and has no way to know `null` won’t actually occur here. `DOMElement|null`
188220
if ($dom->documentElement === null) {
189221
return [];
190222
}
@@ -228,7 +260,7 @@ private function casServiceValidate(string $ticket, string $service): array
228260

229261

230262
/**
231-
* Main validation method, redirects to correct method
263+
* Main validation method, redirects to the correct method
232264
* (keeps finalStep clean)
233265
*
234266
* @param string $ticket

0 commit comments

Comments
 (0)