180852c15SAndreas Gohr<?php 2*290e9b1fSAndreas Gohr 3*290e9b1fSAndreas Gohr// phpcs:disable PSR1.Files.SideEffects.FoundWithSymbols 4*290e9b1fSAndreas 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 12*290e9b1fSAndreas Gohruse dokuwiki\Extension\Plugin; 1398a36116SAndreas Gohruse dokuwiki\Extension\Event; 1404a78b87SAndreas Gohruse dokuwiki\plugin\oauth\Adapter; 1580852c15SAndreas Gohr 16*290e9b1fSAndreas 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 */ 21*290e9b1fSAndreas 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) { 68*290e9b1fSAndreas Gohr $services = array_filter($services, static fn($service) => 6904a78b87SAndreas Gohr /** @var Adapter $service */ 70*290e9b1fSAndreas 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') === '') { 82*290e9b1fSAndreas Gohr return []; 833c0c1b14SMichael Große } 84d9818adbSMichael Große $validDomains = explode(',', trim($this->getConf('mailRestriction'), ',')); 85*290e9b1fSAndreas Gohr return array_map('trim', $validDomains); 86d9818adbSMichael Große } 87d9818adbSMichael Große 88d9818adbSMichael Große /** 89d9818adbSMichael Große * @param string $mail 90d9818adbSMichael Große * 91d9818adbSMichael Große * @return bool 92d9818adbSMichael Große */ 9398a36116SAndreas Gohr public function checkMail($mail) 9498a36116SAndreas Gohr { 9539730c7eSAnna Dabrowska $validDomains = $this->getValidDomains(); 9639730c7eSAnna Dabrowska if (empty($validDomains)) return true; 97d9818adbSMichael Große 9839730c7eSAnna Dabrowska foreach ($validDomains as $validDomain) { 99*290e9b1fSAndreas Gohr if (str_ends_with($mail, $validDomain)) { 100d9818adbSMichael Große return true; 101d9818adbSMichael Große } 102d9818adbSMichael Große } 103d9818adbSMichael Große return false; 104d9818adbSMichael Große } 105f2e164b0SMichael Große 106f2e164b0SMichael Große /** 1073e7ac5b1SAndreas Gohr * Display an exception to the user 1083e7ac5b1SAndreas Gohr * 1093e7ac5b1SAndreas Gohr * @param Exception $e 110d1826331SAndreas Gohr * @param string $friendly - user friendly explanation if available 1113e7ac5b1SAndreas Gohr */ 112*290e9b1fSAndreas Gohr public function showException(Exception $e, $friendly = '') 1133e7ac5b1SAndreas Gohr { 1143e7ac5b1SAndreas Gohr global $conf; 115c82ad624SAndreas Gohr 116d1826331SAndreas Gohr $msg = $e->getMessage(); 117d1826331SAndreas Gohr 118d1826331SAndreas Gohr // translate the message if possible, using context if available 119d1826331SAndreas Gohr $trans = $this->getLang($msg); 120d1826331SAndreas Gohr if ($trans) { 121*290e9b1fSAndreas Gohr if ($e instanceof \dokuwiki\plugin\oauth\Exception) { 122d1826331SAndreas Gohr $context = $e->getContext(); 123d1826331SAndreas Gohr $trans = sprintf($trans, ...$context); 124d1826331SAndreas Gohr } 125d1826331SAndreas Gohr $msg = $trans; 126d1826331SAndreas Gohr } 127d1826331SAndreas Gohr 128d1826331SAndreas Gohr msg('OAuth: ' . $friendly . ' ' . hsc($msg), -1); 1293e7ac5b1SAndreas Gohr if ($conf['allowdebug']) { 130d1826331SAndreas Gohr $msg = get_class($e) . ' at ' . $e->getFile() . ':' . $e->getLine() . '<br>'; 131d1826331SAndreas Gohr $msg .= hsc($e->getTraceAsString()); 132d1826331SAndreas Gohr msg("<pre>$msg</pre>", -1); 1333e7ac5b1SAndreas Gohr } 1343e7ac5b1SAndreas Gohr } 13580852c15SAndreas Gohr} 136