Skip to content

Commit ba5968a

Browse files
committed
Refactor fetchURL return type to custom Response class
1 parent 69bd300 commit ba5968a

File tree

2 files changed

+49
-41
lines changed

2 files changed

+49
-41
lines changed

src/OpenIDConnectClient.php

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ protected function getWellKnownConfigValue(string $param, $default = null)
753753
if (count($this->wellKnownConfigParameters) > 0) {
754754
$well_known_config_url .= '?' . http_build_query($this->wellKnownConfigParameters) ;
755755
}
756-
$this->wellKnown = json_decode($this->fetchURL($well_known_config_url), false);
756+
$this->wellKnown = json_decode($this->fetchURL($well_known_config_url)->getBody(), false);
757757
}
758758

759759
$value = $this->wellKnown->{$param} ?? false;
@@ -953,7 +953,7 @@ public function requestClientCredentialsToken()
953953
// Convert token params to string format
954954
$post_params = http_build_query($post_data, '', '&', $this->encType);
955955

956-
return json_decode($this->fetchURL($token_endpoint, $post_params, $headers), false);
956+
return json_decode($this->fetchURL($token_endpoint, $post_params, $headers)->getBody(), false);
957957
}
958958

959959
/**
@@ -993,7 +993,7 @@ public function requestResourceOwnerToken(bool $bClientAuth = false)
993993
// Convert token params to string format
994994
$post_params = http_build_query($post_data, '', '&', $this->encType);
995995

996-
return json_decode($this->fetchURL($token_endpoint, $post_params, $headers), false);
996+
return json_decode($this->fetchURL($token_endpoint, $post_params, $headers)->getBody(), false);
997997
}
998998

999999

@@ -1068,7 +1068,7 @@ protected function requestTokens(string $code, array $headers = [])
10681068
$headers[] = $authorizationHeader;
10691069
}
10701070

1071-
$this->tokenResponse = json_decode($this->fetchURL($token_endpoint, $token_params, $headers), false);
1071+
$this->tokenResponse = json_decode($this->fetchURL($token_endpoint, $token_params, $headers)->getBody(), false);
10721072

10731073
return $this->tokenResponse;
10741074
}
@@ -1112,7 +1112,7 @@ public function requestTokenExchange(string $subjectToken, string $subjectTokenT
11121112
// Convert token params to string format
11131113
$post_params = http_build_query($post_data, null, '&', $this->encType);
11141114

1115-
return json_decode($this->fetchURL($token_endpoint, $post_params, $headers), false);
1115+
return json_decode($this->fetchURL($token_endpoint, $post_params, $headers)->getBody(), false);
11161116
}
11171117

11181118

@@ -1163,7 +1163,7 @@ public function refreshToken(string $refresh_token)
11631163
// Convert token params to string format
11641164
$token_params = http_build_query($token_params, '', '&', $this->encType);
11651165

1166-
$json = json_decode($this->fetchURL($token_endpoint, $token_params, $headers), false);
1166+
$json = json_decode($this->fetchURL($token_endpoint, $token_params, $headers)->getBody(), false);
11671167

11681168
if (isset($json->access_token)) {
11691169
$this->accessToken = $json->access_token;
@@ -1220,7 +1220,7 @@ public function verifyJWS(JWS $jws): bool
12201220
throw new OpenIDConnectClientException('Unable to verify signature due to no jwks_uri being defined');
12211221
}
12221222

1223-
$jwkSet = JWKSet::createFromJson($this->fetchURL($jwksUri));
1223+
$jwkSet = JWKSet::createFromJson($this->fetchURL($jwksUri)->getBody());
12241224

12251225
// Add additional JWKs
12261226
foreach ($this->additionalJwks as $additionalJwk) {
@@ -1329,9 +1329,10 @@ public function requestUserInfo(?string $attribute = null)
13291329
$headers = ["Authorization: Bearer $this->accessToken",
13301330
'Accept: application/json'];
13311331

1332+
13321333
$response = $this->fetchURL($user_info_endpoint, null, $headers);
1333-
if ($this->getResponseCode() !== 200) {
1334-
throw new OpenIDConnectClientException('The communication to retrieve user data has failed with status code '.$this->getResponseCode());
1334+
if ($response->getStatus() !== 200) {
1335+
throw new OpenIDConnectClientException('The communication to retrieve user data has failed with status code '.$response->getStatus());
13351336
}
13361337

13371338
// When we receive application/jwt, the UserInfo Response is signed and/or encrypted.
@@ -1346,16 +1347,16 @@ public function requestUserInfo(?string $attribute = null)
13461347
*/
13471348

13481349
// Extract the content type from the response (remove optional charset)
1349-
$contentType = explode(";", $this->getResponseContentType())[0];
1350+
$contentType = explode(";", $response->getContentType())[0];
13501351

13511352
if ($contentType === 'application/jwt') {
13521353

1353-
$jws = $this->jwsSerializerManager->unserialize($response);
1354+
$jws = $this->jwsSerializerManager->unserialize($response->getBody());
13541355

13551356
if ($jws->getSignature(0)->hasProtectedHeaderParameter('enc')) {
13561357
// Handle JWE; Throw exception as JWE is not supported in this library
13571358
// @TODO: What should be done with the return value?
1358-
$this->handleJweResponse($response);
1359+
$this->handleJweResponse($response->getBody());
13591360
}
13601361

13611362
// Verify header
@@ -1384,7 +1385,7 @@ public function requestUserInfo(?string $attribute = null)
13841385
]
13851386
))->check((array) $claims, ['sub', 'aud', 'iss']);
13861387
} else {
1387-
$claims = json_decode($response);
1388+
$claims = json_decode($response->getBody());
13881389

13891390
/*
13901391
* The sub (subject) Claim MUST always be returned in the UserInfo Response.
@@ -1450,10 +1451,10 @@ public function getVerifiedClaims(?string $attribute = null)
14501451
* @param string $url
14511452
* @param string | null $post_body string If this is set the post type will be POST
14521453
* @param array $headers Extra headers to be sent with the request. Format as 'NameHeader: ValueHeader'
1453-
* @return bool|string
1454+
* @return Response
14541455
* @throws OpenIDConnectClientException
14551456
*/
1456-
protected function fetchURL(string $url, ?string $post_body = null, array $headers = [])
1457+
protected function fetchURL(string $url, ?string $post_body = null, array $headers = []): Response
14571458
{
14581459

14591460
// OK cool - then let's create a new cURL resource handle
@@ -1540,7 +1541,8 @@ protected function fetchURL(string $url, ?string $post_body = null, array $heade
15401541
// Close the cURL resource, and free system resources
15411542
curl_close($ch);
15421543

1543-
return $output;
1544+
1545+
return new Response($info['http_code'], $info['content_type'], $output);
15441546
}
15451547

15461548
/**
@@ -1707,9 +1709,7 @@ public function register()
17071709
'client_name' => $this->getClientName()
17081710
]);
17091711

1710-
$response = $this->fetchURL($registration_endpoint, json_encode($send_object));
1711-
1712-
$json_response = json_decode($response, false);
1712+
$json_response = json_decode($this->fetchURL($registration_endpoint, json_encode($send_object))->getBody(), false);
17131713

17141714
// Throw some errors if we encounter them
17151715
if ($json_response === false) {
@@ -1771,7 +1771,7 @@ public function introspectToken(string $token, string $token_type_hint = '', ?st
17711771

17721772
$post_params = http_build_query($post_data, '', '&');
17731773

1774-
return json_decode($this->fetchURL($introspection_endpoint, $post_params, $headers), false);
1774+
return json_decode($this->fetchURL($introspection_endpoint, $post_params, $headers)->getBody(), false);
17751775
}
17761776

17771777
/**
@@ -1802,7 +1802,7 @@ public function revokeToken(string $token, string $token_type_hint = '', ?string
18021802
$headers = ['Authorization: Basic ' . base64_encode(urlencode($clientId) . ':' . urlencode($clientSecret)),
18031803
'Accept: application/json'];
18041804

1805-
return json_decode($this->fetchURL($revocation_endpoint, $post_params, $headers), false);
1805+
return json_decode($this->fetchURL($revocation_endpoint, $post_params, $headers)->getBody(), false);
18061806
}
18071807

18081808
/**
@@ -2000,26 +2000,6 @@ protected function unsetCodeVerifier()
20002000
$this->unsetSessionKey('openid_connect_code_verifier');
20012001
}
20022002

2003-
/**
2004-
* Get the response code from last action/curl request.
2005-
*
2006-
* @return int
2007-
*/
2008-
public function getResponseCode(): int
2009-
{
2010-
return $this->responseCode;
2011-
}
2012-
2013-
/**
2014-
* Get the content type from last action/curl request.
2015-
*
2016-
* @return string|null
2017-
*/
2018-
public function getResponseContentType()
2019-
{
2020-
return $this->responseContentType;
2021-
}
2022-
20232003
/**
20242004
* Set timeout (seconds)
20252005
*

src/Response.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Jumbojett;
4+
5+
class Response
6+
{
7+
public function __construct(
8+
private readonly int $status,
9+
private readonly string $contentType,
10+
private readonly string $body,
11+
) {
12+
}
13+
14+
public function getStatus(): int
15+
{
16+
return $this->status;
17+
}
18+
19+
public function getContentType(): string
20+
{
21+
return $this->contentType;
22+
}
23+
24+
public function getBody(): string
25+
{
26+
return $this->body;
27+
}
28+
}

0 commit comments

Comments
 (0)