Commit 0b923c74 authored by Frederic Logier's avatar Frederic Logier
Browse files

added a worker api to replay an ftp user with a new token

parent 7c8767b9
......@@ -279,9 +279,9 @@ Service::~Service()
api_app->deleteLater();
thread_api_app->wait();
qDebug() << "api_ftpauth->deleteLater();";
api_ftpauth->deleteLater();
thread_api_ftpauth->wait();
qDebug() << "api_ftp->deleteLater();";
api_ftp->deleteLater();
thread_api_ftp->wait();
/* delete(api_node);
delete(api_workflow);
......@@ -392,14 +392,14 @@ void Service::Http_api_init()
thread_api_app->start();
thread_api_ftpauth = new QThread(this);
api_ftpauth = new Api_ftpauth(m_ncs_params.base_directory, port + 105);
thread_api_ftp = new QThread(this);
api_ftp = new Api_ftp(m_ncs_params.base_directory, port + 105);
connect(thread_api_ftpauth, SIGNAL(started()), api_ftpauth, SLOT(init()));
connect(api_ftpauth, SIGNAL(destroyed()), thread_api_ftpauth, SLOT(quit()), Qt::DirectConnection);
connect(thread_api_ftp, SIGNAL(started()), api_ftp, SLOT(init()));
connect(api_ftp, SIGNAL(destroyed()), thread_api_ftp, SLOT(quit()), Qt::DirectConnection);
api_ftpauth->moveToThread(thread_api_ftpauth);
thread_api_ftpauth->start();
api_ftp->moveToThread(thread_api_ftp);
thread_api_ftp->start();
}
......
......@@ -97,7 +97,7 @@ public:
Api_user *api_user;
Api_session *api_session;
Api_app *api_app;
Api_ftpauth *api_ftpauth;
Api_ftp *api_ftp;
private:
......@@ -109,7 +109,7 @@ private:
QThread *thread_api_user;
QThread *thread_api_session;
QThread *thread_api_app;
QThread *thread_api_ftpauth;
QThread *thread_api_ftp;
QThread *thread_xmpp_server;
......
......@@ -234,6 +234,70 @@ void Worker_api::get_ftp_users(bson::bo a_payload)
}
/********** RESEND FTP USER WITH A NEW TOKEN ************/
void Worker_api::replay_ftp_user(bson::bo a_payload)
{
std::cout << "Worker_api::replay_ftp_user : " << a_payload << std::endl;
QString dest = QString::fromStdString(a_payload.getFieldDotted("payload.dest").str());
QString login = QString::fromStdString(a_payload.getFieldDotted("payload.login").str());
if ("self" == dest)
{
QString node_uuid = QString::fromStdString(a_payload.getField("node_uuid").str());
QString worker_name = QString::fromStdString(a_payload.getField("worker_name").str());
QString worker_uuid = QString::fromStdString(a_payload.getField("uuid").str());
dest = node_uuid + "." + worker_name + "." + worker_uuid + " @";
}
else dest.append(" @");
// BSONObj search = BSON("dest" << a_payload.getFieldDotted("payload.from").str() << "payload_type" << a_payload.getFieldDotted("payload.payload_type"));
// QList <BSONObj> pubsub_payloads_list = mongodb_->FindAll("pubsub_payloads", search);
BSONObj search = BSON("email" << login.toStdString() << "ftp.activated" << true);
BSONObj ftp_user = mongodb_->Find("users", search);
if (ftp_user.nFields() != 0)
{
QUuid ftp_token = QUuid::createUuid();
QString str_ftp_token = ftp_token.toString().remove(QChar('-')).mid(1,32);
BSONObj update = BSON("ftp.token" << str_ftp_token.toStdString());
BSONObj search = BSON("_id" << ftp_user.getField("_id"));
if (mongodb_->Update("users", search, update))
{
qDebug() << "NEW FTP TOKEN : " << login << " TOKEN : " << str_ftp_token;
QString payload = dest;
QString json = "{\"email\": \"" + QString::fromStdString(ftp_user.getField("email").str()) + "\", " +
"\"password\": \"" + str_ftp_token + "\", " +
"\"path\": \"" + QString::fromStdString(ftp_user.getFieldDotted("ftp.directory").str()) + "\"}";
payload.append(json);
qDebug() << "Worker_api::replay_ftp_user payload send : " << payload;
/****** REPLAY API PAYLOAD *******/
qDebug() << "Worker_api::replay_ftp_user SEND PAYLOAD";
QByteArray s_payload = payload.toAscii();
zmq::message_t z_message(s_payload.size()+1);
memcpy(z_message.data(), s_payload.constData(), s_payload.size()+1);
z_publish_api->send(z_message);
/************************/
}
}
}
/********** RESEND PAYLOAD PAYLOAD ************/
void Worker_api::replay_pushpull_payload(bson::bo a_payload)
{
......@@ -359,6 +423,12 @@ void Worker_api::receive_payload()
get_ftp_users(payload.copy());
}
else if (payload_action == "replay_ftp_user")
{
std::cout << "RECEIVE REPLAY FTP USER : " << payload << std::endl;
replay_ftp_user(payload.copy());
}
else if (payload_action == "push")
{
std::cout << "Worker_api::payload CREATE PAYLOAD : " << payload <<std::endl;
......
......@@ -35,6 +35,7 @@ public:
private:
void replay_pubsub_payload(bson::bo l_payload);
void get_ftp_users(bson::bo a_payload);
void replay_ftp_user(bson::bo a_payload);
QSocketNotifier *check_payload;
......
......@@ -1402,24 +1402,24 @@ void Api_app::receive_http_payload()
/*********************/
Api_ftpauth::Api_ftpauth(QString basedirectory, int port) : Zerogw(basedirectory, port)
Api_ftp::Api_ftp(QString basedirectory, int port) : Zerogw(basedirectory, port)
{
std::cout << "Api_ftpauth::Api_ftpauth constructeur" << std::endl;
std::cout << "Api_ftp::Api_ftp constructor" << std::endl;
enumToZerogwHeaderFtpauth.insert(0, HTTP_METHOD);
enumToZerogwHeaderFtpauth.insert(1, URI);
enumToZerogwHeaderFtpauth.insert(2, X_user_email);
enumToZerogwHeaderFtpauth.insert(3, X_user_password);
enumToZerogwHeaderFtp.insert(0, HTTP_METHOD);
enumToZerogwHeaderFtp.insert(1, URI);
enumToZerogwHeaderFtp.insert(2, X_user_email);
enumToZerogwHeaderFtp.insert(3, X_user_password);
}
void Api_ftpauth::receive_http_payload()
void Api_ftp::receive_http_payload()
{
check_http_data->setEnabled(false);
std::cout << "Api_ftpauth::receive_payload" << std::endl;
std::cout << "Api_ftp::receive_payload" << std::endl;
QHash <QString, QString> zerogw;
QString key;
......@@ -1436,11 +1436,11 @@ void Api_ftpauth::receive_http_payload()
std::size_t eventsSize = sizeof(events);
m_socket_zerogw->getsockopt(ZMQ_RCVMORE, &events, &eventsSize);
std::cout << "Api_ftpauth::receive_payload received request: [" << (char*) request.data() << "]" << std::endl;
std::cout << "Api_ftp::receive_payload received request: [" << (char*) request.data() << "]" << std::endl;
QString data_from_zerogw;
switch(enumToZerogwHeaderFtpauth[counter])
switch(enumToZerogwHeaderFtp[counter])
{
case HTTP_METHOD:
// check METHOD
......@@ -1463,7 +1463,10 @@ void Api_ftpauth::receive_http_payload()
else
{
data_from_zerogw = QString::fromAscii((char*)request.data(), request.size());
zerogw["URI"] = data_from_zerogw;
QList <QString> uri_params = data_from_zerogw.split("/");
qDebug() << "URI PARAMS " << uri_params[2];
zerogw["URI"] = uri_params[2];
}
break;
......@@ -1504,20 +1507,38 @@ void Api_ftpauth::receive_http_payload()
cipher.addData(zerogw["X-user-password"].simplified().toAscii());
QByteArray password_hash = cipher.result();
qDebug() << "Api_ftpauth::receive_payload password_hash : " << password_hash.toHex();
qDebug() << "Api_ftp::receive_payload password_hash : " << password_hash.toHex();
BSONObj bauth = BSON("email" << zerogw["X-user-email"].toStdString() << "password" << QString::fromLatin1(password_hash.toHex()).toStdString());
BSONObj l_user = mongodb_->Find("users", bauth);
if (l_user.nFields() == 0)
{
qDebug() << "Api_ftpauth::receive_payload auth failed !";
qDebug() << "Api_ftp::receive_payload auth failed !";
bodyMessage = buildResponse("error", "auth", "unknown user");
}
else
{
bodyMessage = buildResponse("token", QString::fromStdString(l_user.getFieldDotted("ftp.token").str()));
qDebug() << "Api_ftpauth::receive_payload TOKEN : " << bodyMessage;
if (zerogw["URI"] == "auth")
{
bodyMessage = buildResponse("token", QString::fromStdString(l_user.getFieldDotted("ftp.token").str()));
qDebug() << "Api_ftp::receive_payload TOKEN : " << bodyMessage;
}
/* else if (zerogw["URI"] == "renew")
{
QUuid ftp_token = QUuid::createUuid();
QString str_ftp_token = ftp_token.toString().remove(QChar('-')).mid(1,32);
QBool res = mongodb_->Update("users", BSON("_id" << l_user.getField("_id")), BSON("ftp.token" << str_ftp_token.toStdString()));
bodyMessage = buildResponse("status", res? "ok" : "ko");
QString command = "{\"email\": \"" + QString::fromStdString(b_user.getField("email").str()) + "\", \"password\": \"" + str_ftp_token + "\", \"path\": \"" + str_ftp_directory + "\"}";
emit reload_ftp_user("nodeftp", command);
qDebug() << "EMIT CREATE FTP USER";
}*/
else bodyMessage = buildResponse("error", "URI", "unknown");
}
}
zmq::message_t m_message(bodyMessage.length());
......
......@@ -67,7 +67,7 @@ enum ZerogwHeader {
typedef QMap<int, ZerogwHeader> IntToZerogwHeaderPayload;
typedef QMap<int, ZerogwHeader> IntToZerogwHeaderSession;
typedef QMap<int, ZerogwHeader> IntToZerogwHeaderApp;
typedef QMap<int, ZerogwHeader> IntToZerogwHeaderFtpauth;
typedef QMap<int, ZerogwHeader> IntToZerogwHeaderFtp;
......@@ -206,14 +206,14 @@ private slots:
};
class Api_ftpauth : public Zerogw
class Api_ftp : public Zerogw
{
Q_OBJECT
public:
Api_ftpauth(QString basedirectory, int port);
Api_ftp(QString basedirectory, int port);
private:
IntToZerogwHeaderFtpauth enumToZerogwHeaderFtpauth;
IntToZerogwHeaderFtp enumToZerogwHeaderFtp;
private slots:
void receive_http_payload();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment