protocol auth fixed

This commit is contained in:
Samy Ben Tayeb 2025-06-16 19:15:09 +02:00
parent 4947a95899
commit 810260b05a
6 changed files with 60 additions and 31 deletions

View file

@ -6,7 +6,7 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */ /* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/20 21:50:32 by rparodi #+# #+# */ /* Created: 2025/05/20 21:50:32 by rparodi #+# #+# */
/* Updated: 2025/06/14 23:08:11 by sben-tay ### ########.fr */ /* Updated: 2025/06/16 18:01:10 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -41,4 +41,5 @@ public:
std::string getPassword() const; std::string getPassword() const;
void showInfo() const; void showInfo() const;
void printUsers() const; void printUsers() const;
void disconnectClient(int fd);
}; };

View file

@ -6,7 +6,7 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */ /* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/20 21:57:49 by rparodi #+# #+# */ /* Created: 2025/05/20 21:57:49 by rparodi #+# #+# */
/* Updated: 2025/06/08 21:59:53 by sben-tay ### ########.fr */ /* Updated: 2025/06/16 18:36:01 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,7 +26,9 @@ class User
std::string _username; std::string _username;
bool _hasNick; bool _hasNick;
bool _hasUser; bool _hasUser;
bool _hasPass; // to check if the user has sent a PASS command bool _hasPass;
bool _passReceived;
bool _passIsValid;
public: public:
User(short unsigned fd); User(short unsigned fd);
@ -46,6 +48,8 @@ class User
void setHasNick(bool value); void setHasNick(bool value);
void setHasUser(bool value); void setHasUser(bool value);
void setHasPass(bool value); void setHasPass(bool value);
void setPassReceived(bool value);
void setPassIsValid(bool value);
bool getHasPass() const; bool getHasPass() const;
std::string getNickname() const; std::string getNickname() const;

View file

@ -6,7 +6,7 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */ /* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */ /* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */
/* Updated: 2025/06/12 17:52:12 by sben-tay ### ########.fr */ /* Updated: 2025/06/16 18:30:15 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,9 +21,8 @@ e_code cmd::Nick::checkArgs() {
return ERR_NONICKNAMEGIVEN; return ERR_NONICKNAMEGIVEN;
} }
User* existing = searchList<User*>(_users, _args[1]); // à adapter si besoin User* existing = searchList<User*>(_users, _args[1]); // à adapter si besoin
if (existing != NULL) { if (existing != NULL) {
_sender->appendToWriteBuffer(":" + _sender->getPrefix() + " 433 * " + _args[1] + " :Nickname is already in use\r\n");
WARNING_MSG("Nick already in use: " << _args[1]);
return ERR_NICKNAMEINUSE; return ERR_NICKNAMEINUSE;
} }
return _PARSING_OK; return _PARSING_OK;

View file

@ -6,7 +6,7 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */ /* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */ /* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */
/* Updated: 2025/06/12 17:52:35 by sben-tay ### ########.fr */ /* Updated: 2025/06/16 17:33:20 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -29,17 +29,22 @@ e_code Pass::checkArgs() {
* @note To connect a user with the password * @note To connect a user with the password
*/ */
void Pass::execute() { void cmd::Pass::execute() {
if (checkArgs() != _PARSING_OK) { if (_args.size() < 2) {
ERROR_MSG("Invalid arguments for Pass command (see warning message)"); _sender->appendToWriteBuffer(":localhost 461 PASS :Not enough parameters\r\n");
DEBUG_MSG("skill issues");
return;
}
DEBUG_MSG("mais pas trop skill issues");
if (_args.at(1) != _server->getPassword()) {
ERROR_MSG("The password is incorrect");
return; return;
} }
_sender->setHasPass(true); _sender->setHasPass(true);
_sender->setPassReceived(true);
if (_args[1] == _server->getPassword())
{
DEBUG_MSG("PASSISVALID");
_sender->setPassIsValid(true);
}
else{
DEBUG_MSG("PASSISINVALID");
_sender->setPassIsValid(false);
}
_sender->checkRegistration(); _sender->checkRegistration();
} }

View file

@ -6,7 +6,7 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */ /* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/13 11:11:07 by rparodi #+# #+# */ /* Created: 2025/05/13 11:11:07 by rparodi #+# #+# */
/* Updated: 2025/06/14 23:16:54 by sben-tay ### ########.fr */ /* Updated: 2025/06/16 18:35:26 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -95,8 +95,7 @@ void Server::start()
// Handle disconnected clients // Handle disconnected clients
for (size_t i = 0; i < disconnected.size(); ++i) for (size_t i = 0; i < disconnected.size(); ++i)
{ {
delete _users[disconnected[i]]; disconnectClient(disconnected[i]);
_users.erase(disconnected[i]);
} }
for (size_t i = 0; i < readyClients.size(); ++i) for (size_t i = 0; i < readyClients.size(); ++i)
{ {
@ -132,7 +131,6 @@ void Server::start()
_users[fd]->clearWriteBuffer(); _users[fd]->clearWriteBuffer();
} }
} }
} }
std::cout << "Poll loop finished" << std::endl; std::cout << "Poll loop finished" << std::endl;
} }
@ -203,4 +201,13 @@ std::vector<std::string> splitLines(const std::string& input) {
lines.push_back(line); lines.push_back(line);
} }
return lines; return lines;
}
void Server::disconnectClient(int fd) {
_pollManager.removeClient(fd);
close(fd);
if (_users.count(fd)) {
delete _users[fd];
_users.erase(fd);
}
} }

View file

@ -6,15 +6,16 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */ /* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/21 20:37:12 by omoudni #+# #+# */ /* Created: 2025/05/21 20:37:12 by omoudni #+# #+# */
/* Updated: 2025/06/14 23:27:43 by sben-tay ### ########.fr */ /* Updated: 2025/06/16 18:36:36 by sben-tay ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "core.hpp" #include "core.hpp"
#include <unistd.h>
// Constructor // Constructor
User::User(short unsigned int fd) : _fd(fd), _registered(false), _hasNick(false), _hasUser(false), \ User::User(short unsigned int fd) : _fd(fd), _registered(false), _hasNick(false), _hasUser(false), \
_hasPass(false) {} _hasPass(false), _passReceived(false), _passIsValid(false) {}
/** /**
* @brief Getter for the fd * @brief Getter for the fd
@ -35,7 +36,6 @@ std::string User::getName() const {
void User::setUsername(const std::string &username) { void User::setUsername(const std::string &username) {
_username = username; _username = username;
_hasUser = true; _hasUser = true;
checkRegistration();
} }
// Setter for nickname (with basic checks) // Setter for nickname (with basic checks)
@ -51,7 +51,6 @@ void User::setNickname(const std::string &nickname) {
} else { } else {
_nickname = nickname; _nickname = nickname;
_hasNick = true; _hasNick = true;
checkRegistration();
} }
} }
@ -83,14 +82,24 @@ void User::appendToWriteBuffer(const std::string &data)
// Check registration // Check registration
void User::checkRegistration() void User::checkRegistration()
{ {
if (!_registered && _hasNick && _hasUser && _hasPass) if (_registered)
return;
if (_hasNick && _hasUser && _passReceived && _passIsValid)
{ {
std::cout << "JE SUIS ENREGISTRE" << std::endl;
_registered = true; _registered = true;
std::string welcome = ":localhost 001 " + _nickname + std::string msg = ":localhost 001 " + _nickname +
" :Welcome to the IRC server " + getPrefix() + "\r\n"; " :Welcome to the IRC server " + getPrefix() + "\r\n";
appendToWriteBuffer(msg);
appendToWriteBuffer(welcome); }
else if (_hasNick && _hasUser && _passReceived && !_passIsValid)
{
std::string msg = ":localhost 464 * :Password incorrect\r\n";
appendToWriteBuffer(msg);
}
else if (_hasNick && _hasUser && !_passReceived)
{
std::string msg = ":localhost 451 * :You must send PASS to register\r\n";
appendToWriteBuffer(msg);
} }
} }
@ -123,6 +132,10 @@ void User::setHasUser(bool value) { _hasUser = value; }
void User::setHasPass(bool value) { _hasPass = value; } void User::setHasPass(bool value) { _hasPass = value; }
void User::setPassReceived(bool value) { _passReceived = value; }
void User::setPassIsValid(bool value) { _passIsValid = value; }
bool User::getHasPass() const { return _hasPass; } bool User::getHasPass() const { return _hasPass; }
std::string User::getNickname() const { return _nickname; } std::string User::getNickname() const { return _nickname; }