X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=classes%2Fxorgsession.php;h=84128af3beb42844fb1f04bba2c99da342a7ec16;hb=54cccfcc1f4e128e0a2c7f0e23c20e10107b9504;hp=f7573377379b6f2fb8e6d1869c06d4b0bd5f572d;hpb=3f95a7bd17cd6cd7b218f12787276a485f496f1d;p=platal.git diff --git a/classes/xorgsession.php b/classes/xorgsession.php index f757337..84128af 100644 --- a/classes/xorgsession.php +++ b/classes/xorgsession.php @@ -1,6 +1,6 @@ fetchOneRow()) { - $expected_response = sha1("$uname:$password:" . S::v('challenge')); - /* XXX: Deprecates len(password) > 10 conversion */ + if ($user === null) { + Platal::page()->trigError(self::TEXT_INVALID_LOGIN); + return false; + } else { + $password = $user->password(); + $expected_response = sha1("$login:$password:" . S::v('challenge')); + /* Deprecates len(password) > 10 conversion. */ if ($response != $expected_response) { if (!S::logged()) { - Platal::page()->trigError('Mot de passe ou nom d\'utilisateur invalide'); + Platal::page()->trigError(self::TEXT_INVALID_LOGIN); } else { - Platal::page()->trigError('Mot de passe invalide'); + Platal::page()->trigError(self::TEXT_INVALID_PASS); } S::logger($uid)->log('auth_fail', 'bad password'); - return null; + return false; } - return $uid; + return true; } - Platal::page()->trigError('Mot de passe ou nom d\'utilisateur invalide'); - return null; } @@ -118,7 +118,7 @@ class XorgSession extends PlSession if (!S::logged()) { S::set('auth', AUTH_COOKIE); } - return User::getSilentWithValues(null, array('user_id' => S::i('auth_by_cookie'))); + return User::getSilentWithUID(S::i('auth_by_cookie')); } @@ -131,56 +131,32 @@ class XorgSession extends PlSession /** We come from an authentication form. */ if (S::suid()) { - $login = $uname = S::suid('uid'); - $redirect = false; + $login = S::suid('uid'); } else { - $uname = Env::v('username'); - if (Env::v('domain') == "alias") { - $res = XDB::query('SELECT redirect - FROM virtual - INNER JOIN virtual_redirect USING(vid) - WHERE alias LIKE {?}', - $uname . '@' . $globals->mail->alias_dom); - $redirect = $res->fetchOneCell(); - if ($redirect) { - $login = substr($redirect, 0, strpos($redirect, '@')); - } else { - $login = ''; - } - } else { - $login = $uname; - $redirect = false; - } + $login = Post::v('username'); } - $uid = $this->checkPassword($uname, $login, Post::v('response'), (!$redirect && is_numeric($uname)) ? 'id' : 'alias'); - if (!is_null($uid) && S::suid()) { - if (S::suid('uid') == $uid) { - $uid = S::i('uid'); - } else { - $uid = null; - } + $user = User::getSilent($login); + + $success = $this->checkPassword($login, $user, Post::v('response')); + + if (!is_null($user) && S::suid()) { + $success = (S::suid('uid') == $user->id()); + } else { + $success = $this->checkPassword($login, $user, Post::v('response')); } - if (!is_null($uid)) { + + if ($success) { S::set('auth', AUTH_MDP); - if (!S::suid()) { - if (Post::has('domain')) { - if (($domain = Post::v('domain', 'login')) == 'alias') { - Cookie::set('domain', 'alias', 300); - } else { - Cookie::kill('domain'); - } - } - } S::kill('challenge'); - S::logger($uid)->log('auth_ok'); + S::logger($user->id())->log('auth_ok'); } - return User::getSilentWithValues(null, array('user_id' => $uid)); + return $user; } protected function startSessionAs($user, $level) { - if ((!is_null(S::v('user')) && S::v('user')->id() != $user->id()) + if ((!is_null(S::user()) && S::user()->id() != $user->id()) || (S::has('uid') && S::i('uid') != $user->id())) { return false; } else if (S::has('uid')) { @@ -190,31 +166,8 @@ class XorgSession extends PlSession S::set('auth', AUTH_MDP); } - // Retrieves main user properties. - /** TODO: Move needed informations to account tables */ - /** TODO: Currently suppressed data are matricule, promo */ - /** TODO: Use the User object to fetch all this */ - $res = XDB::query("SELECT a.uid, a.hruid, a.display_name, a.full_name, - a.sex = 'female' AS femme, a.email_format, - a.token, FIND_IN_SET('watch', a.flags) AS watch_account, - UNIX_TIMESTAMP(fp.last_seen) AS banana_last, UNIX_TIMESTAMP(w.last) AS watch_last, - a.last_version, g.g_account_name IS NOT NULL AS googleapps, - UNIX_TIMESTAMP(s.start) AS lastlogin, s.host, - a.is_admin, at.perms - FROM accounts AS a - INNER JOIN account_types AS at ON(a.type = at.type) - INNER JOIN watch AS w ON(w.uid = a.uid) - LEFT JOIN forum_profiles AS fp ON(fp.uid = a.uid) - LEFT JOIN gapps_accounts AS g ON(a.uid = g.l_userid AND g.g_status = 'active') - LEFT JOIN logger.last_sessions AS ls ON (ls.uid = a.uid) - LEFT JOIN logger.sessions AS s ON(s.id = ls.id) - WHERE a.uid = {?} AND a.state = 'active'", $user->id()); - $sess = $res->fetchOneAssoc(); - $perms = $sess['perms']; - unset($sess['perms']); - - // Loads the data into the real session. - $_SESSION = array_merge($_SESSION, $sess); + // Loads uid and hruid into the session for developement conveniance. + $_SESSION = array_merge($_SESSION, array('uid' => $user->id(), 'hruid' => $user->hruid, 'token' => $user->token, 'user' => $user)); // Starts the session's logger, and sets up the permanent cookie. if (S::suid()) { @@ -231,32 +184,20 @@ class XorgSession extends PlSession } // Finalizes the session setup. - $this->makePerms($perms, S::b('is_admin')); + $this->makePerms($user->perms, $user->is_admin); $this->securityChecks(); $this->setSkin(); $this->updateNbNotifs(); - check_redirect(); + // Only check email redirection for 'internal' users. + if ($user->checkPerms(PERMS_USER)) { + check_redirect(); + } // We should not have to use this private data anymore S::kill('auth_by_cookie'); return true; } - /** Start a session without authentication data for the given user. - * This is used to identify the user after his registration, to be - * removed after rewriting registration procedure. - * XXX: Temporary - */ - public function startWeakSession($user) - { - if (!$this->startSessionAs($user, AUTH_MDP)) { - $this->destroy(); - return false; - } - S::set('auth', AUTH_MDP); - return true; - } - private function securityChecks() { $mail_subject = array(); @@ -279,13 +220,60 @@ class XorgSession extends PlSession } } + /** + * The authentication schema is based on three query parameters: + * ?user=×tamp=&sig= + * where: + * - hruid is the hruid of the querying user + * - timestamp is the current UNIX timestamp, which has to be within a + * given distance of the server-side UNIX timestamp + * - sig is the HMAC of "###" using + * a known secret of the user as the key. + * + * At the moment, the shared secret of the user is the sha1 hash of its + * password. This is temporary, though, until better support for tokens is + * implemented in plat/al. + * TODO(vzanotti): Switch to dedicated secrets for authentication. + */ + public function apiAuth($method, $resource, $payload) + { + // Verify that the timestamp is within acceptable bounds. + $timestamp = Env::i('timestamp', 0); + if (abs($timestamp - time()) > Platal::globals()->api->timestamp_tolerance) { + return null; + } + + // Retrieve the user corresponding to the forlife. Note that at the + // moment, other aliases are also accepted. + $user = User::getSilent(Env::s('user', '')); + if (is_null($user) || !$user->isActive()) { + return null; + } + + // Determine the list of tokens associated with the user. At the moment, + // this is just the sha1 of the password. + $tokens = array($user->password()); + + // For each token, try to validate the signature. + $message = implode('#', array($method, $resource, $payload, $timestamp)); + $signature = Env::s('sig'); + foreach ($tokens as $token) { + $expected_signature = hash_hmac( + Platal::globals()->api->hmac_algo, $message, $token); + if ($signature == $expected_signature) { + return $user; + } + } + + return null; + } + public function tokenAuth($login, $token) { - $res = XDB::query('SELECT a.uid AS user_id, a.hruid - FROM aliases AS l - INNER JOIN accounts AS a ON (l.id = a.uid AND a.state = \'active\') - WHERE a.token = {?} AND l.alias = {?} AND l.type != \'homonyme\'', - $token, $login); + $res = XDB::query('SELECT a.uid, a.hruid + FROM accounts AS a + WHERE a.token = {?} AND a.hruid = {?} AND a.state = \'active\'', + $token, $login); if ($res->numRows() == 1) { return new User(null, $res->fetchOneAssoc()); }