public function testGetKeys() { $query = \OC_DB::prepare('SELECT DISTINCT `configkey` FROM `*PREFIX*preferences` WHERE `userid` = ? AND `appid` = ?'); $result = $query->execute(array('Someuser', 'getkeysapp')); $expected = array(); while ($row = $result->fetchRow()) { $expected[] = $row['configkey']; } $this->assertEquals($expected, \OC_Preferences::getKeys('Someuser', 'getkeysapp')); }
/** * perform login using the magic cookie (remember login) * * @param string $uid the username * @param string $currentToken * @return bool */ public function loginWithCookie($uid, $currentToken) { $this->manager->emit('\\OC\\User', 'preRememberedLogin', array($uid)); $user = $this->manager->get($uid); if (is_null($user)) { // user does not exist return false; } // get stored tokens $tokens = \OC_Preferences::getKeys($uid, 'login_token'); // test cookies token against stored tokens if (!in_array($currentToken, $tokens, true)) { return false; } // replace successfully used token with a new one \OC_Preferences::deleteKey($uid, 'login_token', $currentToken); $newToken = \OC_Util::generateRandomBytes(32); \OC_Preferences::setValue($uid, 'login_token', $newToken, time()); $this->setMagicInCookie($user->getUID(), $newToken); //login $this->setUser($user); $this->manager->emit('\\OC\\User', 'postRememberedLogin', array($user)); return true; }
protected static function tryRememberLogin() { if (!isset($_COOKIE["oc_remember_login"]) || !isset($_COOKIE["oc_token"]) || !isset($_COOKIE["oc_username"]) || !$_COOKIE["oc_remember_login"]) { return false; } OC_App::loadApps(array('authentication')); if (defined("DEBUG") && DEBUG) { OC_Log::write('core', 'Trying to login from cookie', OC_Log::DEBUG); } // confirm credentials in cookie if (isset($_COOKIE['oc_token']) && OC_User::userExists($_COOKIE['oc_username'])) { // delete outdated cookies self::cleanupLoginTokens($_COOKIE['oc_username']); // get stored tokens $tokens = OC_Preferences::getKeys($_COOKIE['oc_username'], 'login_token'); // test cookies token against stored tokens if (in_array($_COOKIE['oc_token'], $tokens, true)) { // replace successfully used token with a new one OC_Preferences::deleteKey($_COOKIE['oc_username'], 'login_token', $_COOKIE['oc_token']); $token = OC_Util::generate_random_bytes(32); OC_Preferences::setValue($_COOKIE['oc_username'], 'login_token', $token, time()); OC_User::setMagicInCookie($_COOKIE['oc_username'], $token); // login OC_User::setUserId($_COOKIE['oc_username']); OC_Util::redirectToDefaultPage(); // doesn't return } // if you reach this point you have changed your password // or you are an attacker // we can not delete tokens here because users may reach // this point multiple times after a password change OC_Log::write('core', 'Authentication cookie rejected for user ' . $_COOKIE['oc_username'], OC_Log::WARN); } OC_User::unsetMagicInCookie(); return true; }
/** * Remove outdated and therefore invalid tokens for a user * @param string $user */ protected static function cleanupLoginTokens($user) { $cutoff = time() - OC_Config::getValue('remember_login_cookie_lifetime', 60 * 60 * 24 * 15); $tokens = OC_Preferences::getKeys($user, 'login_token'); foreach ($tokens as $token) { $time = OC_Preferences::getValue($user, 'login_token', $token); if ($time < $cutoff) { OC_Preferences::deleteKey($user, 'login_token', $token); } } }
/** * get private data * @param string $user * @param string $app * @param string $key * @param bool $like use LIKE instead of = when comparing keys * @return array */ public static function getData($user, $app = "", $key = "") { if ($app) { $apps = array($app); } else { $apps = OC_Preferences::getApps($user); } if ($key) { $keys = array($key); } else { foreach ($apps as $app) { $keys = OC_Preferences::getKeys($user, $app); } } $result = array(); foreach ($apps as $app) { foreach ($keys as $key) { $value = OC_Preferences::getValue($user, $app, $key); $result[] = array('app' => $app, 'key' => $key, 'value' => $value); } } return $result; }
/** * Get the keys of all stored by an app for the user * * @param string $userId the userId of the user that we want to store the value under * @param string $appName the appName that we stored the value under * @return string[] */ public function getUserKeys($userId, $appName) { return \OC_Preferences::getKeys($userId, $appName); }