1fca58076SAndreas Gohr<?php 28b7620a8SAndreas Gohr 38b7620a8SAndreas Gohruse dokuwiki\plugin\twofactor\Manager; 4*a01d09a8SAndreas Gohruse dokuwiki\plugin\twofactor\OtpField; 56c996db8SAndreas Gohruse dokuwiki\plugin\twofactor\Provider; 68b7620a8SAndreas Gohr 730625b49SAndreas Gohr/** 830625b49SAndreas Gohr * DokuWiki Plugin twofactor (Action Component) 930625b49SAndreas Gohr * 1030625b49SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 1130625b49SAndreas Gohr */ 12fca58076SAndreas Gohrclass action_plugin_twofactor_login extends DokuWiki_Action_Plugin 13fca58076SAndreas Gohr{ 14848a9be0SAndreas Gohr const TWOFACTOR_COOKIE = '2FA' . DOKU_COOKIE; 15848a9be0SAndreas Gohr 16fca58076SAndreas Gohr /** 17fca58076SAndreas Gohr * Registers the event handlers. 18fca58076SAndreas Gohr */ 19fca58076SAndreas Gohr public function register(Doku_Event_Handler $controller) 20fca58076SAndreas Gohr { 21a386a536SAndreas Gohr // check 2fa requirements and either move to profile or login handling 22a386a536SAndreas Gohr $controller->register_hook( 23a386a536SAndreas Gohr 'ACTION_ACT_PREPROCESS', 24a386a536SAndreas Gohr 'BEFORE', 25a386a536SAndreas Gohr $this, 26a386a536SAndreas Gohr 'handleActionPreProcess', 27a386a536SAndreas Gohr null, 285f8f561aSAndreas Gohr Manager::EVENT_PRIORITY 29a386a536SAndreas Gohr ); 30fca58076SAndreas Gohr 31a386a536SAndreas Gohr // display login form 32a386a536SAndreas Gohr $controller->register_hook( 33a386a536SAndreas Gohr 'TPL_ACT_UNKNOWN', 34a386a536SAndreas Gohr 'BEFORE', 35a386a536SAndreas Gohr $this, 36a386a536SAndreas Gohr 'handleLoginDisplay' 37a386a536SAndreas Gohr ); 38a386a536SAndreas Gohr 39210d81e3SAndreas Gohr // disable user in all non-main screens (media, detail, ajax, ...) 40210d81e3SAndreas Gohr $controller->register_hook( 41210d81e3SAndreas Gohr 'DOKUWIKI_INIT_DONE', 42210d81e3SAndreas Gohr 'BEFORE', 43210d81e3SAndreas Gohr $this, 44210d81e3SAndreas Gohr 'handleInitDone' 45210d81e3SAndreas Gohr ); 46fca58076SAndreas Gohr } 47fca58076SAndreas Gohr 48fca58076SAndreas Gohr /** 49a386a536SAndreas Gohr * Decide if any 2fa handling needs to be done for the current user 50a386a536SAndreas Gohr * 51a386a536SAndreas Gohr * @param Doku_Event $event 52a386a536SAndreas Gohr */ 53a386a536SAndreas Gohr public function handleActionPreProcess(Doku_Event $event) 54a386a536SAndreas Gohr { 555f8f561aSAndreas Gohr $manager = Manager::getInstance(); 565f8f561aSAndreas Gohr if (!$manager->isReady()) return; 57a386a536SAndreas Gohr 58a386a536SAndreas Gohr global $INPUT; 59a386a536SAndreas Gohr 60a386a536SAndreas Gohr // already in a 2fa login? 61a386a536SAndreas Gohr if ($event->data === 'twofactor_login') { 62848a9be0SAndreas Gohr if ($this->verify( 63848a9be0SAndreas Gohr $INPUT->str('2fa_code'), 64848a9be0SAndreas Gohr $INPUT->str('2fa_provider'), 65848a9be0SAndreas Gohr $INPUT->bool('sticky') 66848a9be0SAndreas Gohr )) { 67a386a536SAndreas Gohr $event->data = 'show'; 68848a9be0SAndreas Gohr return; 69a386a536SAndreas Gohr } else { 70a386a536SAndreas Gohr // show form 71a386a536SAndreas Gohr $event->preventDefault(); 72a386a536SAndreas Gohr return; 73a386a536SAndreas Gohr } 74a386a536SAndreas Gohr } 75a386a536SAndreas Gohr 76857c5abcSAndreas Gohr // clear cookie on logout 77857c5abcSAndreas Gohr if ($event->data === 'logout') { 78857c5abcSAndreas Gohr $this->deAuth(); 79857c5abcSAndreas Gohr return; 80857c5abcSAndreas Gohr } 81857c5abcSAndreas Gohr 82a386a536SAndreas Gohr // authed already, continue 83a386a536SAndreas Gohr if ($this->isAuthed()) { 84a386a536SAndreas Gohr return; 85a386a536SAndreas Gohr } 86a386a536SAndreas Gohr 875f8f561aSAndreas Gohr if (count($manager->getUserProviders())) { 88a386a536SAndreas Gohr // user has already 2fa set up - they need to authenticate before anything else 89a386a536SAndreas Gohr $event->data = 'twofactor_login'; 90a386a536SAndreas Gohr $event->preventDefault(); 91a386a536SAndreas Gohr $event->stopPropagation(); 92a386a536SAndreas Gohr return; 93a386a536SAndreas Gohr } 94a386a536SAndreas Gohr 955f8f561aSAndreas Gohr if ($manager->isRequired()) { 96a386a536SAndreas Gohr // 2fa is required - they need to set it up now 97a386a536SAndreas Gohr // this will be handled by action/profile.php 98a386a536SAndreas Gohr $event->data = 'twofactor_profile'; 99a386a536SAndreas Gohr } 100a386a536SAndreas Gohr 101a386a536SAndreas Gohr // all good. proceed 102a386a536SAndreas Gohr } 103a386a536SAndreas Gohr 104a386a536SAndreas Gohr /** 105a386a536SAndreas Gohr * Show a 2fa login screen 106a386a536SAndreas Gohr * 107a386a536SAndreas Gohr * @param Doku_Event $event 108a386a536SAndreas Gohr */ 109a386a536SAndreas Gohr public function handleLoginDisplay(Doku_Event $event) 110a386a536SAndreas Gohr { 111a386a536SAndreas Gohr if ($event->data !== 'twofactor_login') return; 1125f8f561aSAndreas Gohr $manager = Manager::getInstance(); 1135f8f561aSAndreas Gohr if (!$manager->isReady()) return; 1145f8f561aSAndreas Gohr 115a386a536SAndreas Gohr $event->preventDefault(); 116a386a536SAndreas Gohr $event->stopPropagation(); 117a386a536SAndreas Gohr 118a386a536SAndreas Gohr global $INPUT; 119c9e42a8dSAndreas Gohr global $ID; 120c9e42a8dSAndreas Gohr 121a386a536SAndreas Gohr $providerID = $INPUT->str('2fa_provider'); 1225f8f561aSAndreas Gohr $providers = $manager->getUserProviders(); 1235f8f561aSAndreas Gohr $provider = $providers[$providerID] ?? $manager->getUserDefaultProvider(); 124b6119621SAndreas Gohr // remove current provider from list 125b6119621SAndreas Gohr unset($providers[$provider->getProviderID()]); 126a386a536SAndreas Gohr 1270407d282SAndreas Gohr echo '<div class="plugin_twofactor_login">'; 1280407d282SAndreas Gohr echo inlineSVG(__DIR__ . '/../admin.svg'); 129857c5abcSAndreas Gohr echo $this->locale_xhtml('login'); 130848a9be0SAndreas Gohr $form = new dokuwiki\Form\Form(['method' => 'POST']); 131848a9be0SAndreas Gohr $form->setHiddenField('do', 'twofactor_login'); 132a386a536SAndreas Gohr $form->setHiddenField('2fa_provider', $provider->getProviderID()); 133a386a536SAndreas Gohr $form->addFieldsetOpen($provider->getLabel()); 134a386a536SAndreas Gohr try { 135a386a536SAndreas Gohr $code = $provider->generateCode(); 136a386a536SAndreas Gohr $info = $provider->transmitMessage($code); 137a386a536SAndreas Gohr $form->addHTML('<p>' . hsc($info) . '</p>'); 138*a01d09a8SAndreas Gohr $form->addElement(new OtpField('2fa_code')); 139*a01d09a8SAndreas Gohr $form->addCheckbox('sticky', 'Remember this browser'); // FIXME localize 1400407d282SAndreas Gohr $form->addTagOpen('div')->addClass('buttons'); 141*a01d09a8SAndreas Gohr $form->addButton('2fa', $this->getLang('btn_confirm'))->attr('type', 'submit'); 1420407d282SAndreas Gohr $form->addTagClose('div'); 1435f8f561aSAndreas Gohr } catch (Exception $e) { 144a386a536SAndreas Gohr msg(hsc($e->getMessage()), -1); // FIXME better handling 145a386a536SAndreas Gohr } 146a386a536SAndreas Gohr $form->addFieldsetClose(); 147a386a536SAndreas Gohr 148a386a536SAndreas Gohr if (count($providers)) { 14978279978SAndreas Gohr $form->addFieldsetOpen('Alternative methods')->addClass('list'); 15078279978SAndreas Gohr $form->addTagOpen('ul'); 151a386a536SAndreas Gohr foreach ($providers as $prov) { 152c9e42a8dSAndreas Gohr $url = wl($ID, [ 153c9e42a8dSAndreas Gohr 'do' => 'twofactor_login', 154c9e42a8dSAndreas Gohr '2fa_provider' => $prov->getProviderID(), 155c9e42a8dSAndreas Gohr ]); 15678279978SAndreas Gohr $form->addHTML( 15778279978SAndreas Gohr '<li><div class="li"><a href="' . $url . '">' . hsc($prov->getLabel()) . '</a></div></li>' 15878279978SAndreas Gohr ); 159a386a536SAndreas Gohr } 16078279978SAndreas Gohr 16178279978SAndreas Gohr $form->addTagClose('ul'); 162a386a536SAndreas Gohr $form->addFieldsetClose(); 163a386a536SAndreas Gohr } 164a386a536SAndreas Gohr 165a386a536SAndreas Gohr echo $form->toHTML(); 1660407d282SAndreas Gohr echo '</div>'; 167a386a536SAndreas Gohr } 168a386a536SAndreas Gohr 169a386a536SAndreas Gohr /** 170210d81e3SAndreas Gohr * Remove user info from non-main entry points while we wait for 2fa 171210d81e3SAndreas Gohr * 172210d81e3SAndreas Gohr * @param Doku_Event $event 173210d81e3SAndreas Gohr */ 174210d81e3SAndreas Gohr public function handleInitDone(Doku_Event $event) 175210d81e3SAndreas Gohr { 176210d81e3SAndreas Gohr global $INPUT; 177210d81e3SAndreas Gohr 178210d81e3SAndreas Gohr if (!(Manager::getInstance())->isReady()) return; 179210d81e3SAndreas Gohr if (basename($INPUT->server->str('SCRIPT_NAME')) == DOKU_SCRIPT) return; 180210d81e3SAndreas Gohr if ($this->isAuthed()) return; 181210d81e3SAndreas Gohr 182210d81e3SAndreas Gohr // temporarily remove user info from environment 183210d81e3SAndreas Gohr $INPUT->server->remove('REMOTE_USER'); 184210d81e3SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']); 185210d81e3SAndreas Gohr unset($GLOBALS['USERINFO']); 186210d81e3SAndreas Gohr } 187210d81e3SAndreas Gohr 188210d81e3SAndreas Gohr /** 189a386a536SAndreas Gohr * Has the user already authenticated with the second factor? 190a386a536SAndreas Gohr * @return bool 191a386a536SAndreas Gohr */ 192a386a536SAndreas Gohr protected function isAuthed() 193a386a536SAndreas Gohr { 194848a9be0SAndreas Gohr if (!isset($_COOKIE[self::TWOFACTOR_COOKIE])) return false; 195848a9be0SAndreas Gohr $data = unserialize(base64_decode($_COOKIE[self::TWOFACTOR_COOKIE])); 196848a9be0SAndreas Gohr if (!is_array($data)) return false; 1976c996db8SAndreas Gohr list($providerID, $hash,) = $data; 198848a9be0SAndreas Gohr 199848a9be0SAndreas Gohr try { 2005f8f561aSAndreas Gohr $provider = (Manager::getInstance())->getUserProvider($providerID); 2016c996db8SAndreas Gohr if ($this->cookieHash($provider) !== $hash) return false; 202848a9be0SAndreas Gohr return true; 2035f8f561aSAndreas Gohr } catch (Exception $ignored) { 204a386a536SAndreas Gohr return false; 205a386a536SAndreas Gohr } 206848a9be0SAndreas Gohr } 207a386a536SAndreas Gohr 208a386a536SAndreas Gohr /** 209857c5abcSAndreas Gohr * Deletes the cookie 210857c5abcSAndreas Gohr * 211857c5abcSAndreas Gohr * @return void 212857c5abcSAndreas Gohr */ 213857c5abcSAndreas Gohr protected function deAuth() 214857c5abcSAndreas Gohr { 215857c5abcSAndreas Gohr global $conf; 216857c5abcSAndreas Gohr 217857c5abcSAndreas Gohr $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 218857c5abcSAndreas Gohr $time = time() - 60 * 60 * 24 * 365; // one year in the past 219857c5abcSAndreas Gohr setcookie(self::TWOFACTOR_COOKIE, null, $time, $cookieDir, '', ($conf['securecookie'] && is_ssl()), true); 220857c5abcSAndreas Gohr } 221857c5abcSAndreas Gohr 222857c5abcSAndreas Gohr /** 223a386a536SAndreas Gohr * Verify a given code 224a386a536SAndreas Gohr * 225a386a536SAndreas Gohr * @return bool 226a386a536SAndreas Gohr * @throws Exception 227a386a536SAndreas Gohr */ 228848a9be0SAndreas Gohr protected function verify($code, $providerID, $sticky) 229a386a536SAndreas Gohr { 230848a9be0SAndreas Gohr global $conf; 231848a9be0SAndreas Gohr 232a386a536SAndreas Gohr if (!$code) return false; 233a386a536SAndreas Gohr if (!$providerID) return false; 2345f8f561aSAndreas Gohr $provider = (Manager::getInstance())->getUserProvider($providerID); 235a386a536SAndreas Gohr $ok = $provider->checkCode($code); 23616ed3964SAndreas Gohr if (!$ok) return false; 237a386a536SAndreas Gohr 238848a9be0SAndreas Gohr // store cookie 2396c996db8SAndreas Gohr $hash = $this->cookieHash($provider); 2406c996db8SAndreas Gohr $data = base64_encode(serialize([$providerID, $hash, time()])); 241848a9be0SAndreas Gohr $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 242848a9be0SAndreas Gohr $time = $sticky ? (time() + 60 * 60 * 24 * 365) : 0; //one year 243848a9be0SAndreas Gohr setcookie(self::TWOFACTOR_COOKIE, $data, $time, $cookieDir, '', ($conf['securecookie'] && is_ssl()), true); 244a386a536SAndreas Gohr 245a386a536SAndreas Gohr return true; 246a386a536SAndreas Gohr } 2476c996db8SAndreas Gohr 2486c996db8SAndreas Gohr /** 2496c996db8SAndreas Gohr * Create a hash that validates the cookie 2506c996db8SAndreas Gohr * 2516c996db8SAndreas Gohr * @param Provider $provider 2526c996db8SAndreas Gohr * @return string 2536c996db8SAndreas Gohr */ 2546c996db8SAndreas Gohr protected function cookieHash($provider) 2556c996db8SAndreas Gohr { 2566c996db8SAndreas Gohr return sha1(join("\n", [ 2576c996db8SAndreas Gohr $provider->getProviderID(), 2585f8f561aSAndreas Gohr (Manager::getInstance())->getUser(), 2596c996db8SAndreas Gohr $provider->getSecret(), 2606c996db8SAndreas Gohr auth_browseruid(), 2616c996db8SAndreas Gohr auth_cookiesalt(false, true), 2626c996db8SAndreas Gohr ])); 2636c996db8SAndreas Gohr } 264fca58076SAndreas Gohr} 265