summaryrefslogtreecommitdiffhomepage
path: root/dev/Kernel/src/User.cc
diff options
context:
space:
mode:
authorAmlal <amlalelmahrouss@icloud.com>2024-12-21 21:59:13 +0100
committerAmlal <amlalelmahrouss@icloud.com>2024-12-21 21:59:45 +0100
commit610f91d87152cbe48d3054fcf437d8239da6ef35 (patch)
treea386f7047ab73d088169ab2371ddc6ffe8020f1c /dev/Kernel/src/User.cc
parent509fcca5986651c8ba712fb395f8498f2dea4109 (diff)
IMP: :boom: Breaking changes some checks are needed to be done.
Signed-off-by: Amlal <amlalelmahrouss@icloud.com>
Diffstat (limited to 'dev/Kernel/src/User.cc')
-rw-r--r--dev/Kernel/src/User.cc178
1 files changed, 178 insertions, 0 deletions
diff --git a/dev/Kernel/src/User.cc b/dev/Kernel/src/User.cc
new file mode 100644
index 00000000..601eee15
--- /dev/null
+++ b/dev/Kernel/src/User.cc
@@ -0,0 +1,178 @@
+/*
+ * ========================================================
+ *
+ * ZKA
+ * Copyright (C) 2024, TQ B.V, all rights reserved., all rights reserved.
+ *
+ * File: User.cc
+ * Purpose: User class, used to provide authentication and security.
+ *
+ * ========================================================
+ */
+
+#include <KernelKit/User.h>
+#include <KernelKit/LPC.h>
+#include <NewKit/Stop.h>
+#include <KernelKit/FileMgr.h>
+#include <KernelKit/UserProcessScheduler.h>
+#include <KernelKit/Heap.h>
+
+#define kStdUserType (0xCE)
+#define kSuperUserType (0xEC)
+
+/// @file User.cc
+/// @brief User support (or also called )
+
+namespace Kernel
+{
+ namespace Detail
+ {
+ /// \brief Constructs a password by hashing the password.
+ /// \param password password to hash.
+ /// \return the hashed password
+ const Int32 cred_construct_token(Char* password, const Char* in_password, User* user, SizeT length)
+ {
+ if (!password || !user)
+ return 1;
+
+ kcout << "cred_construct_token: Hashing user password...\r";
+
+ for (Size i_pass = 0; i_pass < length; ++i_pass)
+ {
+ const Char& cur_chr = in_password[i_pass];
+
+ if (cur_chr == 0)
+ break;
+
+ password[i_pass] = cur_chr | (user->IsStdUser() ? kStdUserType : kSuperUserType);
+ }
+
+ kcout << "cred_construct_token: Hashed user password.\r";
+
+ return 0;
+ }
+ } // namespace Detail
+
+ /// @brief User ring constructor.
+ User::User(const Int32& sel, const Char* userName)
+ : mUserRing((UserRingKind)sel)
+ {
+ MUST_PASS(sel >= 0);
+ rt_copy_memory((VoidPtr)userName, this->mUserName, rt_string_len(userName));
+ }
+
+ /// @brief User ring constructor.
+ User::User(const UserRingKind& ringKind, const Char* userName)
+ : mUserRing(ringKind)
+ {
+ rt_copy_memory((VoidPtr)userName, this->mUserName, rt_string_len(userName));
+ }
+
+ /// @brief User destructor class.
+ User::~User() = default;
+
+ Bool User::Save(const usr_public_key_kind password_to_fill) noexcept
+ {
+ if (!password_to_fill ||
+ *password_to_fill == 0)
+ return No;
+
+ SizeT len = rt_string_len(password_to_fill);
+
+ Char* password = new Char[len];
+ MUST_PASS(password);
+
+ // fill data first, generate hash.
+ // return false on error.
+
+ rt_copy_memory((VoidPtr)password_to_fill, password, len);
+
+ if (!Detail::cred_construct_token(password, password_to_fill, this, len))
+ {
+ delete[] password;
+ password = nullptr;
+
+ return No;
+ }
+
+ // then store password.
+
+ rt_copy_memory(password, this->mUserToken, rt_string_len(password_to_fill));
+
+ delete[] password;
+ password = nullptr;
+
+ kcout << "User::Save: Saved password successfully...\r";
+
+ return Yes;
+ }
+
+ Bool User::Matches(const usr_public_key_kind password_to_fill) noexcept
+ {
+ if (!password_to_fill ||
+ *password_to_fill)
+ return No;
+
+ SizeT len = rt_string_len(password_to_fill);
+
+ Char* password = new Char[len];
+ MUST_PASS(password);
+
+ // fill data first, generate hash.
+ // return false on error.
+
+ rt_copy_memory((VoidPtr)password_to_fill, password, len);
+
+ if (!Detail::cred_construct_token(password, password_to_fill, this, len))
+ {
+ delete[] password;
+ password = nullptr;
+
+ return No;
+ }
+
+ kcout << "User::Matches: Validating hashed passwords...\r";
+
+ // now check if the password matches.
+ if (rt_string_cmp(password, this->mUserToken, rt_string_len(this->mUserToken)) == 0)
+ {
+ kcout << "User::Matches: Password is valid.\r";
+ return Yes;
+ }
+
+ kcout << "User::Matches: Password isn't valid.\r";
+ return No;
+ }
+
+ Bool User::operator==(const User& lhs)
+ {
+ return lhs.mUserRing == this->mUserRing;
+ }
+
+ Bool User::operator!=(const User& lhs)
+ {
+ return lhs.mUserRing != this->mUserRing;
+ }
+
+ Char* User::Name() noexcept
+ {
+ return this->mUserName;
+ }
+
+ /// @brief Returns the user's ring.
+ /// @return The king of ring the user is attached to.
+ const UserRingKind& User::Ring() noexcept
+ {
+ return this->mUserRing;
+ }
+
+ Bool User::IsStdUser() noexcept
+ {
+ return this->Ring() == UserRingKind::kRingStdUser;
+ }
+
+ Bool User::IsSuperUser() noexcept
+ {
+ return this->Ring() == UserRingKind::kRingSuperUser;
+ }
+} // namespace Kernel