Skip to content

Commit ad69827

Browse files
committed
reimplement for Keycloak
Signed-off-by: Naoto Kobayashi <[email protected]>
1 parent cf22df2 commit ad69827

File tree

11 files changed

+150
-223
lines changed

11 files changed

+150
-223
lines changed

DotAccess.php

Lines changed: 0 additions & 70 deletions
This file was deleted.

Generic.php

Lines changed: 0 additions & 37 deletions
This file was deleted.

Keycloak.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
namespace dokuwiki\plugin\oauthkeycloak;
4+
5+
use dokuwiki\plugin\oauth\Service\AbstractOAuth2Base;
6+
use OAuth\Common\Http\Uri\Uri;
7+
8+
/**
9+
* Custom Service for Keycloak oAuth
10+
*/
11+
class Keycloak extends AbstractOAuth2Base
12+
{
13+
/**
14+
* Defined scopes are listed here:
15+
* @link https://www.keycloak.org/docs/latest/server_admin/#_client_scopes
16+
*/
17+
const SCOPE_OPENID = 'openid';
18+
19+
/**
20+
* Endpoints are listed here:
21+
* @link https://www.iana.org/assignments/oauth-parameters/oauth-parameters.xhtml#authorization-server-metadata
22+
*/
23+
const ENDPOINT_AUTH = 'authorization_endpoint';
24+
const ENDPOINT_TOKEN = 'token_endpoint';
25+
const ENDPOINT_USERINFO = 'userinfo_endpoint';
26+
/**
27+
* This endpoint is used for backchannel logout and documented here
28+
* @link https://www.keycloak.org/docs/latest/server_admin/#con-basic-settings_server_administration_guide
29+
*/
30+
const ENDPOINT_LOGOUT = 'end_session_endpoint';
31+
32+
/**
33+
* Return URI of discovered endpoint
34+
*
35+
* @return string
36+
*/
37+
public static function getEndpointUri(string $endpoint)
38+
{
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];
45+
}
46+
47+
/** @inheritdoc */
48+
public function getAuthorizationEndpoint()
49+
{
50+
return new Uri(self::getEndpointUri(self::ENDPOINT_AUTH));
51+
}
52+
53+
/** @inheritdoc */
54+
public function getAccessTokenEndpoint()
55+
{
56+
return new Uri(self::getEndpointUri(self::ENDPOINT_TOKEN));
57+
}
58+
59+
/** @inheritdoc */
60+
protected function getAuthorizationMethod()
61+
{
62+
return static::AUTHORIZATION_METHOD_HEADER_BEARER;
63+
}
64+
65+
/**
66+
* Logout from Keycloak
67+
*
68+
* @return void
69+
* @throws \OAuth\Common\Exception\Exception
70+
*/
71+
public function logout()
72+
{
73+
$token = $this->getStorage()->retrieveAccessToken($this->service());
74+
$refreshToken = $token->getRefreshToken();
75+
76+
if (!$refreshToken) {
77+
return;
78+
}
79+
80+
$parameters = [
81+
'client_id' => $this->credentials->getConsumerId(),
82+
'client_secret' => $this->credentials->getConsumerSecret(),
83+
'refresh_token' => $refreshToken,
84+
];
85+
86+
$this->httpClient->retrieveResponse(
87+
new Uri(self::getEndpointUri(self::ENDPOINT_LOGOUT)),
88+
$parameters,
89+
$this->getExtraOAuthHeaders()
90+
);
91+
}
92+
}

README renamed to README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
1-
oauthgeneric Plugin for DokuWiki
1+
oauthkeycloak Plugin for DokuWiki
2+
===
23

3-
Generic Service for use with the oAuth Plugin
4+
Keycloak Service for use with the oAuth Plugin 2021-12-19 or above
45

56
All documentation for this plugin can be found at
6-
http://www.dokuwiki.org/plugin:oauthgeneric
7+
<http://www.dokuwiki.org/plugin:oauthkeycloak>
78

89
If you install this plugin manually, make sure it is installed in
9-
lib/plugins/oauthgeneric/ - if the folder is called different it
10+
lib/plugins/oauthkeycloak/ - if the folder is called different it
1011
will not work!
1112

12-
Please refer to http://www.dokuwiki.org/plugins for additional info
13+
Please refer to <http://www.dokuwiki.org/plugins> for additional info
1314
on how to install plugins in DokuWiki.
1415

1516
----
16-
Copyright (C) Andreas Gohr <[email protected]>
17+
Copyright (C) Naoto Kobayashi <[email protected]>
18+
19+
This program is based on [dokuwiki-plugin-oauthgeneric](https://github.com/cosmocode/dokuwiki-plugin-oauthgeneric)
20+
by Andreas Gohr <[email protected]>
1721

1822
This program is free software; you can redistribute it and/or modify
1923
it under the terms of the GNU General Public License as published by

action.php

Lines changed: 26 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
<?php
22

33
use dokuwiki\plugin\oauth\Adapter;
4-
use dokuwiki\plugin\oauthgeneric\DotAccess;
5-
use dokuwiki\plugin\oauthgeneric\Generic;
4+
use dokuwiki\plugin\oauthkeycloak\Keycloak;
65

76
/**
8-
* Service Implementation for oAuth Doorkeeper authentication
7+
* Service Implementation for Keycloak authentication
98
*/
10-
class action_plugin_oauthgeneric extends Adapter
9+
class action_plugin_oauthkeycloak extends Adapter
1110
{
12-
1311
/** @inheritdoc */
1412
public function registerServiceClass()
1513
{
16-
return Generic::class;
14+
return Keycloak::class;
15+
}
16+
17+
/**
18+
* @inheritdoc
19+
* @throws \OAuth\Common\Exception\Exception
20+
*/
21+
public function logout()
22+
{
23+
/** @var Keycloak */
24+
$oauth = $this->getOAuthService();
25+
$oauth->logout();
1726
}
1827

1928
/** * @inheritDoc */
@@ -22,59 +31,31 @@ public function getUser()
2231
$oauth = $this->getOAuthService();
2332
$data = array();
2433

25-
$url = $this->getConf('userurl');
34+
$url = Keycloak::getEndpointUri(Keycloak::ENDPOINT_USERINFO);
2635
$raw = $oauth->request($url);
2736

28-
if (!$raw) throw new OAuthException('Failed to fetch data from userurl');
37+
if (!$raw) throw new OAuthException('Failed to fetch data from userinfo endpoint');
2938
$result = json_decode($raw, true);
30-
if (!$result) throw new OAuthException('Failed to parse data from userurl');
31-
32-
$user = DotAccess::get($result, $this->getConf('json-user'), '');
33-
$name = DotAccess::get($result, $this->getConf('json-name'), '');
34-
$mail = DotAccess::get($result, $this->getConf('json-mail'), '');
35-
$grps = DotAccess::get($result, $this->getConf('json-grps'), []);
36-
37-
// type fixes
38-
if (is_array($user)) $user = array_shift($user);
39-
if (is_array($name)) $user = array_shift($name);
40-
if (is_array($mail)) $user = array_shift($mail);
41-
if (!is_array($grps)) {
42-
$grps = explode(',', $grps);
43-
$grps = array_map('trim', $grps);
44-
}
39+
if (!$result) throw new OAuthException('Failed to parse data from userinfo endpoint');
4540

46-
// fallbacks for user name
47-
if (empty($user)) {
48-
if (!empty($name)) {
49-
$user = $name;
50-
} elseif (!empty($mail)) {
51-
list($user) = explode('@', $mail);
52-
}
53-
}
54-
55-
// fallback for full name
56-
if (empty($name)) {
57-
$name = $user;
58-
}
41+
$data = array();
42+
$data['user'] = $result['preferred_username'];
43+
$data['name'] = $result['name'];
44+
$data['mail'] = $result['email'];
45+
$data['grps'] = $result['groups'];
5946

60-
return compact('user', 'name', 'mail', 'grps');
47+
return $data;
6148
}
6249

6350
/** @inheritdoc */
6451
public function getScopes()
6552
{
66-
return $this->getConf('scopes');
67-
}
68-
69-
/** @inheritDoc */
70-
public function getLabel()
71-
{
72-
return $this->getConf('label');
53+
return array(Keycloak::SCOPE_OPENID);
7354
}
7455

7556
/** @inheritDoc */
7657
public function getColor()
7758
{
78-
return $this->getConf('color');
59+
return '#333333';
7960
}
8061
}

conf/default.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
11
<?php
2+
23
/**
3-
* Default settings for the oauthgeneric plugin
4+
* Default settings for the oauthkeycloak plugin
45
*/
56

67
$conf['key'] = '';
78
$conf['secret'] = '';
89

9-
$conf['authurl'] = '';
10-
$conf['tokenurl'] = '';
11-
$conf['userurl'] = '';
12-
$conf['authmethod'] = 0;
13-
$conf['scopes'] = '';
14-
15-
$conf['json-user'] = '';
16-
$conf['json-name'] = '';
17-
$conf['json-mail'] = '';
18-
$conf['json-grps'] = '';
19-
20-
$conf['label'] = 'OAuth';
21-
$conf['color'] = '#333333';
10+
$conf['openidurl'] = '';

conf/metadata.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
11
<?php
2+
23
/**
3-
* Options for the oauthgeneric plugin
4+
* Options for the oauthkeycloak plugin
45
*/
56

67
$meta['key'] = array('string');
78
$meta['secret'] = array('password');
89

9-
$meta['authurl'] = array('string');
10-
$meta['tokenurl'] = array('string');
11-
$meta['userurl'] = array('string');
12-
$meta['authmethod'] = array('multichoice', '_choices' => [0, 1, 6, 2, 3, 4, 5]);
13-
$meta['scopes'] = array('array');
14-
15-
$meta['json-user'] = array('string');
16-
$meta['json-name'] = array('string');
17-
$meta['json-mail'] = array('string');
18-
$meta['json-grps'] = array('string');
19-
20-
$meta['label'] = array('string');
21-
$meta['color'] = array('string');
10+
$meta['openidurl'] = array('string');

0 commit comments

Comments
 (0)