Skip to content
Open
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
28 changes: 23 additions & 5 deletions Keycloak.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,28 @@ class Keycloak extends AbstractOAuth2Base
*
* @return string
*/
public function getEndpoint(string $endpoint)
public function getDiscovery(string $endpoint, $default = '')
{
if (!isset($this->discovery)) {
$plugin = plugin_load('helper', 'oauthkeycloak');
$json = file_get_contents($plugin->getConf('openidurl'));
if (!$json) throw new \Exception('Failed accessing ' . $plugin->getConf('openidurl'));
$this->discovery = json_decode($json, true);
}
if (!isset($this->discovery[$endpoint])) return '';
if (!isset($this->discovery[$endpoint])) return $default;
return $this->discovery[$endpoint];
}

/** @inheritdoc */
public function getAuthorizationEndpoint()
{
return new Uri($this->getEndpoint(self::ENDPOINT_AUTH));
return new Uri($this->getDiscovery(self::ENDPOINT_AUTH));
}

/** @inheritdoc */
public function getAccessTokenEndpoint()
{
return new Uri($this->getEndpoint(self::ENDPOINT_TOKEN));
return new Uri($this->getDiscovery(self::ENDPOINT_TOKEN));
}

/** @inheritdoc */
Expand All @@ -66,6 +66,24 @@ protected function getAuthorizationMethod()
return static::AUTHORIZATION_METHOD_HEADER_BEARER;
}

/** @inheritdoc */
public function needsStateParameterInAuthUrl()
{
return true;
}

/** @inheritdoc */
public function getCodeChallengeMethod()
{
$supported = $this->getDiscovery('code_challenge_methods_supported', []);
foreach (['S256', 'plain'] as $method) {
if (in_array($method, $supported)) {
return $method;
}
}
return null;
}

/**
* Logout from Keycloak
*
Expand All @@ -88,7 +106,7 @@ public function logout()
];

$this->httpClient->retrieveResponse(
new Uri($this->getEndpoint(self::ENDPOINT_LOGOUT)),
new Uri($this->getDiscovery(self::ENDPOINT_LOGOUT)),
$parameters,
$this->getExtraOAuthHeaders()
);
Expand Down