diff --git a/.gitignore b/.gitignore index 9c9c8f2..e8c7cb6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ /build /vendor composer.phar -composer.lock -.DS_Store +composer.lock \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index d99d282..87831a5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,6 @@ php: - 5.6 - 7.0 - 7.1 - - hhvm matrix: include: diff --git a/composer.json b/composer.json index 324611a..6b28ab5 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "keycloak" ], "require": { - "league/oauth2-client": "^2.0 <2.3.0", + "league/oauth2-client": "^2.0", "firebase/php-jwt": "^4.0" }, "require-dev": { diff --git a/src/Provider/Keycloak.php b/src/Provider/Keycloak.php index f4c619e..04dcf6f 100644 --- a/src/Provider/Keycloak.php +++ b/src/Provider/Keycloak.php @@ -10,6 +10,7 @@ use League\OAuth2\Client\Tool\BearerAuthorizationTrait; use Psr\Http\Message\ResponseInterface; use Stevenmaguire\OAuth2\Client\Provider\Exception\EncryptionConfigurationException; +use UnexpectedValueException; class Keycloak extends AbstractProvider { @@ -222,11 +223,18 @@ protected function createResourceOwner(array $response, AccessToken $token) * * @param AccessToken $token * @return KeycloakResourceOwner + * @throws EncryptionConfigurationException */ public function getResourceOwner(AccessToken $token) { $response = $this->fetchResourceOwnerDetails($token); + // We are always getting an array. We have to check if it is + // the array we created + if (array_key_exists('jwt', $response)) { + $response = $response['jwt']; + } + $response = $this->decryptResponse($response); return $this->createResourceOwner($response, $token); @@ -288,4 +296,30 @@ public function usesEncryption() { return (bool) $this->encryptionAlgorithm && $this->encryptionKey; } + + /** + * Parses the response according to its content-type header. + * + * @throws UnexpectedValueException + * @param ResponseInterface $response + * @return array + */ + protected function parseResponse(ResponseInterface $response) + { + // We have a problem with keycloak when the userinfo responses + // with a jwt token + // Because it just return a jwt as string with the header + // application/jwt + // This can't be parsed to a array + // Dont know why this function only allow an array as return value... + $content = (string) $response->getBody(); + $type = $this->getContentType($response); + + if (strpos($type, 'jwt') !== false) { + // Here we make the temporary array + return ['jwt' => $content]; + } + + return parent::parseResponse($response); + } }