Skip to content
Snippets Groups Projects
Commit cf22df2f authored by Andreas Gohr's avatar Andreas Gohr
Browse files

initial checkin

parents
Branches
No related tags found
No related merge requests found
<?php
namespace dokuwiki\plugin\oauthgeneric;
/**
* Dot notation access to arrays
*
* @see https://stackoverflow.com/a/39118759/172068
*/
class DotAccess
{
/**
* Get an item from an array using "dot" notation.
*
* @param \ArrayAccess|array $array
* @param string $key
* @param mixed $default
* @return mixed
*/
public static function get($array, $key, $default = null)
{
if (!static::accessible($array)) {
return $default;
}
if (is_null($key)) {
return $array;
}
if (static::exists($array, $key)) {
return $array[$key];
}
if (strpos($key, '.') === false) {
return $array[$key] ?? $default;
}
foreach (explode('.', $key) as $segment) {
if (static::accessible($array) && static::exists($array, $segment)) {
$array = $array[$segment];
} else {
return $default;
}
}
return $array;
}
/**
* Determine whether the given value is array accessible.
*
* @param mixed $value
* @return bool
*/
protected static function accessible($value)
{
return is_array($value) || $value instanceof \ArrayAccess;
}
/**
* Determine if the given key exists in the provided array.
*
* @param \ArrayAccess|array $array
* @param string|int $key
* @return bool
*/
protected static function exists($array, $key)
{
if ($array instanceof \ArrayAccess) {
return $array->offsetExists($key);
}
return array_key_exists($key, $array);
}
}
<?php
namespace dokuwiki\plugin\oauthgeneric;
use dokuwiki\plugin\oauth\Service\AbstractOAuth2Base;
use OAuth\Common\Http\Uri\Uri;
/**
* Custom Service for Generic oAuth
*/
class Generic extends AbstractOAuth2Base
{
/** @inheritdoc */
public function getAuthorizationEndpoint()
{
$plugin = plugin_load('helper', 'oauthgeneric');
return new Uri($plugin->getConf('authurl'));
}
/** @inheritdoc */
public function getAccessTokenEndpoint()
{
$plugin = plugin_load('helper', 'oauthgeneric');
return new Uri($plugin->getConf('tokenurl'));
}
/**
* @inheritdoc
*/
protected function getAuthorizationMethod()
{
$plugin = plugin_load('helper', 'oauthgeneric');
return $plugin->getConf('authmethod');
}
}
README 0 → 100644
oauthgeneric Plugin for DokuWiki
Generic Service for use with the oAuth Plugin
All documentation for this plugin can be found at
http://www.dokuwiki.org/plugin:oauthgeneric
If you install this plugin manually, make sure it is installed in
lib/plugins/oauthgeneric/ - if the folder is called different it
will not work!
Please refer to http://www.dokuwiki.org/plugins for additional info
on how to install plugins in DokuWiki.
----
Copyright (C) Andreas Gohr <dokuwiki@cosmocode.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
See the COPYING file in your DokuWiki folder for details
<?php
use dokuwiki\plugin\oauth\Adapter;
use dokuwiki\plugin\oauthgeneric\DotAccess;
use dokuwiki\plugin\oauthgeneric\Generic;
/**
* Service Implementation for oAuth Doorkeeper authentication
*/
class action_plugin_oauthgeneric extends Adapter
{
/** @inheritdoc */
public function registerServiceClass()
{
return Generic::class;
}
/** * @inheritDoc */
public function getUser()
{
$oauth = $this->getOAuthService();
$data = array();
$url = $this->getConf('userurl');
$raw = $oauth->request($url);
if (!$raw) throw new OAuthException('Failed to fetch data from userurl');
$result = json_decode($raw, true);
if (!$result) throw new OAuthException('Failed to parse data from userurl');
$user = DotAccess::get($result, $this->getConf('json-user'), '');
$name = DotAccess::get($result, $this->getConf('json-name'), '');
$mail = DotAccess::get($result, $this->getConf('json-mail'), '');
$grps = DotAccess::get($result, $this->getConf('json-grps'), []);
// type fixes
if (is_array($user)) $user = array_shift($user);
if (is_array($name)) $user = array_shift($name);
if (is_array($mail)) $user = array_shift($mail);
if (!is_array($grps)) {
$grps = explode(',', $grps);
$grps = array_map('trim', $grps);
}
// fallbacks for user name
if (empty($user)) {
if (!empty($name)) {
$user = $name;
} elseif (!empty($mail)) {
list($user) = explode('@', $mail);
}
}
// fallback for full name
if (empty($name)) {
$name = $user;
}
return compact('user', 'name', 'mail', 'grps');
}
/** @inheritdoc */
public function getScopes()
{
return $this->getConf('scopes');
}
/** @inheritDoc */
public function getLabel()
{
return $this->getConf('label');
}
/** @inheritDoc */
public function getColor()
{
return $this->getConf('color');
}
}
<?php
/**
* Default settings for the oauthgeneric plugin
*/
$conf['key'] = '';
$conf['secret'] = '';
$conf['authurl'] = '';
$conf['tokenurl'] = '';
$conf['userurl'] = '';
$conf['authmethod'] = 0;
$conf['scopes'] = '';
$conf['json-user'] = '';
$conf['json-name'] = '';
$conf['json-mail'] = '';
$conf['json-grps'] = '';
$conf['label'] = 'OAuth';
$conf['color'] = '#333333';
<?php
/**
* Options for the oauthgeneric plugin
*/
$meta['key'] = array('string');
$meta['secret'] = array('password');
$meta['authurl'] = array('string');
$meta['tokenurl'] = array('string');
$meta['userurl'] = array('string');
$meta['authmethod'] = array('multichoice', '_choices' => [0, 1, 6, 2, 3, 4, 5]);
$meta['scopes'] = array('array');
$meta['json-user'] = array('string');
$meta['json-name'] = array('string');
$meta['json-mail'] = array('string');
$meta['json-grps'] = array('string');
$meta['label'] = array('string');
$meta['color'] = array('string');
<?php
/**
* DokuWiki Plugin oauthgeneric (Helper Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Andreas Gohr <dokuwiki@cosmocode.de>
*/
class helper_plugin_oauthgeneric extends \dokuwiki\Extension\Plugin
{
}
<?php
/**
* english language file for oauthgeneric plugin
*
* @author Andreas Gohr <dokuwiki@cosmocode.de>
*/
$lang['key'] = 'The Application UID';
$lang['secret'] = 'The Application Secret';
$lang['authurl'] = 'URL to the authentication endpoint';
$lang['tokenurl'] = 'URL to the token endpoint';
$lang['userurl'] = 'URL to the user info API endpoint (must return JSON about the authenticated user)';
$lang['authmethod'] = 'Authorization method used when talking to the user API';
$lang['scopes'] = 'Scopes to request (comma separated)';
$lang['json-user'] = 'Access to the username in dot notation';
$lang['json-name'] = 'Access to the username in dot notation';
$lang['json-mail'] = 'Access to the username in dot notation';
$lang['json-grps'] = 'Access to the username in dot notation';
$lang['label'] = 'Label to display on the login button';
$lang['color'] = 'Color to use with the login button';
$lang['authmethod_o_0'] = 'OAuth Header';
$lang['authmethod_o_1'] = 'Bearer Header';
$lang['authmethod_o_6'] = 'Token Header';
$lang['authmethod_o_2'] = 'Query String v1';
$lang['authmethod_o_3'] = 'Query String v2';
$lang['authmethod_o_4'] = 'Query String v3';
$lang['authmethod_o_5'] = 'Query String v4';
<svg viewBox="0 0 24 24"><path d="M12,17A2,2 0 0,0 14,15C14,13.89 13.1,13 12,13A2,2 0 0,0 10,15A2,2 0 0,0 12,17M18,8A2,2 0 0,1 20,10V20A2,2 0 0,1 18,22H6A2,2 0 0,1 4,20V10C4,8.89 4.9,8 6,8H7V6A5,5 0 0,1 12,1A5,5 0 0,1 17,6V8H18M12,3A3,3 0 0,0 9,6V8H15V6A3,3 0 0,0 12,3Z"/></svg>
\ No newline at end of file
base oauthgeneric
author Andreas Gohr
email dokuwiki@cosmocode.de
date 2021-12-05
name oauth generic Service
desc Generic Service for use with the oAuth Plugin
url http://www.dokuwiki.org/plugin:oauthgeneric
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment