*/ class action_plugin_oauth_login extends ActionPlugin { /** @var helper_plugin_oauth */ protected $hlp; /** * Constructor * * Initializes the helper */ public function __construct() { $this->hlp = plugin_load('helper', 'oauth'); } /** * Registers a callback function for a given event * * @param EventHandler $controller DokuWiki's event controller object * @return void */ public function register(EventHandler $controller) { global $conf; if ($conf['authtype'] != 'oauth') return; $conf['profileconfirm'] = false; // password confirmation doesn't work with oauth only users $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'handleStart'); $controller->register_hook('HTML_LOGINFORM_OUTPUT', 'BEFORE', $this, 'handleOldLoginForm'); // @deprecated $controller->register_hook('FORM_LOGIN_OUTPUT', 'BEFORE', $this, 'handleLoginForm'); $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleDoLogin'); $controller->register_hook('ACTION_DENIED_TPLCONTENT', 'BEFORE', $this, 'handleDeniedForm'); } /** * Start an oAuth login or restore environment after successful login * * @param Event $event * @return void */ public function handleStart(Event $event) { global $INPUT; // see if a login needs to be started $servicename = $INPUT->str('oauthlogin'); if (!$servicename) return; try { $om = new OAuthManager(); $om->startFlow($servicename); } catch (TokenResponseException | Exception $e) { $this->hlp->showException($e, 'login failed'); } } /** * Add the oAuth login links to login form * * @param Event $event event object by reference * @return void * @deprecated can be removed in the future */ public function handleOldLoginForm(Event $event) { /** @var Doku_Form $form */ $form = $event->data; $html = $this->prepareLoginButtons(); if (!$html) return; // remove login form if single service is set $singleService = $this->getConf('singleService'); if ($singleService) { $form->_content = []; } $form->_content[] = form_openfieldset( [ '_legend' => $this->getLang('loginwith'), 'class' => 'plugin_oauth', ] ); $form->_content[] = $html; $form->_content[] = form_closefieldset(); } /** * Add the oAuth login links to login form * * @param Event $event event object by reference * @return void * @deprecated can be removed in the future */ public function handleLoginForm(Event $event) { /** @var Form $form */ $form = $event->data; $html = $this->prepareLoginButtons(); if (!$html) return; // remove login form if single service is set $singleService = $this->getConf('singleService'); if ($singleService) { do { $form->removeElement(0); } while ($form->elementCount() > 0); } $form->addFieldsetOpen($this->getLang('loginwith'))->addClass('plugin_oauth'); $form->addHTML($html); $form->addFieldsetClose(); } /** * Create HTML for the various login buttons * * @return string the HTML */ protected function prepareLoginButtons() { $html = ''; $validDomains = $this->hlp->getValidDomains(); if (count($validDomains) > 0) { $html .= '
' . sprintf( $this->getLang('eMailRestricted'), '' . implode(', ', $validDomains) . '' ) . '
'; } $html .= '