Skip to content

Commit cf22df2

Browse files
committed
initial checkin
0 parents  commit cf22df2

File tree

10 files changed

+306
-0
lines changed

10 files changed

+306
-0
lines changed

DotAccess.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace dokuwiki\plugin\oauthgeneric;
4+
5+
/**
6+
* Dot notation access to arrays
7+
*
8+
* @see https://stackoverflow.com/a/39118759/172068
9+
*/
10+
class DotAccess
11+
{
12+
13+
/**
14+
* Get an item from an array using "dot" notation.
15+
*
16+
* @param \ArrayAccess|array $array
17+
* @param string $key
18+
* @param mixed $default
19+
* @return mixed
20+
*/
21+
public static function get($array, $key, $default = null)
22+
{
23+
if (!static::accessible($array)) {
24+
return $default;
25+
}
26+
if (is_null($key)) {
27+
return $array;
28+
}
29+
if (static::exists($array, $key)) {
30+
return $array[$key];
31+
}
32+
if (strpos($key, '.') === false) {
33+
return $array[$key] ?? $default;
34+
}
35+
foreach (explode('.', $key) as $segment) {
36+
if (static::accessible($array) && static::exists($array, $segment)) {
37+
$array = $array[$segment];
38+
} else {
39+
return $default;
40+
}
41+
}
42+
return $array;
43+
}
44+
45+
/**
46+
* Determine whether the given value is array accessible.
47+
*
48+
* @param mixed $value
49+
* @return bool
50+
*/
51+
protected static function accessible($value)
52+
{
53+
return is_array($value) || $value instanceof \ArrayAccess;
54+
}
55+
56+
/**
57+
* Determine if the given key exists in the provided array.
58+
*
59+
* @param \ArrayAccess|array $array
60+
* @param string|int $key
61+
* @return bool
62+
*/
63+
protected static function exists($array, $key)
64+
{
65+
if ($array instanceof \ArrayAccess) {
66+
return $array->offsetExists($key);
67+
}
68+
return array_key_exists($key, $array);
69+
}
70+
}

Generic.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace dokuwiki\plugin\oauthgeneric;
4+
5+
use dokuwiki\plugin\oauth\Service\AbstractOAuth2Base;
6+
use OAuth\Common\Http\Uri\Uri;
7+
8+
/**
9+
* Custom Service for Generic oAuth
10+
*/
11+
class Generic extends AbstractOAuth2Base
12+
{
13+
14+
/** @inheritdoc */
15+
public function getAuthorizationEndpoint()
16+
{
17+
$plugin = plugin_load('helper', 'oauthgeneric');
18+
return new Uri($plugin->getConf('authurl'));
19+
}
20+
21+
/** @inheritdoc */
22+
public function getAccessTokenEndpoint()
23+
{
24+
$plugin = plugin_load('helper', 'oauthgeneric');
25+
return new Uri($plugin->getConf('tokenurl'));
26+
}
27+
28+
/**
29+
* @inheritdoc
30+
*/
31+
protected function getAuthorizationMethod()
32+
{
33+
$plugin = plugin_load('helper', 'oauthgeneric');
34+
35+
return $plugin->getConf('authmethod');
36+
}
37+
}

README

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
oauthgeneric Plugin for DokuWiki
2+
3+
Generic Service for use with the oAuth Plugin
4+
5+
All documentation for this plugin can be found at
6+
http://www.dokuwiki.org/plugin:oauthgeneric
7+
8+
If you install this plugin manually, make sure it is installed in
9+
lib/plugins/oauthgeneric/ - if the folder is called different it
10+
will not work!
11+
12+
Please refer to http://www.dokuwiki.org/plugins for additional info
13+
on how to install plugins in DokuWiki.
14+
15+
----
16+
Copyright (C) Andreas Gohr <[email protected]>
17+
18+
This program is free software; you can redistribute it and/or modify
19+
it under the terms of the GNU General Public License as published by
20+
the Free Software Foundation; version 2 of the License
21+
22+
This program is distributed in the hope that it will be useful,
23+
but WITHOUT ANY WARRANTY; without even the implied warranty of
24+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25+
GNU General Public License for more details.
26+
27+
See the COPYING file in your DokuWiki folder for details

action.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
use dokuwiki\plugin\oauth\Adapter;
4+
use dokuwiki\plugin\oauthgeneric\DotAccess;
5+
use dokuwiki\plugin\oauthgeneric\Generic;
6+
7+
/**
8+
* Service Implementation for oAuth Doorkeeper authentication
9+
*/
10+
class action_plugin_oauthgeneric extends Adapter
11+
{
12+
13+
/** @inheritdoc */
14+
public function registerServiceClass()
15+
{
16+
return Generic::class;
17+
}
18+
19+
/** * @inheritDoc */
20+
public function getUser()
21+
{
22+
$oauth = $this->getOAuthService();
23+
$data = array();
24+
25+
$url = $this->getConf('userurl');
26+
$raw = $oauth->request($url);
27+
28+
if (!$raw) throw new OAuthException('Failed to fetch data from userurl');
29+
$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+
}
45+
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+
}
59+
60+
return compact('user', 'name', 'mail', 'grps');
61+
}
62+
63+
/** @inheritdoc */
64+
public function getScopes()
65+
{
66+
return $this->getConf('scopes');
67+
}
68+
69+
/** @inheritDoc */
70+
public function getLabel()
71+
{
72+
return $this->getConf('label');
73+
}
74+
75+
/** @inheritDoc */
76+
public function getColor()
77+
{
78+
return $this->getConf('color');
79+
}
80+
}

conf/default.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Default settings for the oauthgeneric plugin
4+
*/
5+
6+
$conf['key'] = '';
7+
$conf['secret'] = '';
8+
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';

conf/metadata.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Options for the oauthgeneric plugin
4+
*/
5+
6+
$meta['key'] = array('string');
7+
$meta['secret'] = array('password');
8+
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');

helper.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/**
3+
* DokuWiki Plugin oauthgeneric (Helper Component)
4+
*
5+
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6+
* @author Andreas Gohr <[email protected]>
7+
*/
8+
class helper_plugin_oauthgeneric extends \dokuwiki\Extension\Plugin
9+
{
10+
11+
}
12+

lang/en/settings.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* english language file for oauthgeneric plugin
4+
*
5+
* @author Andreas Gohr <[email protected]>
6+
*/
7+
8+
$lang['key'] = 'The Application UID';
9+
$lang['secret'] = 'The Application Secret';
10+
$lang['authurl'] = 'URL to the authentication endpoint';
11+
$lang['tokenurl'] = 'URL to the token endpoint';
12+
$lang['userurl'] = 'URL to the user info API endpoint (must return JSON about the authenticated user)';
13+
$lang['authmethod'] = 'Authorization method used when talking to the user API';
14+
$lang['scopes'] = 'Scopes to request (comma separated)';
15+
16+
$lang['json-user'] = 'Access to the username in dot notation';
17+
$lang['json-name'] = 'Access to the username in dot notation';
18+
$lang['json-mail'] = 'Access to the username in dot notation';
19+
$lang['json-grps'] = 'Access to the username in dot notation';
20+
21+
$lang['label'] = 'Label to display on the login button';
22+
$lang['color'] = 'Color to use with the login button';
23+
24+
$lang['authmethod_o_0'] = 'OAuth Header';
25+
$lang['authmethod_o_1'] = 'Bearer Header';
26+
$lang['authmethod_o_6'] = 'Token Header';
27+
$lang['authmethod_o_2'] = 'Query String v1';
28+
$lang['authmethod_o_3'] = 'Query String v2';
29+
$lang['authmethod_o_4'] = 'Query String v3';
30+
$lang['authmethod_o_5'] = 'Query String v4';

logo.svg

Lines changed: 1 addition & 0 deletions
Loading

plugin.info.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
base oauthgeneric
2+
author Andreas Gohr
3+
4+
date 2021-12-05
5+
name oauth generic Service
6+
desc Generic Service for use with the oAuth Plugin
7+
url http://www.dokuwiki.org/plugin:oauthgeneric

0 commit comments

Comments
 (0)