Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Joomla\Http\Exception\UnexpectedResponseException;
use Joomla\Http\Http;
use Joomla\Http\HttpFactory;
use Joomla\Http\Response;
use Joomla\Input\Input;
use Joomla\Uri\Uri;

Expand Down Expand Up @@ -79,6 +80,24 @@ public function __construct($options = [], ?Http $http = null, ?Input $input = n
$this->application = $application;
}

/**
* Tests if given response contains JSON header
*
* @param Response $response The response object
*
* @return boolean
*
*/
protected function isJsonResponse(Response $response)
{
foreach ($response->getHeader('Content-Type') as $type) {
if (str_starts_with($type, 'application/json')) {
return true;
}
}
return false;
}

/**
* Get the access token or redirect to the authentication URL.
*
Expand Down Expand Up @@ -114,7 +133,7 @@ public function authenticate()
);
}

if (in_array('application/json', $response->getHeader('Content-Type'))) {
if ($this->isJsonResponse($response)) {
$token = array_merge(json_decode((string) $response->getBody(), true), ['created' => time()]);
} else {
parse_str((string) $response->getBody(), $token);
Expand Down Expand Up @@ -388,7 +407,8 @@ public function refreshToken($token = null)
);
}

if (in_array('application/json', $response->getHeader('Content-Type'))) {

if ($this->isJsonResponse($response)) {
$token = array_merge(json_decode((string) $response->getBody(), true), ['created' => time()]);
} else {
parse_str((string) $response->getBody(), $token);
Expand Down