180852c15SAndreas Gohr<?php 2290e9b1fSAndreas Gohr 3290e9b1fSAndreas Gohr// phpcs:disable PSR1.Files.SideEffects.FoundWithSymbols 4290e9b1fSAndreas Gohr 580852c15SAndreas Gohr/** 680852c15SAndreas Gohr * DokuWiki Plugin oauth (Helper Component) 780852c15SAndreas Gohr * 880852c15SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 980852c15SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1080852c15SAndreas Gohr */ 1180852c15SAndreas Gohr 12290e9b1fSAndreas Gohruse dokuwiki\Extension\Plugin; 1398a36116SAndreas Gohruse dokuwiki\Extension\Event; 1404a78b87SAndreas Gohruse dokuwiki\plugin\oauth\Adapter; 1580852c15SAndreas Gohr 16290e9b1fSAndreas Gohrrequire_once(__DIR__ . '/vendor/autoload.php'); // @todo can be removed with next dw release 1798a36116SAndreas Gohr 1898a36116SAndreas Gohr/** 1998a36116SAndreas Gohr * Basic helper methods for the oauth flow 2098a36116SAndreas Gohr */ 21290e9b1fSAndreas Gohrclass helper_plugin_oauth extends Plugin 2298a36116SAndreas Gohr{ 2380852c15SAndreas Gohr /** 24f10e09e2SAndreas Gohr * Load the needed libraries and initialize the named oAuth service 2580852c15SAndreas Gohr * 26f10e09e2SAndreas Gohr * @param string $servicename 2704a78b87SAndreas Gohr * @return null|Adapter 2880852c15SAndreas Gohr */ 2998a36116SAndreas Gohr public function loadService($servicename) 3098a36116SAndreas Gohr { 3198a36116SAndreas Gohr $services = $this->listServices(true); 3298a36116SAndreas Gohr if (!isset($services[$servicename])) return null; 336d9a8a49SAndreas Gohr return $services[$servicename]; 34f10e09e2SAndreas Gohr } 35f10e09e2SAndreas Gohr 36a90c044eSAndreas Gohr /** 37a90c044eSAndreas Gohr * The redirect URI used in all oAuth requests 38a90c044eSAndreas Gohr * 39a90c044eSAndreas Gohr * @return string 40a90c044eSAndreas Gohr */ 4198a36116SAndreas Gohr public function redirectURI() 4298a36116SAndreas Gohr { 439683193cSMichael Große if ($this->getConf('custom-redirectURI') !== '') { 449683193cSMichael Große return $this->getConf('custom-redirectURI'); 459683193cSMichael Große } else { 462e94f0b8SAndreas Gohr return DOKU_URL . DOKU_SCRIPT; 472e94f0b8SAndreas Gohr } 489683193cSMichael Große } 492e94f0b8SAndreas Gohr 50dfbdd519SAndreas Gohr /** 51dfbdd519SAndreas Gohr * List available Services 52dfbdd519SAndreas Gohr * 536d9a8a49SAndreas Gohr * Services returned here, do not have initialized oAuth providers yet! 546d9a8a49SAndreas Gohr * 5598a36116SAndreas Gohr * @param bool $enabledonly list only services that have been configured 5698a36116SAndreas Gohr * @triggers PLUGIN_OAUTH_BACKEND_REGISTER 5704a78b87SAndreas Gohr * @return Adapter[] list of service objects 58dfbdd519SAndreas Gohr */ 5998a36116SAndreas Gohr public function listServices($enabledonly = true) 6098a36116SAndreas Gohr { 6198a36116SAndreas Gohr $services = []; 6298a36116SAndreas Gohr $event = new Event('PLUGIN_OAUTH_BACKEND_REGISTER', $services); 6398a36116SAndreas Gohr $event->advise_before(false); 6498a36116SAndreas Gohr $event->advise_after(); 65dfbdd519SAndreas Gohr 6698a36116SAndreas Gohr // filter out unconfigured services 6798a36116SAndreas Gohr if ($enabledonly) { 68290e9b1fSAndreas Gohr $services = array_filter($services, static fn($service) => 6904a78b87SAndreas Gohr /** @var Adapter $service */ 70290e9b1fSAndreas Gohr (bool)$service->getKey()); 71dfbdd519SAndreas Gohr } 72dfbdd519SAndreas Gohr 73dfbdd519SAndreas Gohr return $services; 74dfbdd519SAndreas Gohr } 75f10e09e2SAndreas Gohr 76f10e09e2SAndreas Gohr /** 77ebf681d1SMichael Große * @return array 78d9818adbSMichael Große */ 7998a36116SAndreas Gohr public function getValidDomains() 8098a36116SAndreas Gohr { 813c0c1b14SMichael Große if ($this->getConf('mailRestriction') === '') { 82290e9b1fSAndreas Gohr return []; 833c0c1b14SMichael Große } 84*fe49fd82SAndreas Gohr $domains = explode(',', trim($this->getConf('mailRestriction'), ',')); 85*fe49fd82SAndreas Gohr return array_map('trim', $domains); 86*fe49fd82SAndreas Gohr } 87*fe49fd82SAndreas Gohr 88*fe49fd82SAndreas Gohr /** 89*fe49fd82SAndreas Gohr * @return array 90*fe49fd82SAndreas Gohr */ 91*fe49fd82SAndreas Gohr public function getEnforcedDomains() 92*fe49fd82SAndreas Gohr { 93*fe49fd82SAndreas Gohr if ($this->getConf('mailEnforcement') === '') { 94*fe49fd82SAndreas Gohr return []; 95*fe49fd82SAndreas Gohr } 96*fe49fd82SAndreas Gohr $domains = explode(',', trim($this->getConf('mailEnforcement'), ',')); 97*fe49fd82SAndreas Gohr return array_map('trim', $domains); 98d9818adbSMichael Große } 99d9818adbSMichael Große 100d9818adbSMichael Große /** 101d9818adbSMichael Große * @param string $mail 102*fe49fd82SAndreas Gohr * @param array $domains List of domains to check against (from getValidDomains or getEnforcedDomains) 103d9818adbSMichael Große * 104d9818adbSMichael Große * @return bool 105d9818adbSMichael Große */ 106*fe49fd82SAndreas Gohr public function checkMail($mail, array $domains) 10798a36116SAndreas Gohr { 108*fe49fd82SAndreas Gohr if (empty($domains)) return true; 109d9818adbSMichael Große 110*fe49fd82SAndreas Gohr foreach ($domains as $validDomain) { 111290e9b1fSAndreas Gohr if (str_ends_with($mail, $validDomain)) { 112d9818adbSMichael Große return true; 113d9818adbSMichael Große } 114d9818adbSMichael Große } 115d9818adbSMichael Große return false; 116d9818adbSMichael Große } 117f2e164b0SMichael Große 118f2e164b0SMichael Große /** 1193e7ac5b1SAndreas Gohr * Display an exception to the user 1203e7ac5b1SAndreas Gohr * 1213e7ac5b1SAndreas Gohr * @param Exception $e 122d1826331SAndreas Gohr * @param string $friendly - user friendly explanation if available 1233e7ac5b1SAndreas Gohr */ 124290e9b1fSAndreas Gohr public function showException(Exception $e, $friendly = '') 1253e7ac5b1SAndreas Gohr { 1263e7ac5b1SAndreas Gohr global $conf; 127c82ad624SAndreas Gohr 128d1826331SAndreas Gohr $msg = $e->getMessage(); 129d1826331SAndreas Gohr 130d1826331SAndreas Gohr // translate the message if possible, using context if available 131d1826331SAndreas Gohr $trans = $this->getLang($msg); 132d1826331SAndreas Gohr if ($trans) { 133290e9b1fSAndreas Gohr if ($e instanceof \dokuwiki\plugin\oauth\Exception) { 134d1826331SAndreas Gohr $context = $e->getContext(); 135d1826331SAndreas Gohr $trans = sprintf($trans, ...$context); 136d1826331SAndreas Gohr } 137d1826331SAndreas Gohr $msg = $trans; 138d1826331SAndreas Gohr } 139d1826331SAndreas Gohr 140d1826331SAndreas Gohr msg('OAuth: ' . $friendly . ' ' . hsc($msg), -1); 1413e7ac5b1SAndreas Gohr if ($conf['allowdebug']) { 142d1826331SAndreas Gohr $msg = get_class($e) . ' at ' . $e->getFile() . ':' . $e->getLine() . '<br>'; 143d1826331SAndreas Gohr $msg .= hsc($e->getTraceAsString()); 144d1826331SAndreas Gohr msg("<pre>$msg</pre>", -1); 1453e7ac5b1SAndreas Gohr } 1463e7ac5b1SAndreas Gohr } 14780852c15SAndreas Gohr} 148