summaryrefslogtreecommitdiff
path: root/core/classes/authentication/Core
diff options
context:
space:
mode:
Diffstat (limited to 'core/classes/authentication/Core')
-rw-r--r--core/classes/authentication/Core/PasswordTrait.php38
-rw-r--r--core/classes/authentication/Core/UserManager.php101
-rw-r--r--core/classes/authentication/Core/UserManagerInterface.php25
3 files changed, 164 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..40a7381
--- /dev/null
+++ b/core/classes/authentication/Core/PasswordTrait.php
@@ -0,0 +1,38 @@
+<?php
+/** Trait PasswordTrait
+ *
+ * @package DevCoder\Authentication\Core
+ *
+ */
+# namespace DevCoder\Authentication\Core;
+#
+# use DevCoder\Authentication\UserInterface;
+
+trait PasswordTrait
+{
+
+ private $cost = 10; // Cost must be in the range of 4-31
+ // a cost value in the range of 8-11
+ // is a good balance between performance and security
+
+
+ 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 > 31) {
+ 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..54cd2cb
--- /dev/null
+++ b/core/classes/authentication/Core/UserManager.php
@@ -0,0 +1,101 @@
+<?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();
+ }
+ }
+
+
+ /** getUserToken()
+ * == get user from session
+ *
+ */
+ public function getUserToken(): ?UserTokenInterface
+ {
+ $userToken = null;
+ if ($this->hasUserToken()) {
+ $userToken = unserialize($_SESSION[UserTokenInterface::DEFAULT_PREFIX_KEY]);
+ }
+
+ return $userToken;
+ }
+
+
+ /** hasUserToken()
+ * == is user loged in
+ *
+ */
+ public function hasUserToken(): bool
+ {
+ $key = UserTokenInterface::DEFAULT_PREFIX_KEY;
+ return (array_key_exists($key, $_SESSION) && unserialize($_SESSION[$key]) !== false);
+ }
+
+
+ /** isGranted
+ *
+ * checks if user is granded some role(s)
+ * from the array of roles that are passed
+ *
+ * ex. $token->isGranted(['editor', 'designer']) ...
+ * returns true if the user is editor or designer (or both)
+ *
+ */
+ public function isGranted(array $roles): bool
+ {
+ // if (!is_null($userToken = $this->getUserToken())) {
+ if (is_null($userToken = $this->getUserToken())) {
+ return false;
+ }
+
+ if ($userToken->getUser() instanceof UserInterface) {
+ return (!empty(array_intersect($roles, $userToken->getUser()->getRoles())));
+ }
+
+ return false;
+ }
+
+
+ /** createUserToken()
+ * == serializes user and stores it into session
+ * so $_SESSION[DEFAULT_PREFIX_KEY] has the serialized representaion of user
+ */
+ public function createUserToken(UserInterface $user): UserTokenInterface
+ {
+ $userToken = new UserToken($user);
+ $_SESSION[UserTokenInterface::DEFAULT_PREFIX_KEY] = $userToken->serialize();
+
+ return $userToken;
+ }
+
+
+ /** logout
+ * == clear session
+ *
+ */
+ 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..05110fb
--- /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;
+}