Skip to content

Commit

Permalink
Fix/initialize security service (#112)
Browse files Browse the repository at this point in the history
* Code cleanup

* Fixed security service initialization

* Improved CS

* Change getSessionToken() to getRequestToken() in CSRF forms

* Added workaround for phalcon/cphalcon#14346

* Do not use helper container()
  • Loading branch information
sergeyklay committed Sep 2, 2019
1 parent c24a6af commit 3a7b403
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 25 deletions.
8 changes: 5 additions & 3 deletions app/Controllers/PermissionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ public function indexAction(): void
$this->acl->rebuild();

// Pass the current permissions to the view
$this->view->permissions = $this->acl->getPermissions($profile);
$this->view->setVar('permissions', $this->acl->getPermissions($profile));
}

$this->view->profile = $profile;
$this->view->setVar('profile', $profile);
}

$profiles = Profiles::find([
Expand All @@ -66,7 +66,7 @@ public function indexAction(): void
]
]);

$this->view->profilesSelect = $this->tag->select([
$profilesSelect = $this->tag->select([
'profileId',
$profiles,
'using' => [
Expand All @@ -78,5 +78,7 @@ public function indexAction(): void
'emptyValue' => '',
'class' => 'form-control mr-sm-2',
]);

$this->view->setVar('profilesSelect', $profilesSelect);
}
}
13 changes: 6 additions & 7 deletions app/Controllers/ProfilesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function initialize(): void
public function indexAction(): void
{
$this->persistent->conditions = null;
$this->view->form = new ProfilesForm();
$this->view->setVar('form', new ProfilesForm(null));
}

/**
Expand Down Expand Up @@ -74,7 +74,7 @@ public function searchAction()
"page" => $numberPage
]);

$this->view->page = $paginator->paginate();
$this->view->setVar('page', $paginator->paginate());
}

/**
Expand All @@ -97,7 +97,7 @@ public function createAction(): void
}
}

$this->view->form = new ProfilesForm(null);
$this->view->setVar('form', new ProfilesForm(null));
}

/**
Expand Down Expand Up @@ -130,11 +130,10 @@ public function editAction($id)
}
}

$this->view->form = new ProfilesForm(null, [
'edit' => true
$this->view->setVars([
'form' => new ProfilesForm(null, ['edit' => true]),
'profile' => $profile,
]);

$this->view->profile = $profile;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions app/Controllers/SessionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function signupAction()
}
}

$this->view->form = $form;
$this->view->setVar('form', $form);
}

/**
Expand Down Expand Up @@ -99,7 +99,7 @@ public function loginAction()
$this->flash->error($e->getMessage());
}

$this->view->form = $form;
$this->view->setVar('form', $form);
}

/**
Expand Down Expand Up @@ -139,7 +139,7 @@ public function forgotPasswordAction(): void
}
}

$this->view->form = $form;
$this->view->setVar('form', $form);
}

/**
Expand Down
16 changes: 9 additions & 7 deletions app/Controllers/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function initialize(): void
public function indexAction(): void
{
$this->persistent->conditions = null;
$this->view->form = new UsersForm();
$this->view->setVar('form', new UsersForm());
}

/**
Expand Down Expand Up @@ -71,7 +71,7 @@ public function searchAction()
"page" => $numberPage
]);

$this->view->page = $paginator->paginate();
$this->view->setVar('page', $paginator->paginate());
}

/**
Expand Down Expand Up @@ -102,7 +102,7 @@ public function createAction(): void
}
}

$this->view->form = $form;
$this->view->setVar('form', $form);
}

/**
Expand Down Expand Up @@ -148,9 +148,11 @@ public function editAction($id)
}
}

$this->view->user = $user;
$this->view->form = new UsersForm(null, [
'edit' => true
$this->view->setVars([
'user' => $user,
'form' => new UsersForm(null, [
'edit' => true
]),
]);
}

Expand Down Expand Up @@ -215,6 +217,6 @@ public function changePasswordAction(): void
}
}

$this->view->form = $form;
$this->view->setVar('form', $form);
}
}
2 changes: 1 addition & 1 deletion app/Forms/LoginForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function initialize()
// CSRF
$csrf = new Hidden('csrf');
$csrf->addValidator(new Identical([
'value' => $this->security->getSessionToken(),
'value' => $this->security->getRequestToken(),
'message' => 'CSRF validation failed'
]));
$csrf->clear();
Expand Down
4 changes: 2 additions & 2 deletions app/Forms/SignUpForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function initialize(string $entity = null, array $options = [])
'messageMinimum' => 'Password is too short. Minimum 8 characters'
]),
new Confirmation([
'message' => 'Password doesn\'t match confirmation',
'message' => "Password doesn't match confirmation",
'with' => 'confirmPassword'
])
]);
Expand Down Expand Up @@ -102,7 +102,7 @@ public function initialize(string $entity = null, array $options = [])
// CSRF
$csrf = new Hidden('csrf');
$csrf->addValidator(new Identical([
'value' => $this->security->getSessionToken(),
'value' => $this->security->getRequestToken(),
'message' => 'CSRF validation failed'
]));
$csrf->clear();
Expand Down
3 changes: 2 additions & 1 deletion app/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
namespace Vokuro;

use Phalcon\Di;
use Phalcon\Di\DiInterface;

/**
* Call Dependency Injection container
*
* @return mixed
* @return mixed|null|DiInterface
*/
function container()
{
Expand Down
48 changes: 48 additions & 0 deletions app/Phalcon/Beta2FixSecurity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);

namespace Phalcon;

/**
* Extended class for fixing phalcon/cphalcon#14346 issue
*
* @see https://github.com/phalcon/cphalcon/pull/14347
*/
class Beta2FixSecurity extends Security
{
/**
* @inheritDoc
*/
public function getRequestToken(): string
{
if (empty($this->requestToken)) {
return $this->getSessionToken();
}

return (string) $this->requestToken;
}

/**
* @inheritDoc
*
* @return string
* @throws Exception
*/
public function getSessionToken(): string
{
$di = $this->getDI();

if (!is_object($di)) {
throw new Exception(
Exception::containerServiceNotFound("the 'session' service")
);
}

if ($di->has('session')) {
$session = $di->getShared('session');
return (string) $session->get($this->tokenValueSessionId);
}

return '';
}
}
2 changes: 1 addition & 1 deletion app/Plugins/Auth/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public function checkUserFlags(Users $user)
/**
* Returns the current identity
*
* @return array
* @return array|null
*/
public function getIdentity()
{
Expand Down
60 changes: 60 additions & 0 deletions app/Providers/SecurityProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
declare(strict_types=1);

/**
* This file is part of the Vökuró.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Vokuro\Providers;

use Phalcon\Beta2FixSecurity;
use Phalcon\Di\DiInterface;
use Phalcon\Di\ServiceProviderInterface;
use Phalcon\Security;
use Phalcon\Version;

class SecurityProvider implements ServiceProviderInterface
{
/**
* @var string
*/
protected $providerName = 'security';

/**
* @param DiInterface $di
* @return void
*/
public function register(DiInterface $di): void
{
$that = $this;
$di->set($this->providerName, function () use ($di, $that) {
return $that->getSecurity($di);
});
}

/**
* Remove current method after after next release of Phalcon 4
*
* @see https://github.com/phalcon/cphalcon/issues/14346
*
* @param DiInterface $di
* @return Security
*/
protected function getSecurity(DiInterface $di): Security
{
if (Version::get() !== '4.0.0-beta.2') {
$security = new Security();
} else {
$security = new Beta2FixSecurity();
}

$security->setDI($di);

return $security;
}
}
1 change: 1 addition & 0 deletions configs/providers.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
\Vokuro\Providers\RouterProvider::class,
\Vokuro\Providers\SessionBagProvider::class,
\Vokuro\Providers\SessionProvider::class,
\Vokuro\Providers\SecurityProvider::class,
\Vokuro\Providers\UrlProvider::class,
\Vokuro\Providers\ViewProvider::class,
];

0 comments on commit 3a7b403

Please sign in to comment.