diff --git a/src/Database/Handler.php b/src/Database/Handler.php index d4166aa..fd46b35 100644 --- a/src/Database/Handler.php +++ b/src/Database/Handler.php @@ -48,6 +48,14 @@ class Handler ]); return $statement->rowCount() > 0; } + public function getPasswordHash(string $username): string + { + $statement = $this->pdo->prepare("SELECT password FROM users WHERE username = :username"); + $statement->execute([ + $username + ]); + return $statement->fetch()["password"]; + } public function changeUsername(string $username, string $newUsername): void { $statement = $this->pdo->prepare("UPDATE users SET username = :newUsername WHERE username = :username"); diff --git a/src/Http/LoginController.php b/src/Http/LoginController.php index 08a7ddc..79fe5cc 100644 --- a/src/Http/LoginController.php +++ b/src/Http/LoginController.php @@ -2,6 +2,7 @@ namespace ComCen\Http; +use ComCen\Database\Handler; use Ratchet\ConnectionInterface; use Ratchet\Http\HttpServerInterface; use Psr\Http\Message\RequestInterface; @@ -11,20 +12,33 @@ class LoginController implements HttpServerInterface public function onOpen(ConnectionInterface $conn, RequestInterface $request = null): void { $params = []; + $login = true; + $responseHead = ""; + $json = ""; parse_str($request->getUri()->getQuery(), $params); $username = $params["username"]; $password = $params["password"]; - if (!$username || !$password) { - $json = json_encode(["error" => "Bad Credentials"]); - Utils::class->sendJson($conn, "404 Bad Request", $json); + if (!$username || !$password) + { + $login = false; + $responseHead = "400"; + $json = json_encode(["error" => "Not enough params"]); + } + else if (password_verify($password, Handler::class->getPasswordHash($username))) + + if (!$login) + { + Utils::class->responeJson($conn, $responseHead, $json); $conn->close(); return; } - $json = json_encode(["token" => "token"]); - Utils::class->sendJson($conn, "200 OK", $json); + Handler::class->addUser($username, $password); + + $json = json_encode(["error" => "none"]); + Utils::class->responeJson($conn, "200", $json); $conn->close(); } diff --git a/src/Http/RegisterController.php b/src/Http/RegisterController.php index 4f3926d..9a2d967 100644 --- a/src/Http/RegisterController.php +++ b/src/Http/RegisterController.php @@ -12,20 +12,44 @@ class RegisterController implements HttpServerInterface public function onOpen(ConnectionInterface $conn, RequestInterface $request = null): void { $params = []; + $createAccount = true; + $responseHead = ""; + $json = ""; parse_str($request->getUri()->getQuery(), $params); $username = $params["username"]; $password = $params["password"]; - if (!$username || !$password) { - $json = json_encode(["error" => "Bad Credentials"]); - Utils::class->sendJson($conn, "404 Bad Request", $json); + if (!$username || !$password) + { + $createAccount = false; + $responseHead = "400"; + $json = json_encode(["error" => "Not enough params"]); + } + else if (count($password) < 5) + { + $createAccount = false; + $responseHead = "400"; + $json = json_encode(["error" => "Short password"]); + } + else if (Handler::class->userExists($username)) + { + $createAccount = false; + $responseHead = "409"; + $json = json_encode(["error" => "Taken"]); + } + + if (!$createAccount) + { + Utils::class->responeJson($conn, $responseHead, $json); $conn->close(); return; } - $json = json_encode(["token" => "token"]); - Utils::class->sendJson($conn, "200 OK", $json); + Handler::class->addUser($username, $password); + + $json = json_encode(["error" => "none"]); + Utils::class->responeJson($conn, "200", $json); $conn->close(); } diff --git a/src/Http/Utils.php b/src/Http/Utils.php index aee932f..6d8d84a 100644 --- a/src/Http/Utils.php +++ b/src/Http/Utils.php @@ -6,7 +6,7 @@ use Ratchet\ConnectionInterface; class Utils { - function sendJson(ConnectionInterface $conn, string $head, string $jsonData): void + function responeJson(ConnectionInterface $conn, string $head, string $jsonData): void { $conn->send("HTTP/1.1 {$head}\r\nContent-Type: application/json\r\n\r\n{$jsonData}"); } diff --git a/src/Security/TokenHandler.php b/src/Security/TokenHandler.php new file mode 100644 index 0000000..72f0d89 --- /dev/null +++ b/src/Security/TokenHandler.php @@ -0,0 +1,13 @@ +