Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve session init and timeout handling #915

Open
wants to merge 24 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions bbbeasy-backend/app/config/access.ini
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,11 @@ allow GET @settings_collect = *

allow PUT @recording_publish = *
allow POST @settings_save_logo = *


; initial user session loading
allow GET @users_init = *

allow GET @file = *

; notification routes
allow GET @warning_notification = *




2 changes: 1 addition & 1 deletion bbbeasy-backend/app/config/routes.ini
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ GET @roles_permissions_collect : /api/roles-permissions = Actions\RolesPermissio

; users routes
GET @users_index : /api/users = Actions\Users\Index->show
GET @users_init : /api/users/session = Actions\Users\Session->execute
POST @users_add : /api/users = Actions\Users\Add->save
PUT @users_edit : /api/users/@id = Actions\Users\Edit->save
DELETE @user_delete : /api/users/@id = Actions\Users\Delete->execute

; presets routes
GET @presets_index : /api/presets/@user_id = Actions\Presets\Index->show
POST @presets_add : /api/presets = Actions\Presets\Add->save
Expand Down
2 changes: 1 addition & 1 deletion bbbeasy-backend/app/src/Actions/Account/ChangePassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private function changePassword($user, $password, $resetToken, $errorMessage): v
{
try {
$user->password = $password;
$user->status = UserStatus::ACTIVE;
$user->status = UserStatus::ACTIVE;
$resetToken->save();
$user->save();
} catch (\Exception $e) {
Expand Down
13 changes: 12 additions & 1 deletion bbbeasy-backend/app/src/Actions/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,17 @@ public function __construct()

public function beforeroute(): void
{
if ($this->session->isLoggedIn() && !$this->session->getSession(session_id())) {
$this->session->revokeUser();
$this->f3->error(401);

exit;
}

$this->access->authorize($this->getRole(), function($route, $subject): void {
$this->onAccessAuthorizeDeny($route, $subject);
});

if ($this->session->isLoggedIn() && $this->f3->get('ALIAS') === $this->f3->get('ALIASES.login')) {
$this->f3->reroute($this->f3->get('ALIASES.home'));
} elseif ('POST' === $this->f3->VERB && !$this->session->validateToken()) {
Expand All @@ -130,7 +138,10 @@ public function beforeroute(): void
public function onAccessAuthorizeDeny($route, $subject): void
{
$this->logger->warning('Access denied to route ' . $route . ' for subject ' . ($subject ?: 'unknown'));
$this->f3->error(404);
if (!$this->session->isLoggedIn()) {
$this->f3->error(401);
}
$this->f3->error(403);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions bbbeasy-backend/app/src/Actions/Recordings/Publish.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
* Copyright (c) 2022-2023 RIADVICE SUARL and by respective authors (see below).
*
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* terms of the GNU Affero General Public License as published by the Free Software
* Foundation; either version 3.0 of the License, or (at your option) any later
* version.
*
* BBBEasy is distributed in the hope that it will be useful, but WITHOUT ANY
* BBBeasy is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
* PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along
* with BBBEasy; if not, see <http://www.gnu.org/licenses/>.
* You should have received a copy of the GNU Affero General Public License along
* with BBBeasy. If not, see <https://www.gnu.org/licenses/>
*/

namespace Actions\Recordings;
Expand Down
4 changes: 1 addition & 3 deletions bbbeasy-backend/app/src/Actions/RequirePrivilegeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,4 @@

namespace Actions;

trait RequirePrivilegeTrait
{
}
trait RequirePrivilegeTrait {}
3 changes: 3 additions & 0 deletions bbbeasy-backend/app/src/Actions/Roles/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ public function save($f3, $params): void

try {
$role->save();
if ($roleId === $this->session->get('user.roleId')) {
$this->session->set('user.role', $role->name);
}
} catch (\Exception $e) {
$this->logger->error($errorMessage, ['error' => $e->getMessage()]);
$this->renderJson(['errors' => $e->getMessage()], ResponseCode::HTTP_INTERNAL_SERVER_ERROR);
Expand Down
58 changes: 58 additions & 0 deletions bbbeasy-backend/app/src/Actions/Users/Session.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* BBBEasy open source platform - https://riadvice.tn/
*
* Copyright (c) 2022-2023 RIADVICE SUARL and by respective authors (see below).
*
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU Affero General Public License as published by the Free Software
* Foundation; either version 3.0 of the License, or (at your option) any later
* version.
*
* BBBeasy is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License along
* with BBBeasy. If not, see <https://www.gnu.org/licenses/>
*/

namespace Actions\Users;

use Actions\Base as BaseAction;
use Models\User;
use Models\UserSession;

class Session extends BaseAction
{
public function execute($f3, $params)
{
$user = new User();
$user_id = $this->session->get('user.id');

if (!$user_id) {
$this->session->revokeUser();

$this->f3->error(401);
}

$Infos = $user->getById($user_id);

$userInfos = [
'id' => $Infos->id,
'username' => $Infos->username,
'email' => $Infos->email,
'role' => $Infos->role->name,
'avatar' => $Infos->avatar,
'permissions' => $Infos->role->getRolePermissions(),
];
$userSession = new UserSession();
$sessionInfos = [
'PHPSESSID' => session_id(),
'expires' => $userSession->getSessionExpirationTime(session_id()),
];

$this->renderJson(['user' => $userInfos, 'session' => $sessionInfos]);
}
}
12 changes: 12 additions & 0 deletions bbbeasy-backend/app/src/Core/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class Session extends \Prefab
*/
public function __construct(SQL $db = null, $table = 'sessions', $force = false, $onsuspect = null, $key = null)
{
$this->db = $db ?: \Registry::get('db');
GhaziTriki marked this conversation as resolved.
Show resolved Hide resolved
$this->f3 = \Base::instance();
$this->initLogger();
if ('CACHE' === $table) {
Expand Down Expand Up @@ -100,6 +101,17 @@ public function set($key, $value): void
$this->f3->sync('SESSION');
}

public function getSession($sessionId)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still problematic in production mode since session data is stored in redis in production environment.

{
$result = $this->db->exec('SELECT expires FROM users_sessions where session_id = :session', [':session' => $sessionId]);
GhaziTriki marked this conversation as resolved.
Show resolved Hide resolved

if (\count($result) < 1) {
return false;
}

return true;
}

/**
* @param mixed $key
*
Expand Down
10 changes: 6 additions & 4 deletions bbbeasy-backend/app/src/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,13 @@ public function usernameExists(string $username, $id = null): bool
{
return $this->load(['lower(username) = ? and id != ?', mb_strtolower($username), $id]);
}
public function getUsers($username,$email){
$data = [];
$users = $this->find(['username != ? and email != ? ', $username, $email ]);

public function getUsers($username, $email)
{
$data = [];
$users = $this->find(['username != ? and email != ? ', $username, $email]);
if ($users) {
$data = $users->castAll(['username', 'email','password']);
$data = $users->castAll(['username', 'email', 'password']);
}

return $data;
Expand Down
11 changes: 2 additions & 9 deletions bbbeasy-backend/app/src/Utils/PresetProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,9 @@
use Enum\Presets\LockSettings;
use Enum\Presets\Presentation;
use Enum\Presets\Recording;

use Enum\Presets\Security;

use Enum\Presets\Screenshare;
use Enum\Presets\Security;
use Enum\Presets\UserExperience;

use Enum\Presets\Webcams;
use Enum\Presets\Whiteboard;

Expand Down Expand Up @@ -80,10 +77,8 @@ public function toCreateMeetingParams($preset, $createParams)
$presetsData->setData(BreakoutRooms::GROUP_NAME, BreakoutRooms::PRIVATE_CHAT, $preparePresetData[BreakoutRooms::GROUP_NAME][BreakoutRooms::PRIVATE_CHAT]);

$presetsData->setData(General::GROUP_NAME, General::DURATION, $preparePresetData[General::GROUP_NAME][General::DURATION] ?: null);


$presetsData->setData(General::GROUP_NAME, General::MAXIMUM_PARTICIPANTS, $preparePresetData[General::GROUP_NAME][General::MAXIMUM_PARTICIPANTS] ?: null);


$presetsData->setData(General::GROUP_NAME, General::MAXIMUM_PARTICIPANTS, $preparePresetData[General::GROUP_NAME][General::MAXIMUM_PARTICIPANTS] ?: null);
$presetsData->setData(General::GROUP_NAME, General::WELCOME, $preparePresetData[General::GROUP_NAME][General::WELCOME]);
Expand All @@ -104,14 +99,12 @@ public function toCreateMeetingParams($preset, $createParams)
$presetsData->setData(Recording::GROUP_NAME, Recording::AUTO_START, $preparePresetData[Recording::GROUP_NAME][Recording::AUTO_START]);
$presetsData->setData(Recording::GROUP_NAME, Recording::ALLOW_START_STOP, $preparePresetData[Recording::GROUP_NAME][Recording::ALLOW_START_STOP]);
$presetsData->setData(Recording::GROUP_NAME, Recording::RECORD, $preparePresetData[Recording::GROUP_NAME][Recording::RECORD]);


$presetsData->setData(Security::GROUP_NAME, Security::PASSWORD_FOR_MODERATOR, $preparePresetData[Security::GROUP_NAME][Security::PASSWORD_FOR_MODERATOR]);
$presetsData->setData(Security::GROUP_NAME, Security::PASSWORD_FOR_ATTENDEE, $preparePresetData[Security::GROUP_NAME][Security::PASSWORD_FOR_ATTENDEE]);


$presetsData->setData(Screenshare::GROUP_NAME, Screenshare::CONFIGURABLE, $preparePresetData[Screenshare::GROUP_NAME][Screenshare::CONFIGURABLE]);

$presetsData->setData(Webcams::GROUP_NAME, Webcams::VISIBLE_FOR_MODERATOR_ONLY, $preparePresetData[Webcams::GROUP_NAME][Webcams::VISIBLE_FOR_MODERATOR_ONLY]);
$presetsData->setData(Webcams::GROUP_NAME, Webcams::MODERATOR_ALLOWED_CAMERA_EJECT, $preparePresetData[Webcams::GROUP_NAME][Webcams::MODERATOR_ALLOWED_CAMERA_EJECT]);

Expand Down
12 changes: 5 additions & 7 deletions bbbeasy-backend/app/src/Utils/SecurityUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,18 @@ class SecurityUtils

public static function credentialsAreCommon(string $username, string $email, string $password): string|null
{
$user=new User();
$user = new User();

$users = $user->getUsers($username, $email);

$users=$user->getUsers($username,$email);

foreach ($users as $user1){
$user=$user->getByEmail($user1['email']);
if($user->verifyPassword($password)){
foreach ($users as $user1) {
$user = $user->getByEmail($user1['email']);
if ($user->verifyPassword($password)) {
return 'Avoid choosing a common password';
}
}
// @fixme: to be cached, reload to cache if update time changed


return null;
}

Expand Down
Loading