A simple security solution.
- php: ~7.0
- chubbyphp/chubbyphp-error-handler: ~1.0
- chubbyphp/chubbyphp-session: ~1.0
- pimple/pimple: ~3.0
Through Composer as chubbyphp/chubbyphp-security.
composer require chubbyphp/chubbyphp-security "^1.2"
<?php
use Chubbyphp\Security\Authentication\AuthenticationProvider;
use Chubbyphp\Security\Authentication\FormAuthentication;
use Pimple\Container;
$container->register(new AuthenticationProvider);
$container->extend('security.authentication.authentications', function (array $authentications) use ($container) {
$authentications[] = new FormAuthentication(...);
return $authentications;
});
$container['security.authentication']->isAuthenticated($request);
<?php
use Chubbyphp\Security\Authentication\AuthenticationErrorHandlerInterface;
use Chubbyphp\Security\Authentication\AuthenticationErrorResponseMiddleware;
use Chubbyphp\Security\Authentication\FormAuthentication;
$middleware = new AuthenticationErrorResponseMiddleware(
new FormAuthentication(...),
new class() implements AuthenticationErrorHandlerInterface {
public function errorResponse(
Request $request,
Response $response,
int $code
): Response {
return $response->withStatus($code);
}
}
);
$middleware($request, $response);
<?php
use Chubbyphp\Security\Authentication\AuthenticationMiddleware;
use Chubbyphp\Security\Authentication\FormAuthentication;
$middleware = new AuthenticationMiddleware(new FormAuthentication(...));
$middleware($request, $response);
<?php
use Chubbyphp\Security\Authentication\FormAuthentication;
use Chubbyphp\Security\Authentication\PasswordManager;
use Chubbyphp\Session\Session;
$authentication = new FormAuthentication(new PasswordManager, new Session, ...);
$authentication->login($request);
$authentication->logout($request);
$authentication->isAuthenticated($request);
$authentication->getAuthenticatedUser($request);
<?php
use Chubbyphp\Security\Authentication\PasswordManager;
$manager = new PasswordManager();
$hash = $manager->hash('password');
$manager->verify('password', $hash);
<?php
use Chubbyphp\Security\Authorization\AuthorizationProvider;
use Chubbyphp\Security\Authorization\RoleAuthorization;
use Pimple\Container;
$container->register(new AuthorizationProvider);
$container->extend('security.authorization.rolehierarchy', function (array $rolehierarchy) use ($container) {
$rolehierarchy['ADMIN'] = ['USER_MANAGEMENT'];
$rolehierarchy['USER_MANAGEMENT'] = ['USER_LIST', 'USER_CREATE', 'USER_EDIT', 'USER_VIEW', 'USER_DELETE'];
return $rolehierarchy;
});
$container['security.authorization.rolehierarchyresolver']->resolve($roles);
$container->extend('security.authorization.authorizations', function (array $authorizations) use ($container) {
$authorizations[] = new RoleAuthorization(...);
return $$authorizations;
});
$container['security.authorization']->isGranted($user, 'USER_EDIT');
<?php
use Chubbyphp\Security\Authorization\RoleAuthorization;
use Chubbyphp\Security\Authorization\RoleHierarchyResolver;
$user->setRoles(['ADMIN']);
$resolver = new RoleHierarchyResolver([
'ADMIN' => ['USER_MANAGEMENT'],
'USER_MANAGEMENT' => ['USER_CREATE', 'USER_EDIT', 'USER_VIEW', 'USER_DELETE']
]);
$authorization = new RoleAuthorization($resolver);
$authorization->isGranted($user, 'USER_EDIT'); // true
<?php
use Chubbyphp\Security\Authorization\RoleHierarchyResolver;
$user->setRoles(['ADMIN']);
$resolver = new RoleHierarchyResolver([
'ADMIN' => ['USER_MANAGEMENT'],
'USER_MANAGEMENT' => ['USER_CREATE', 'USER_EDIT', 'USER_VIEW', 'USER_DELETE']
]);
$resolver->resolve(['ADMIN']);
Dominik Zogg 2016