/** * Basic update test */ public function testUserUpdate() { // Create the user to edit $user = UserFactory::createUser(); $r = new Request(); // Login $r["auth_token"] = $this->login($user); // Change values $r["name"] = Utils::CreateRandomString(); $r["country_id"] = 'MX'; $r["state_id"] = 3; $r["scholar_degree"] = 'MaestrÃa'; $r["birth_date"] = strtotime('1988-01-01'); $r["graduation_date"] = strtotime('2016-02-02'); // Call api $response = UserController::apiUpdate($r); // Check user from db $user_db = AuthTokensDAO::getUserByToken($r["auth_token"]); $this->assertEquals($user_db->getName(), $r["name"]); $this->assertEquals($user_db->getCountryId(), $r["country_id"]); $this->assertEquals($user_db->getStateId(), $r["state_id"]); $this->assertEquals($user_db->getScholarDegree(), $r["scholar_degree"]); $this->assertEquals($user_db->getBirthDate(), gmdate('Y-m-d', $r["birth_date"])); $this->assertEquals($user_db->getGraduationDate(), gmdate('Y-m-d', $r["graduation_date"])); }
/** * Given an User, checks that login let state as supposed * * @param Users $user * @param type $auth_token */ public function assertLogin(Users $user, $auth_token = null) { // Check auth token $authTokenKey = new AuthTokens(array("user_id" => $user->getUserId())); $auth_tokens_bd = AuthTokensDAO::search($authTokenKey); // Validar que el token se guardó en la BDD if (!is_null($auth_token)) { $exists = false; foreach ($auth_tokens_bd as $token_db) { if (strcmp($token_db->getToken(), $auth_token) === 0) { $exists = true; break; } } if ($exists === false) { $this->fail("Token {$auth_token} not in DB."); } } // @todo check last access time }
private function RegisterSession(Users $vo_User, $b_ReturnAuthTokenAsString = false) { // Log the login. UserLoginLogDAO::save(new UserLoginLog(array('user_id' => $vo_User->user_id, 'ip' => ip2long($_SERVER['REMOTE_ADDR'])))); // Expire the local session cache. self::$current_session = null; //find if this user has older sessions $vo_AuthT = new AuthTokens(); $vo_AuthT->setUserId($vo_User->getUserId()); //erase expired tokens try { $tokens_erased = AuthTokensDAO::expireAuthTokens($vo_User->getUserId()); } catch (Exception $e) { // Best effort self::$log->error("Failed to delete expired tokens: {$e->getMessage}()"); } // Create the new token $entropy = bin2hex(mcrypt_create_iv(SessionController::AUTH_TOKEN_ENTROPY_SIZE, MCRYPT_DEV_URANDOM)); $s_AuthT = $entropy . '-' . $vo_User->getUserId() . '-' . hash('sha256', OMEGAUP_MD5_SALT . $vo_User->getUserId() . $entropy); $vo_AuthT = new AuthTokens(); $vo_AuthT->setUserId($vo_User->getUserId()); $vo_AuthT->setToken($s_AuthT); try { AuthTokensDAO::save($vo_AuthT); } catch (Exception $e) { throw new InvalidDatabaseOperationException($e); } if (self::$setCookieOnRegisterSession) { $sm = $this->getSessionManagerInstance(); $sm->setCookie(OMEGAUP_AUTH_TOKEN_COOKIE_NAME, $s_AuthT, 0, '/'); } Cache::deleteFromCache(Cache::SESSION_PREFIX, $s_AuthT); if ($b_ReturnAuthTokenAsString) { return $s_AuthT; } }
public static function apiRegisterForContest(Request $r) { self::validateBasicDetails($r); $current_user = AuthTokensDAO::getUserByToken($r["auth_token"]); $contest_req = new ContestUserRequest(); $contest_req->setUserId($current_user->getUserId()); $contest_req->setContestId($r["contest"]->getContestId()); $contest_req->setRequestTime(gmdate('Y-m-d H:i:s')); try { ContestUserRequestDAO::save($contest_req); } catch (Exception $e) { self::$log->error("Failed to create new ContestUserRequest: " . $e->getMessage()); throw new InvalidDatabaseOperationException($e); } return array("status" => "ok"); }
public function testDeleteTokenExpired() { // Create an user in omegaup $user = UserFactory::createUser(); $auth_token = self::login($user); // Expire token manually $auth_token_dao = AuthTokensDAO::getByPK($auth_token); $auth_token_dao->setCreateTime(date('Y-m-d H:i:s', strtotime($auth_token_dao->getCreateTime() . ' - 9 hour'))); AuthTokensDAO::save($auth_token_dao); $auth_token_2 = self::login($user); $existingTokens = AuthTokensDAO::getByPK($auth_token); $this->assertNull($existingTokens); }