summaryrefslogtreecommitdiff
path: root/core/classes/authentication/Core
diff options
context:
space:
mode:
authorGeorge Halkiadakis <gchalkiadakis@sklavenitis.co.gr>2023-03-14 00:28:47 +0200
committerGeorge Halkiadakis <gchalkiadakis@sklavenitis.co.gr>2023-03-14 00:28:47 +0200
commit8b928471412bbf9016df9b01b2308f003e455d7d (patch)
tree913a7bab845b8bfd72ba489746ea703159314f4b /core/classes/authentication/Core
parente70176b6e6f539754974117625eeab06c4c5dd01 (diff)
parentd9167fb5318d5370ba5445e94c0e0f9b64341df4 (diff)
downloadclassroom-8b928471412bbf9016df9b01b2308f003e455d7d.tar.gz
classroom-8b928471412bbf9016df9b01b2308f003e455d7d.tar.bz2
classroom-8b928471412bbf9016df9b01b2308f003e455d7d.zip
Merge branch 'authentication'
Diffstat (limited to 'core/classes/authentication/Core')
-rw-r--r--core/classes/authentication/Core/PasswordTrait.php32
-rw-r--r--core/classes/authentication/Core/UserManager.php68
-rw-r--r--core/classes/authentication/Core/UserManagerInterface.php25
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;
+}