jce-manager/src/jce/jcelogin.cpp

214 lines
5.9 KiB
C++
Raw Normal View History

2014-06-18 23:44:46 +00:00
#include "jcelogin.h"
jceLogin::jceLogin(user * username)
{
this->recieverPage = new std::string();
this->jceA = username;
2014-06-26 15:15:04 +00:00
this->JceConnector = new jceSSLClient();
2014-06-18 23:44:46 +00:00
}
jceLogin::~jceLogin()
{
this->jceA = NULL;
delete recieverPage;
delete JceConnector;
JceConnector = NULL;
recieverPage = NULL;
}
/**
* @brief jceLogin::makeConnection Connecting to JCE student web site with JceA (username object) and validate it.
* throws error upon the given error from JCE website or Socket error
*/
void jceLogin::makeConnection() throw (jceStatus)
{
2014-06-20 02:59:33 +00:00
if (this->recieverPage == NULL)
this->recieverPage = new std::string();
2014-06-18 23:44:46 +00:00
if (JceConnector->makeConnect(dst_host,dst_port) == false)
throw jceStatus::ERROR_ON_OPEN_SOCKET;
int returnMode;
2014-06-18 23:44:46 +00:00
jceStatus status = jceStatus::JCE_NOT_CONNECTED;
returnMode = checkConnection();
if (returnMode == true) //connected to host
2014-06-18 23:44:46 +00:00
{
returnMode = makeFirstVisit();
if (returnMode == true) //requst and send first validation
2014-06-18 23:44:46 +00:00
{
status = jceStatus::JCE_START_VALIDATING_PROGRESS;
returnMode = checkValidation();
if (returnMode == true) //check if username and password are matching
2014-06-18 23:44:46 +00:00
{
status = jceStatus::JCE_VALIDATION_PASSED;
returnMode = makeSecondVisit();
if (returnMode == true) //siging in the website
2014-06-18 23:44:46 +00:00
{
status = jceStatus::JCE_YOU_ARE_IN;
setLoginFlag(true);
}
else if (returnMode == jceLogin::ERROR_ON_GETTING_INFO)
{
status = jceLogin::ERROR_ON_GETTING_INFO;
}
else if (returnMode == jceLogin::ERROR_ON_SEND_REQUEST)
{
status = jceLogin::ERROR_ON_SEND_REQUEST;
}
2014-06-18 23:44:46 +00:00
else
status = jceStatus::ERROR_ON_VALIDATION;
}
else
status = jceStatus::ERROR_ON_VALIDATION;
}
else if (returnMode == jceLogin::ERROR_ON_GETTING_INFO)
{
status = jceLogin::ERROR_ON_GETTING_INFO;
}
else if (returnMode == jceLogin::ERROR_ON_SEND_REQUEST)
{
status = jceLogin::ERROR_ON_SEND_REQUEST;
}
2014-06-18 23:44:46 +00:00
else
status = jceStatus::ERROR_ON_VALIDATION_USER_BLOCKED;
}
else
status = jceStatus::JCE_NOT_CONNECTED;
2014-06-18 23:44:46 +00:00
//we throw status even if we are IN!
throw status;
}
bool jceLogin::checkConnection()
{
2014-06-26 15:15:04 +00:00
if (JceConnector->isConnected())
2014-06-18 23:44:46 +00:00
return true;
return false;
}
void jceLogin::reConnect() throw (jceStatus)
{
closeAll();
if (this->JceConnector != NULL)
delete JceConnector;
this->recieverPage = new std::string();
2014-06-26 15:15:04 +00:00
this->JceConnector = new jceSSLClient();
2014-06-18 23:44:46 +00:00
try
{
2014-06-20 02:59:33 +00:00
2014-06-18 23:44:46 +00:00
makeConnection();
}
catch (jceLogin::jceStatus &a)
{
throw a;
}
}
void jceLogin::closeAll()
{
2014-06-20 02:59:33 +00:00
JceConnector->makeDiconnect();
2014-06-18 23:44:46 +00:00
delete recieverPage;
recieverPage = NULL;
2014-06-20 02:59:33 +00:00
loginFlag = false;
2014-06-18 23:44:46 +00:00
}
int jceLogin::makeFirstVisit()
{
std::string usr = jceA->getUsername();
std::string psw = jceA->getPassword();
2014-06-26 15:15:04 +00:00
if (JceConnector->sendData(jceLoginHtmlScripts::makeRequest(jceLoginHtmlScripts::getFirstValidationStep(*jceA))))
2014-06-18 23:44:46 +00:00
{
2014-06-26 15:15:04 +00:00
if (!JceConnector->recieveData(*recieverPage,true))
2014-06-18 23:44:46 +00:00
return jceLogin::ERROR_ON_GETTING_INFO;
}
else
return jceLogin::ERROR_ON_SEND_REQUEST;
return true;
}
int jceLogin::makeSecondVisit()
{
std::string usrid=jceA->getUserID();
std::string pswid=jceA->getHashedPassword();
2014-06-26 15:15:04 +00:00
if ((JceConnector->sendData(jceLoginHtmlScripts::makeRequest(jceLoginHtmlScripts::getSecondValidationStep(*jceA)))))
2014-06-18 23:44:46 +00:00
{
2014-06-26 15:15:04 +00:00
if (!(JceConnector->recieveData(*recieverPage,true)))
2014-06-18 23:44:46 +00:00
return jceLogin::ERROR_ON_GETTING_INFO;
return true;
}
else
return jceLogin::ERROR_ON_SEND_REQUEST;
return true;
}
int jceLogin::getGrades()
{
2014-06-26 15:15:04 +00:00
if ((JceConnector->sendData(jceLoginHtmlScripts::makeRequest(jceLoginHtmlScripts::getGradesPath(*jceA)))))
2014-06-18 23:44:46 +00:00
{
2014-06-26 15:15:04 +00:00
if (!(JceConnector->recieveData(*recieverPage,false)))
2014-06-18 23:44:46 +00:00
return jceLogin::ERROR_ON_GETTING_GRADES;
else
return jceLogin::JCE_GRADE_PAGE_PASSED;
2014-06-18 23:44:46 +00:00
}
else
return jceLogin::ERROR_ON_SEND_REQUEST;
return true;
}
void jceLogin::setLoginFlag(bool x)
{
this->loginFlag = x;
}
bool jceLogin::isLoginFlag() const
{
return this->loginFlag;
}
std::string jceLogin::getPage()
{
return *recieverPage;
}
/**
* @brief jceLogin::checkValidation Made by Nadav Luzzato
* @return true if second validation step is right
*/
bool jceLogin::checkValidation()
{
//finds the hashed password
std::size_t hasspass_position1,hasspass_position2;
if ((hasspass_position1 = recieverPage->find("-A,-N")) == string::npos)
return false;
2014-06-18 23:44:46 +00:00
hasspass_position1 += 5;
if ((hasspass_position2 = recieverPage->find(",-A,-A", hasspass_position1)) == string::npos)
return false;
std::string hasspass = recieverPage->substr(hasspass_position1,hasspass_position2-hasspass_position1);
jceA->setHashedPassword(hasspass);
2014-06-18 23:44:46 +00:00
//finds the user id
std::size_t id_position1 = recieverPage->find("value=\"-N", 0);
id_position1 += 9;
std::size_t id_position2 = recieverPage->find(",-A", id_position1);
if ((id_position2 != std::string::npos) && (id_position1 != std::string::npos))
{
std::string hassid = recieverPage->substr(id_position1,id_position2-id_position1);
jceA->setUserID(hassid);
}
if (((jceA->getUserID()).empty()) || ((jceA->getHashedPassword()).empty()))
return false;
return true;
}