Skip to content

Commit 16bafb6

Browse files
committed
Make getEndpointUri instance method
To reuse the data gotten from discovery URL Signed-off-by: Naoto Kobayashi <[email protected]>
1 parent ad69827 commit 16bafb6

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

Keycloak.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,35 @@ class Keycloak extends AbstractOAuth2Base
2929
*/
3030
const ENDPOINT_LOGOUT = 'end_session_endpoint';
3131

32+
protected $discovery;
33+
3234
/**
3335
* Return URI of discovered endpoint
3436
*
3537
* @return string
3638
*/
37-
public static function getEndpointUri(string $endpoint)
39+
public function getEndpoint(string $endpoint)
3840
{
39-
$plugin = plugin_load('helper', 'oauthkeycloak');
40-
$json = file_get_contents($plugin->getConf('openidurl'));
41-
if (!$json) return '';
42-
$data = json_decode($json, true);
43-
if (!isset($data[$endpoint])) return '';
44-
return $data[$endpoint];
41+
if (!isset($this->discovery)) {
42+
$plugin = plugin_load('helper', 'oauthkeycloak');
43+
$json = file_get_contents($plugin->getConf('openidurl'));
44+
if (!$json) return '';
45+
$this->discovery = json_decode($json, true);
46+
}
47+
if (!isset($this->discovery[$endpoint])) return '';
48+
return $this->discovery[$endpoint];
4549
}
4650

4751
/** @inheritdoc */
4852
public function getAuthorizationEndpoint()
4953
{
50-
return new Uri(self::getEndpointUri(self::ENDPOINT_AUTH));
54+
return new Uri($this->getEndpoint(self::ENDPOINT_AUTH));
5155
}
5256

5357
/** @inheritdoc */
5458
public function getAccessTokenEndpoint()
5559
{
56-
return new Uri(self::getEndpointUri(self::ENDPOINT_TOKEN));
60+
return new Uri($this->getEndpoint(self::ENDPOINT_TOKEN));
5761
}
5862

5963
/** @inheritdoc */
@@ -84,7 +88,7 @@ public function logout()
8488
];
8589

8690
$this->httpClient->retrieveResponse(
87-
new Uri(self::getEndpointUri(self::ENDPOINT_LOGOUT)),
91+
new Uri($this->getEndpoint(self::ENDPOINT_LOGOUT)),
8892
$parameters,
8993
$this->getExtraOAuthHeaders()
9094
);

action.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ public function logout()
2828
/** * @inheritDoc */
2929
public function getUser()
3030
{
31+
/** @var Keycloak */
3132
$oauth = $this->getOAuthService();
3233
$data = array();
3334

34-
$url = Keycloak::getEndpointUri(Keycloak::ENDPOINT_USERINFO);
35+
$url = $oauth->getEndpoint(Keycloak::ENDPOINT_USERINFO);
3536
$raw = $oauth->request($url);
3637

3738
if (!$raw) throw new OAuthException('Failed to fetch data from userinfo endpoint');

0 commit comments

Comments
 (0)