3 * Piwik - Open source web analytics
5 * @link http://piwik.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later
9 * @category Piwik_Plugins
13 namespace Piwik\Plugins\CASLogin;
20 use Piwik\Plugins\UsersManager\API;
23 * Class that implements an authentication mechanism via CAS (Central Authentication Services)
25 * @package Piwik_CASLogin
27 class Auth implements \Piwik\Auth
29 protected $login = null;
30 protected $token_auth = null;
32 public function getName()
37 public function initSession($login, $md5Password, $rememberMe) {}
39 public function authenticate()
43 require_once PIWIK_INCLUDE_PATH . '/plugins/CASLogin/CAS/CAS.php';
47 // What happens here: in some piwik functionality, some additional API-style calls are
48 // made from a controller action, where the authenticate() method will be called *again*.
49 // This happens for instance when an admin changes some permissions in Settings->Users.
50 // The first authenticate() is from the page, and the second is due to an API call.
51 // This checks if there was already a phpcas instance already initialized, otherwize
52 // phpCAS::client() would fail.
53 global $PHPCAS_CLIENT;
54 if(!is_object($PHPCAS_CLIENT)) {
56 constant( Config::getInstance()->caslogin['protocol'] ),
57 Config::getInstance()->caslogin['host'],
58 (integer) Config::getInstance()->caslogin['port'],
64 // no SSL validation for the CAS server
65 \phpCAS::setNoCasServerValidation();
67 // Handle single signout requests from CAS server
68 \phpCAS::handleLogoutRequests();
70 // force CAS authentication only if it has been requested by action argument
71 $action = Piwik::getAction();
73 $auth = \phpCAS::checkAuthentication();
75 if($action == 'redirectToCAS') {
76 phpCAS::forceAuthentication();
79 if($action != 'login' && Piwik::getModule() != 'CoreUpdater') {
80 Piwik::redirectToModule('CASLogin', 'login');
82 } elseif($action == 'redirectToCAS') {
83 phpCAS::forceAuthentication();
85 return new AuthResult( AuthResult::FAILURE, $user, NULL );
89 // Additional Attributes
90 // For future retrieval of attributes; they _might_ be of some use, but are highly
91 // dependable on a specific installation. CAS|piwik hackers can do some magic
92 // here with SAML attributes etc.
94 foreach (\phpCAS::getAttributes() as $key => $value) {
95 // syslog(LOG_DEBUG, "attribute: $key - ". print_r($value, true));
99 if (isset($_SESSION['phpCAS']) && isset($_SESSION['phpCAS']['user'])) {
100 $user = $_SESSION['phpCAS']['user'];
104 $db_user = Db::fetchRow('SELECT login, superuser_access FROM '.Common::prefixTable('user').' WHERE login = ?',
107 if($db_user === null) {
108 // ***User Autocreate***
109 // We can either add the authenticated but not-yet-authorized user to the piwik users
110 // database, or ignore that.
111 // TODO: make this a config option
112 $this->_populateDb($user);
117 $login = $db_user['login'];
118 $superuser = $db_user['superuser_access'];
123 $code = AuthResult::SUCCESS_SUPERUSER_AUTH_CODE;
124 else $code = AuthResult::SUCCESS;
125 return new AuthResult($code, $login, NULL );
129 return new AuthResult( AuthResult::FAILURE, $user, NULL );
132 public function setLogin($login)
134 $this->login = $login;
137 public function setTokenAuth($token_auth)
139 $this->token_auth = $token_auth;
143 * This method is used to inject user into Piwik's tables.
144 * @todo Alias could be the 'cn' returned from CAS attributes.
146 private function _populateDb($user)
149 $dummy = md5('abcd1234');
150 if ($this->_helper_userExists($user)) {
151 $this->_helper_updateUser($user, $dummy, '', $user);
153 $this->_helper_addUser($user, $dummy, '', $user);
158 ///// The following methods are taken from Piwik's UserManager, but in order to inject data into piwik's user and access tables, we need
159 ///// to make sure we don't wreck things. The UserManager API uses authenticate() to check if we're eligable to look this up,
160 ///// soi we can't use it - we need superuser permissions anyway.
162 ///// Warning - these methods are of course under Piwik's license.
163 private function _helper_userExists($name)
165 $count = Db::fetchOne("SELECT count(*)
166 FROM ".Common::prefixTable("user"). "
167 WHERE login = ?", $name);
171 private function _helper_updateUser( $userLogin, $password = false, $email = false, $alias = false )
173 $token_auth = API::getTokenAuth($userLogin, $password);
175 Db::get()->update( Common::prefixTable("user"),
177 'password' => $password,
180 'token_auth' => $token_auth,
182 "login = '$userLogin'"
186 private function _helper_addUser( $userLogin, $password, $email, $alias = false )
188 $token_auth = API::getTokenAuth($userLogin, $password);
190 Db::get()->insert( Common::prefixTable("user"), array(
191 'login' => $userLogin,
192 'password' => $password,
195 'token_auth' => $token_auth,