diff --git a/action.php b/action.php --- a/action.php +++ b/action.php @@ -1,113 +1,113 @@ getConf("g2fa_enable"); if($enable===1) { $controller->register_hook('HTML_LOGINFORM_OUTPUT', 'BEFORE', $this, 'two_fa_login_form', array()); $controller->register_hook('HTML_UPDATEPROFILEFORM_OUTPUT', 'AFTER', $this, 'handle_profile_form', array()); } } /** * Handles the login form rendering. */ function two_fa_login_form(&$event, $param) { global $conf; // Show login form above submit button (and above remember me option, if enabled) $pos = $event->data->findElementByAttribute('type', 'submit'); if($conf["rememberme"]) $pos = $pos-1; $event->data->insertElement($pos, form_makePasswordField('t', $this->getLang('g2fa'), '', 'block')); } function handle_profile_form(&$event, $param) { global $ID; global $INPUT; $fn = $INPUT->param('fn'); if(is_array($fn)) { $cmd = key($fn); $param = is_array($fn[$cmd]) ? key($fn[$cmd]) : null; } else { $cmd = $fn; $param = null; } $th = new TokenHelper(); $secret = $th->getSecret($_SERVER['REMOTE_USER']); $form = new Doku_Form($ID); $form->startFieldset($this->getLang('g2fa_fieldset')); $form->addHidden('do', 'profile'); $form->addHidden('g2fa', '1'); $reveal = false; switch($cmd) { case "gensecret" : if($th->createTokenForUser($_SERVER['REMOTE_USER'])) { msg($this->getLang('g2fa_created')); $secret = $th->getSecret($_SERVER['REMOTE_USER']); } break; case "update": if($INPUT->param('secret') == "********") { msg($this->getLang('g2fa_notchanged')); break; } if($th->saveToken($_SERVER['REMOTE_USER'], $INPUT->param('secret'))) msg($this->getLang('g2fa_updated')); else msg($this->getLang('g2fa_notchanged')); $secret = $th->getSecret($_SERVER['REMOTE_USER']); break; case "showqr" : if($secret != '') { $ga = new PHPGangsta_GoogleAuthenticator(); - $url = $ga->getQRCodeGoogleUrl('DokuWiki for '.$_SERVER['REMOTE_USER'], $secret); + $url = $ga->getQRCodeGoogleUrl(urlencode('DokuWiki:'.$_SERVER['REMOTE_USER']), $secret); $form->addElement(form_makeTag('img', array('src' => $url, 'alt' => 'Google 2FA QR Image'))); $form->addElement(form_makeTag('br')); $reveal = true; } break; case "delsecret" : if($th->deleteTokenForUser($_SERVER['REMOTE_USER'])) { msg($this->getLang('g2fa_removed')); $secret = ''; } break; } if($reveal) { $form->addElement(form_makeTextField('secret', $secret, $this->getLang('secret'), '', 'block', array('size'=>'50'))); } else { $form->addElement(form_makeTextField('secret', '********', $this->getLang('secret'), '', 'block', array('size'=>'50'))); } $form->addElement(form_makeButton('submit', '', $this->getLang('showqr'), array('name' => 'fn[showqr]', 'disabled' => $secret == '' ? 'disabled' : ''))); $form->addElement(form_makeTag('br')); $form->addElement(form_makeButton('submit', '', $this->getLang('generate'), array('name' => 'fn[gensecret]'))); $form->addElement(form_makeButton('submit', '', $this->getLang('update'), array('name' => 'fn[update]'))); $form->addElement(form_makeTag('br')); $form->addElement(form_makeButton('submit', '', $this->getLang('delete'), array('name' => 'fn[delsecret]', 'disabled' => $secret == '' ? 'disabled' : '' ))); $form->endFieldset(); html_form('g2fa', $form); } } ?> diff --git a/admin.php b/admin.php --- a/admin.php +++ b/admin.php @@ -1,239 +1,239 @@ */ // must be run within Dokuwiki if(!defined('DOKU_INC')) die(); require_once(dirname(__FILE__).'/GoogleAuthenticator.php'); require_once(dirname(__FILE__).'/TokenHelper.php'); /** * All DokuWiki plugins to extend the admin function * need to inherit from this class */ class admin_plugin_authg2fa extends DokuWiki_Admin_Plugin { protected $_auth = null; protected $_tokens = array(); protected $_start = 0; protected $_pagesize = 20; protected $_filter = array(); protected $_disabled = ""; protected $_user_total = 0; protected $_tokenHelper = null; protected $_unhide = ""; /** * Constructor */ public function admin_plugin_authg2fa() { /** @var DokuWiki_Auth_Plugin $auth */ global $auth; $this->setupLocale(); if (!isset($auth)) { return; //$this->_disabled = $this->lang['noauth']; } else if (!$auth->canDo('getUsers')) { return; //$this->_disabled = $this->lang['nosupport']; } else { // we're good to go $this->_auth = & $auth; } $this->_tokenHelper = new TokenHelper(); $this->_tokens = $this->_tokenHelper->getTokens(); } public function handle() { global $INPUT; if(is_null($this->_auth)) return false; if(!isset($_REQUEST['fn']) || !checkSecurityToken()) return; // extract the command and any specific parameters // submit button name is of the form - fn[cmd][param(s)] $fn = $INPUT->param('fn'); if (is_array($fn)) { $cmd = key($fn); $param = is_array($fn[$cmd]) ? key($fn[$cmd]) : null; } else { $cmd = $fn; $param = null; } if ($cmd != "search") { $this->_start = $INPUT->int('start', 0); $this->_filter = $this->_retrieveFilter(); } switch($cmd) { case "csecret" : $this->_tokenHelper->createTokenForUser($param); $this->_tokens = $this->_tokenHelper->getTokens(); break; case "nsecret" : $this->_createTokenForAllUsers(); break; case "dsecret" : $this->_tokenHelper->deleteTokenForUser($param); $this->_tokens = $this->_tokenHelper->getTokens(); break; case "ssecret" : $this->_unhide = $param; break; } $this->_user_total = $this->_auth->canDo('getUserCount') ? $this->_auth->getUserCount($this->_filter) : -1; // page handling switch($cmd){ case 'start' : $this->_start = 0; break; case 'prev' : $this->_start -= $this->_pagesize; break; case 'next' : $this->_start += $this->_pagesize; break; case 'last' : $this->_start = $this->_user_total; break; } $this->_validatePagination(); return true; } public function html() { global $ID; $user_list = $this->_auth->retrieveUsers($this->_start, $this->_pagesize, $this->_filter); $page_buttons = $this->_pagination(); if($this->_unhide != "") { if(isset($this->_tokens[$this->_unhide])) { $ga = new PHPGangsta_GoogleAuthenticator(); - $url = $ga->getQRCodeGoogleUrl('DokuWiki for '.$this->_unhide, $this->_tokens[$this->_unhide]); + $url = $ga->getQRCodeGoogleUrl(urlencode('DokuWiki:'.$this->_unhide), $this->_tokens[$this->_unhide]); ptln('Showing QR Code for user '.$this->_unhide.':
'); ptln('Google 2FA QR Image
'); } } ptln("
"); formSecurityToken(); ptln("
"); ptln(""); ptln(''); foreach($user_list as $user => $userinfo) { extract($userinfo); ptln(''); ptln(''); if(isset($this->_tokens[$user])) $secret = $this->_tokens[$user]; else $secret = ""; if($this->_unhide == hsc($user)) { ptln(''); $this->_unhide = ""; } else { if($secret != "") { ptln(''); } else { ptln(''); } } ptln(""); ptln(''); } // ptln('
UserSecretAction
'.hsc($user).''.$secret.'********"); ptln(""); ptln(""); ptln(""); ptln("
'); ptln(""); ptln(""); ptln(""); ptln(" "); ptln(" "); ptln(" "); ptln(" "); ptln(" "); ptln(" "); ptln(" "); ptln(" "); ptln(" "); ptln(""); ptln(""); ptln(""); ptln("
"); return true; } /** * Validate and improve the pagination values */ protected function _validatePagination() { if ($this->_start >= $this->_user_total) { $this->_start = $this->_user_total - $this->_pagesize; } if ($this->_start < 0) $this->_start = 0; $this->_last = min($this->_user_total, $this->_start + $this->_pagesize); } /** * Get the current search terms * * @return array */ protected function _retrieveFilter() { global $INPUT; $t_filter = $INPUT->arr('filter'); // messy, but this way we ensure we aren't getting any additional crap from malicious users $filter = array(); if (isset($t_filter['user'])) $filter['user'] = $t_filter['user']; if (isset($t_filter['name'])) $filter['name'] = $t_filter['name']; if (isset($t_filter['mail'])) $filter['mail'] = $t_filter['mail']; if (isset($t_filter['grps'])) $filter['grps'] = $t_filter['grps']; return $filter; } /** * Return an array of strings to enable/disable pagination buttons * * @return array with enable/disable attributes */ protected function _pagination() { $disabled = 'disabled="disabled"'; $buttons['start'] = $buttons['prev'] = ($this->_start == 0) ? $disabled : ''; if ($this->_user_total == -1) { $buttons['last'] = $disabled; $buttons['next'] = ''; } else { $buttons['last'] = $buttons['next'] = (($this->_start + $this->_pagesize) >= $this->_user_total) ? $disabled : ''; } return $buttons; } protected function _createTokenForAllUsers() { $user_list = $this->_auth->retrieveUsers($this->_start, $this->_pagesize, $this->_filter); foreach($user_list as $user => $user_info) { if(!isset($this->_tokens[$user])) $this->_tokenHelper->createTokenForUser($user); } $this->_tokens = $this->_tokenHelper->getTokens(); } } diff --git a/conf/default.php b/conf/default.php --- a/conf/default.php +++ b/conf/default.php @@ -1,6 +1,6 @@ + diff --git a/conf/metadata.php b/conf/metadata.php --- a/conf/metadata.php +++ b/conf/metadata.php @@ -1,6 +1,6 @@ + diff --git a/plugin.info.txt b/plugin.info.txt --- a/plugin.info.txt +++ b/plugin.info.txt @@ -1,7 +1,7 @@ base authg2fa author Andreas Boehler email dev@aboehler.at -date 2014-06-11 +date 2016-11-04 name 2Factor Google Authenticator Plugin desc Adds Google 2Factor Authentication to DokuWiki local password storage. url https://www.dokuwiki.org/plugin:authg2fa