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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
void showInfo() const;
void printUsers() const;
void disconnectClient(int fd);
};

View file

@ -6,7 +6,7 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
bool _hasNick;
bool _hasUser;
bool _hasPass; // to check if the user has sent a PASS command
bool _hasPass;
bool _passReceived;
bool _passIsValid;
public:
User(short unsigned fd);
@ -46,6 +48,8 @@ class User
void setHasNick(bool value);
void setHasUser(bool value);
void setHasPass(bool value);
void setPassReceived(bool value);
void setPassIsValid(bool value);
bool getHasPass() const;
std::string getNickname() const;

View file

@ -6,7 +6,7 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */
@ -22,8 +22,7 @@ e_code cmd::Nick::checkArgs() {
}
User* existing = searchList<User*>(_users, _args[1]); // à adapter si besoin
if (existing != NULL) {
WARNING_MSG("Nick already in use: " << _args[1]);
_sender->appendToWriteBuffer(":" + _sender->getPrefix() + " 433 * " + _args[1] + " :Nickname is already in use\r\n");
return ERR_NICKNAMEINUSE;
}
return _PARSING_OK;

View file

@ -6,7 +6,7 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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
*/
void Pass::execute() {
if (checkArgs() != _PARSING_OK) {
ERROR_MSG("Invalid arguments for Pass command (see warning message)");
DEBUG_MSG("skill issues");
return;
}
DEBUG_MSG("mais pas trop skill issues");
if (_args.at(1) != _server->getPassword()) {
ERROR_MSG("The password is incorrect");
void cmd::Pass::execute() {
if (_args.size() < 2) {
_sender->appendToWriteBuffer(":localhost 461 PASS :Not enough parameters\r\n");
return;
}
_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();
}

View file

@ -6,7 +6,7 @@
/* By: sben-tay <sben-tay@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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
for (size_t i = 0; i < disconnected.size(); ++i)
{
delete _users[disconnected[i]];
_users.erase(disconnected[i]);
disconnectClient(disconnected[i]);
}
for (size_t i = 0; i < readyClients.size(); ++i)
{
@ -132,7 +131,6 @@ void Server::start()
_users[fd]->clearWriteBuffer();
}
}
}
std::cout << "Poll loop finished" << std::endl;
}
@ -204,3 +202,12 @@ std::vector<std::string> splitLines(const std::string& input) {
}
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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <unistd.h>
// Constructor
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
@ -35,7 +36,6 @@ std::string User::getName() const {
void User::setUsername(const std::string &username) {
_username = username;
_hasUser = true;
checkRegistration();
}
// Setter for nickname (with basic checks)
@ -51,7 +51,6 @@ void User::setNickname(const std::string &nickname) {
} else {
_nickname = nickname;
_hasNick = true;
checkRegistration();
}
}
@ -83,14 +82,24 @@ void User::appendToWriteBuffer(const std::string &data)
// Check registration
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;
std::string welcome = ":localhost 001 " + _nickname +
std::string msg = ":localhost 001 " + _nickname +
" :Welcome to the IRC server " + getPrefix() + "\r\n";
appendToWriteBuffer(welcome);
appendToWriteBuffer(msg);
}
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::setPassReceived(bool value) { _passReceived = value; }
void User::setPassIsValid(bool value) { _passIsValid = value; }
bool User::getHasPass() const { return _hasPass; }
std::string User::getNickname() const { return _nickname; }