diff options
Diffstat (limited to 'core/classes/authentication/Core')
| -rw-r--r-- | core/classes/authentication/Core/PasswordTrait.php | 32 | ||||
| -rw-r--r-- | core/classes/authentication/Core/UserManager.php | 68 | ||||
| -rw-r--r-- | core/classes/authentication/Core/UserManagerInterface.php | 25 |
3 files changed, 125 insertions, 0 deletions
diff --git a/core/classes/authentication/Core/PasswordTrait.php b/core/classes/authentication/Core/PasswordTrait.php new file mode 100644 index 0000000..18d67b5 --- /dev/null +++ b/core/classes/authentication/Core/PasswordTrait.php @@ -0,0 +1,32 @@ +<?php +/** Trait PasswordTrait + * + * @package DevCoder\Authentication\Core + * + */ +// namespace DevCoder\Authentication\Core; + +// use DevCoder\Authentication\UserInterface; + +trait PasswordTrait +{ + private $cost = 10; + + public function cryptPassword(string $plainPassword): string + { + return password_hash($plainPassword, PASSWORD_BCRYPT, ['cost' => $this->cost]); + } + + public function isPasswordValid(UserInterface $user, string $plainPassword): bool + { + return password_verify($plainPassword, $user->getPassword()); + } + + public function setCost(int $cost): void + { + if ($cost < 4 || $cost > 12) { + throw new \InvalidArgumentException('Cost must be in the range of 4-31.'); + } + $this->cost = $cost; + } +} diff --git a/core/classes/authentication/Core/UserManager.php b/core/classes/authentication/Core/UserManager.php new file mode 100644 index 0000000..3b380b8 --- /dev/null +++ b/core/classes/authentication/Core/UserManager.php @@ -0,0 +1,68 @@ +<?php +/** Class UserManager + * + * @package DevCoder\Authentication\Core + * + */ +// namespace DevCoder\Authentication\Core; + +// use DevCoder\Authentication\Token\UserToken; +// use DevCoder\Authentication\Token\UserTokenInterface; +// use DevCoder\Authentication\UserInterface; + +class UserManager implements UserManagerInterface +{ + + use PasswordTrait; + + public function __construct() + { + if (session_status() === PHP_SESSION_NONE) { + session_start(); + } + } + + public function getUserToken(): ?UserTokenInterface + { + $userToken = null; + if ($this->hasUserToken()) { + $userToken = unserialize($_SESSION[UserTokenInterface::DEFAULT_PREFIX_KEY]); + } + + return $userToken; + } + + public function hasUserToken(): bool + { + $key = UserTokenInterface::DEFAULT_PREFIX_KEY; + return (array_key_exists($key, $_SESSION) && unserialize($_SESSION[$key]) !== false); + } + + public function isGranted(array $roles): bool + { + if (!is_null($userToken = $this->getUserToken())) { + return false; + } + + if ($userToken->getUser() instanceof UserInterface) { + return (!empty(array_intersect($roles, $userToken->getUser()->getRoles()))); + } + + return false; + } + + public function createUserToken(UserInterface $user): UserTokenInterface + { + $userToken = new UserToken($user); + $_SESSION[UserTokenInterface::DEFAULT_PREFIX_KEY] = $userToken->serialize(); + + return $userToken; + } + + public function logout(): void + { + if ($this->hasUserToken()) { + unset($_SESSION[UserTokenInterface::DEFAULT_PREFIX_KEY]); + } + } +} diff --git a/core/classes/authentication/Core/UserManagerInterface.php b/core/classes/authentication/Core/UserManagerInterface.php new file mode 100644 index 0000000..4702992 --- /dev/null +++ b/core/classes/authentication/Core/UserManagerInterface.php @@ -0,0 +1,25 @@ +<?php +/** Interface UserManagerInterface + * + * @package DevCoder\Authentication\Core + * + */ +// namespace DevCoder\Authentication\Core; + +// use DevCoder\Authentication\Token\UserTokenInterface; +// use DevCoder\Authentication\UserInterface; + +interface UserManagerInterface +{ + public function getUserToken(): ?UserTokenInterface; + + public function hasUserToken(): bool; + + public function createUserToken(UserInterface $user): UserTokenInterface; + + public function logout(): void; + + public function cryptPassword(string $plainPassword): string; + + public function isPasswordValid(UserInterface $user, string $plainPassword): bool; +} |
