Skip to content

Commit

Permalink
automatic style fixes and new workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
splitbrain committed Mar 5, 2024
1 parent db85e03 commit 290e9b1
Show file tree
Hide file tree
Showing 16 changed files with 123 additions and 146 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/dokuwiki.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: DokuWiki Default Tasks
on:
push:
pull_request:
schedule:
- cron: '14 0 5 * *'


jobs:
all:
uses: dokuwiki/github-action/.github/workflows/all.yml@main
52 changes: 0 additions & 52 deletions .github/workflows/phpTestLinux.yml

This file was deleted.

24 changes: 14 additions & 10 deletions Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace dokuwiki\plugin\oauth;

use dokuwiki\Extension\EventHandler;
use dokuwiki\Extension\Event;
use dokuwiki\Extension\ActionPlugin;
use OAuth\Common\Consumer\Credentials;
use OAuth\Common\Http\Exception\TokenResponseException;
Expand Down Expand Up @@ -32,15 +34,15 @@ abstract class Adapter extends ActionPlugin
*
* @inheritDoc
*/
public function register(\Doku_Event_Handler $controller)
public function register(EventHandler $controller)
{
$controller->register_hook('PLUGIN_OAUTH_BACKEND_REGISTER', 'AFTER', $this, 'handleRegister');
}

/**
* Auto register this plugin with the oAuth authentication plugin
*/
public function handleRegister(\Doku_Event $event, $param)
public function handleRegister(Event $event, $param)
{
$event->data[$this->getServiceID()] = $this;
}
Expand All @@ -64,6 +66,7 @@ public function initOAuthService($storageId = '')

$serviceFactory = new ServiceFactory();
$serviceFactory->setHttpClient(new HTTPClient());

$servicename = $this->getServiceID();
$serviceclass = $this->registerServiceClass();
if ($serviceclass) {
Expand Down Expand Up @@ -143,8 +146,10 @@ public function refreshOutdatedToken()
}

$token = $oauth->getStorage()->retrieveAccessToken($oauth->service());
if ($token->getEndOfLife() < 0 ||
$token->getEndOfLife() - time() > 3600) {
if (
$token->getEndOfLife() < 0 ||
$token->getEndOfLife() - time() > 3600
) {
// token is still good
return;
}
Expand All @@ -168,7 +173,7 @@ public function refreshOutdatedToken()
* but might need to be overwritten for specific services
*
* @throws TokenResponseException
* @throws Exception
* @throws \Exception
*/
public function login()
{
Expand All @@ -182,7 +187,7 @@ public function login()
$parameters['state'] = urlencode(base64_encode(json_encode(
[
'animal' => $animal,
'state' => md5(rand()),
'state' => md5(random_int(0, mt_getrandmax())),
]
)));
$oauth->getStorage()->storeAuthorizationState($oauth->service(), $parameters['state']);
Expand Down Expand Up @@ -221,12 +226,10 @@ public function checkToken()
$oauth = $this->getOAuthService();

if (is_a($oauth, Abstract2Service::class)) {
/** @var Abstract2Service $oauth */
if (!$INPUT->get->has('code')) return false;
$state = $INPUT->get->str('state', null);
$accessToken = $oauth->requestAccessToken($INPUT->get->str('code'), $state);
} else {
/** @var Abstract1Service $oauth */
if (!$INPUT->get->has('oauth_token')) return false;
/** @var TokenInterface $token */
$token = $oauth->getStorage()->retrieveAccessToken($this->getServiceID());
Expand All @@ -239,7 +242,8 @@ public function checkToken()

if (
$accessToken->getEndOfLife() !== $accessToken::EOL_NEVER_EXPIRES &&
!$accessToken->getRefreshToken()) {
!$accessToken->getRefreshToken()
) {
msg('Service did not provide a Refresh Token. You will be logged out when the session expires.');
}

Expand All @@ -256,7 +260,7 @@ public function loginButton()
global $ID;

$attr = buildAttributes([
'href' => wl($ID, array('oauthlogin' => $this->getServiceID()), false, '&'),
'href' => wl($ID, ['oauthlogin' => $this->getServiceID()], false, '&'),
'class' => 'plugin_oauth_' . $this->getServiceID(),
'style' => 'background-color: ' . $this->getColor(),
]);
Expand Down
1 change: 0 additions & 1 deletion Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,4 @@ public function setContext(array $context)
{
$this->context = $context;
}

}
5 changes: 2 additions & 3 deletions HTTPClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@
*/
class HTTPClient implements ClientInterface
{

/** @inheritDoc */
public function retrieveResponse(
UriInterface $endpoint,
$requestBody,
array $extraHeaders = array(),
array $extraHeaders = [],
$method = 'POST'
) {
$http = new DokuHTTPClient;
$http = new DokuHTTPClient();
$http->keep_alive = false;
$http->headers = array_merge($http->headers, $extraHeaders);

Expand Down
13 changes: 6 additions & 7 deletions OAuthManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function startFlow($servicename)

$session = Session::getInstance();
$session->setLoginData($servicename, $ID);

$service = $this->loadService($servicename);
$service->initOAuthService();
$service->login(); // redirects
Expand All @@ -36,9 +37,7 @@ public function startFlow($servicename)
*/
public function continueFlow()
{
return $this->loginByService() or
$this->loginBySession() or
$this->loginByCookie();
return $this->loginByService() || $this->loginBySession() || $this->loginByCookie();
}

/**
Expand All @@ -62,6 +61,7 @@ protected function loginByService()
if (!$logindata) return false;
$service = $this->loadService($logindata['servicename']);
$service->initOAuthService();

$session->clearLoginData();

// oAuth login
Expand Down Expand Up @@ -196,14 +196,14 @@ protected function validateUserData($userdata, $servicename)
$hlp = plugin_load('helper', 'oauth');

if (!$hlp->checkMail($userdata['mail'])) {
throw new Exception('rejectedEMail', [join(', ', $hlp->getValidDomains())]);
throw new Exception('rejectedEMail', [implode(', ', $hlp->getValidDomains())]);
}

// make username from mail if empty
if (!isset($userdata['user'])) $userdata['user'] = '';
$userdata['user'] = $auth->cleanUser((string)$userdata['user']);
if ($userdata['user'] === '') {
list($userdata['user']) = explode('@', $userdata['mail']);
[$userdata['user']] = explode('@', $userdata['mail']);
}

// make full name from username if empty
Expand Down Expand Up @@ -238,7 +238,7 @@ protected function processUserData($userdata, $servicename)
if ($localUser) {
$localUserInfo = $auth->getUserData($localUser);
$localUserInfo['user'] = $localUser;
if(isset($localUserInfo['pass'])) unset($localUserInfo['pass']);
if (isset($localUserInfo['pass'])) unset($localUserInfo['pass']);

// check if the user allowed access via this service
if (!in_array($auth->cleanGroup($servicename), $localUserInfo['grps'])) {
Expand Down Expand Up @@ -312,5 +312,4 @@ protected function loadService($servicename)
if ($srv === null) throw new Exception("No such service $servicename");
return $srv;
}

}
16 changes: 8 additions & 8 deletions RedirectSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
/**
* Custom Setting to display the default redirect URL
*/
class RedirectSetting extends Setting {

class RedirectSetting extends Setting
{
/** @inheritdoc */
function update($input) {
public function update($input)
{
return true;
}

Expand All @@ -22,11 +23,10 @@ public function html(\admin_plugin_config $plugin, $echo = false)
$hlp = plugin_load('helper', 'oauth');

$key = htmlspecialchars($this->key);
$value = '<code>'.$hlp->redirectURI().'</code>';
$value = '<code>' . $hlp->redirectURI() . '</code>';

$label = '<label for="config___'.$key.'">'.$this->prompt($plugin).'</label>';
$input = '<div>'.$value.'</div>';
return array($label, $input);
$label = '<label for="config___' . $key . '">' . $this->prompt($plugin) . '</label>';
$input = '<div>' . $value . '</div>';
return [$label, $input];
}

}
2 changes: 0 additions & 2 deletions Service/AbstractOAuth2Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/
abstract class AbstractOAuth2Base extends AbstractService
{

/** @inheritdoc */
protected function parseAccessTokenResponse($responseBody)
{
Expand Down Expand Up @@ -55,5 +54,4 @@ public function isValidScope($scope)
{
return true;
}

}
35 changes: 19 additions & 16 deletions Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class Session
{
/** @var Session */
protected static $instance = null;
protected static $instance;

/**
* hidden constructor
Expand Down Expand Up @@ -52,10 +52,7 @@ public function setLoginData($servicename, $id)
*/
public function getLoginData()
{
if (isset($_SESSION[DOKU_COOKIE]['auth']['oauth'])) {
return $_SESSION[DOKU_COOKIE]['auth']['oauth'];
}
return false;
return $_SESSION[DOKU_COOKIE]['auth']['oauth'] ?? false;
}

/**
Expand Down Expand Up @@ -83,10 +80,10 @@ public function setUser($userdata, $resettime = true)
global $USERINFO;

if (
!isset($userdata['user']) or
!isset($userdata['name']) or
!isset($userdata['mail']) or
!isset($userdata['grps']) or
!isset($userdata['user']) ||
!isset($userdata['name']) ||
!isset($userdata['mail']) ||
!isset($userdata['grps']) ||
!is_array($userdata['grps'])
) {
throw new Exception('Missing user data, cannot save to session');
Expand All @@ -111,10 +108,7 @@ public function setUser($userdata, $resettime = true)
*/
public function getUser()
{
if (isset($_SESSION[DOKU_COOKIE]['auth']['info'])) {
return $_SESSION[DOKU_COOKIE]['auth']['info'];
}
return false;
return $_SESSION[DOKU_COOKIE]['auth']['info'] ?? false;
}

/**
Expand All @@ -133,7 +127,17 @@ public function setCookie($servicename, $storageId)
$cookie = "$servicename|oauth|$storageId";
$cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir'];
$time = time() + $validityPeriodInSeconds;
setcookie(DOKU_COOKIE, $cookie, $time, $cookieDir, '', ($conf['securecookie'] && is_ssl()), true);
setcookie(
DOKU_COOKIE,
$cookie,
[
'expires' => $time,
'path' => $cookieDir,
'domain' => '',
'secure' => $conf['securecookie'] && is_ssl(),
'httponly' => true
]
);
}

/**
Expand All @@ -144,7 +148,7 @@ public function setCookie($servicename, $storageId)
public function getCookie()
{
if (!isset($_COOKIE[DOKU_COOKIE])) return false;
list($servicename, $oauth, $storageId) = explode('|', $_COOKIE[DOKU_COOKIE]);
[$servicename, $oauth, $storageId] = explode('|', $_COOKIE[DOKU_COOKIE]);
if ($oauth !== 'oauth') return false;
return ['servicename' => $servicename, 'storageId' => $storageId];
}
Expand Down Expand Up @@ -173,6 +177,5 @@ public function clear()
{
//FIXME clear cookie?
$this->clearLoginData();

}
}
2 changes: 1 addition & 1 deletion Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected function loadServiceFile($service)
if (file_exists($file)) {
return unserialize(io_readFile($file, false));
} else {
return array();
return [];
}
}

Expand Down
Loading

0 comments on commit 290e9b1

Please sign in to comment.