Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
mstefan21 authored Dec 1, 2021
2 parents be38ccb + 2860f17 commit 8383f68
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/build
/vendor
composer.phar
composer.lock
.DS_Store
composer.lock
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ php:
- 5.6
- 7.0
- 7.1
- hhvm

matrix:
include:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ Please see [CONTRIBUTING](https://github.com/stevenmaguire/oauth2-keycloak/blob/
## Credits

- [Steven Maguire](https://github.com/stevenmaguire)
- [Martin Stefan](https://github.com/mstefan21)
- [All Contributors](https://github.com/stevenmaguire/oauth2-keycloak/contributors)


Expand Down
48 changes: 47 additions & 1 deletion src/Provider/Keycloak.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -174,9 +175,21 @@ protected function getBaseUrlWithRealm()
*/
protected function getDefaultScopes()
{
return ['name', 'email'];
return ['profile', 'email'];
}

/**
* Returns the string that should be used to separate scopes when building
* the URL for requesting an access token.
*
* @return string Scope separator, defaults to ','
*/
protected function getScopeSeparator()
{
return ' ';
}


/**
* Check a provider response for errors.
*
Expand Down Expand Up @@ -210,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);
Expand Down Expand Up @@ -276,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);
}
}

0 comments on commit 8383f68

Please sign in to comment.