Commit beccb4df authored by fred's avatar fred
Browse files

added user api

parent 35a3726b
......@@ -98,29 +98,36 @@ First, create an admin user. Then you can create users, nodes and workflows, or
### API USE
1. create a node
1. create a user
```bash
curl -H "X-user-token: admin-token" -X POST -d '{ "email": "user@email.com", "password": "password", "ftp": true, "tracker": false, "xmpp": false, "api": false}' http://127.0.0.1:2502/user
return user token {"token":"8c70645c-b12b-4b5e-b998-e08158d09bdd"}%
```
2. create a node
```bash
curl -H "X-user-token: your-token" -X POST http://127.0.0.1:2502/node/nodename
return node auth : {"node_password":"e6cc13a3-1236-46cb-b40f-a66650ab5eef","node_uuid":"2d0a7780-e8fe-4e0a-89c6-a5a2737b095a"}
```
2. create a workflow
3. create a workflow
```bash
curl -H "X-user-token: user-token" -X POST -d '{ "worker1": 1, "worker2": 2 }' http://127.0.0.1:2502/workflow/workflowname
return a workflow id : {"uuid":"0ebcdab6-0263-42d3-be7d-9602fa15f68c"}
```
3. SEND DATA
4. SEND DATA
3a. push binary data
4a. push binary data
```bash
curl -H "X-node-uuid: 2d0a7780-e8fe-4e0a-89c6-a5a2737b095a" -H "X-node-password: 2d0a7780-e8fe-4e0a-89c6-a5a2737b095a" -H "X-workflow-uuid: 0ebcdab6-0263-42d3-be7d-9602fa15f68c" -H "X-payload-filename: filename" -H "X-payload-type: filetype" -X POST --data-binary @filename http://127.0.0.1:2502/payload/push
```
3b. push json data
4b. push json data
```bash
curl -H "X-node-uuid: 2d0a7780-e8fe-4e0a-89c6-a5a2737b095a" -H "X-node-password: 2d0a7780-e8fe-4e0a-89c6-a5a2737b095a" -H "X-workflow-uuid: 0ebcdab6-0263-42d3-be7d-9602fa15f68c" -d '{ "data1": "mydata", "data2": "mydata" }' http://127.0.0.1:2502/payload/push
......
......@@ -38,7 +38,7 @@
<div class="group">
<label class="label">Options</label>
<div>
<input type="checkbox" name="workers" id="checkbox_workers" class="checkbox" value="workers"> <label for="checkbox_workers" class="checkbox">Workers</label>
<input type="checkbox" name="api" id="checkbox_api" class="checkbox" value="api"> <label for="checkbox_api" class="checkbox">API</label>
</div>
<div>
<input type="checkbox" name="ftp" id="ftp" class="checkbox" value="ftp"> <label for="checkbox_ftp" class="checkbox">FTP</label>
......
......@@ -839,10 +839,10 @@ void Http_admin::admin_user_post(QxtWebRequestEvent* event)
}
qDebug() << "form_field : " << form_field;
bool workers = form_field.contains("workers")? true : false;
bool api = form_field.contains("api")||create_admin? true : false;
bool ftp = form_field.contains("ftp")||create_admin? true : false;
bool xmpp = form_field.contains("xmpp")? true : false;
bool bittorrent = form_field.contains("bittorrent")? true : false;
bool xmpp = form_field.contains("xmpp")||create_admin? true : false;
bool bittorrent = form_field.contains("bittorrent")||create_admin? true : false;
QUuid token = QUuid::createUuid();
QString str_token = token.toString().mid(1,36);
......@@ -853,6 +853,13 @@ void Http_admin::admin_user_post(QxtWebRequestEvent* event)
QUuid ftp_token = QUuid::createUuid();
QString str_ftp_token = ftp_token.toString().remove(QChar('-')).mid(1,32);
QUuid xmpp_token = QUuid::createUuid();
QString str_xmpp_token = xmpp_token.toString().remove(QChar('-')).mid(1,32);
QUuid api_token = QUuid::createUuid();
QString str_api_token = api_token.toString().remove(QChar('-')).mid(1,32);
QCryptographicHash cipher( QCryptographicHash::Sha1 );
cipher.addData(form_field["password"].simplified().toAscii());
QByteArray password_hash = cipher.result();
......@@ -864,7 +871,10 @@ void Http_admin::admin_user_post(QxtWebRequestEvent* event)
"email" << form_field["email"].toStdString() <<
"token" << str_token.toStdString() <<
"ftp" << BSON ("token" << str_ftp_token.toStdString() << "activated" << ftp << "directory" << "default") <<
"tracker" << BSON ("token" << str_tracker_token.toStdString()));
"tracker" << BSON ("token" << str_tracker_token.toStdString()) << "activated" << bittorrent <<
"xmpp" << BSON ("token" << str_xmpp_token.toStdString()) << "activated" << xmpp <<
"api" << BSON ("token" << str_api_token.toStdString()) << "activated" << api);
mongodb_->Insert("users", t_user);
if (ftp) {
......
......@@ -110,7 +110,6 @@ QBool Http_api::checkAuth(QString token, BSONObjBuilder &payload_builder, BSONOb
/********** REGISTER API ************/
void Http_api::node(QxtWebRequestEvent* event, QString name)
{
QxtWebPageEvent *page;
......@@ -341,6 +340,171 @@ void Http_api::workflow(QxtWebRequestEvent* event, QString name)
/********** CREATE USER ************/
void Http_api::user(QxtWebRequestEvent* event)
{
QxtWebPageEvent *page;
QxtHtmlTemplate body;
switch (enumToHTTPmethod[event->method.toUpper()])
{
case POST:
{
qDebug() << "CREATE USER " << " headers : " << event->headers;
QString bodyMessage;
if (event->content.isNull())
{
bodyMessage = buildResponse("error", "data", "empty json");
postEvent(new QxtWebPageEvent(event->sessionID,
event->requestID,
bodyMessage.toUtf8()));
return;
}
QString user_token = event->headers.value("X-user-token");
qDebug() << "user_token : " << user_token << " length " << user_token.length();
if (user_token.length() == 0)
{
bodyMessage = buildResponse("error", "headers", "X-use-token");
postEvent(new QxtWebPageEvent(event->sessionID,
event->requestID,
bodyMessage.toUtf8()));
return;
}
BSONObjBuilder check_user_builder;
BSONObj check_user;
QBool res = checkAuth(user_token, check_user_builder, check_user);
if (!res)
{
bodyMessage = buildResponse("error", "auth");
}
else if (!check_user.getBoolField("admin"))
{
bodyMessage = buildResponse("error", "user", "admin only");
postEvent(new QxtWebPageEvent(event->sessionID,
event->requestID,
bodyMessage.toUtf8()));
return;
}
else
{
BSONObjBuilder user_builder;
user_builder.genOID();
QxtWebContent *myContent = event->content;
qDebug() << "Bytes to read: " << myContent->unreadBytes();
myContent->waitForAllContent();
//QByteArray requestContent = QByteArray::fromBase64(myContent->readAll());
QByteArray requestContent = myContent->readAll();
QString content = requestContent;
qDebug() << "requestCONTENT : " << content.toAscii();
BSONObj b_user;
try {
b_user = mongo::fromjson(content.toAscii());
std::cout << "b_user : " << b_user << std::endl;
if (!b_user.hasField("email") || !b_user.hasField("password"))
{
bodyMessage = buildResponse("error", "json", "missing email or password");
postEvent(new QxtWebPageEvent(event->sessionID,
event->requestID,
bodyMessage.toUtf8()));
return;
}
QUuid token = QUuid::createUuid();
QString str_token = token.toString().mid(1,36);
QUuid tracker_token = QUuid::createUuid();
QString str_tracker_token = tracker_token.toString().remove(QChar('-')).mid(1,32);
QUuid ftp_token = QUuid::createUuid();
QString str_ftp_token = ftp_token.toString().remove(QChar('-')).mid(1,32);
QUuid xmpp_token = QUuid::createUuid();
QString str_xmpp_token = xmpp_token.toString().remove(QChar('-')).mid(1,32);
QUuid api_token = QUuid::createUuid();
QString str_api_token = api_token.toString().remove(QChar('-')).mid(1,32);
QString password = QString::fromStdString(b_user.getField("password").str());
QCryptographicHash cipher( QCryptographicHash::Sha1 );
cipher.addData(password.simplified().toAscii());
QByteArray password_hash = cipher.result();
qDebug() << "password_hash : " << password_hash.toHex();
QString login = b_user.hasField("login")? QString::fromStdString(b_user.getField("login").str()) : "anonymous";
bool ftp = b_user.hasField("ftp")? b_user.getBoolField("ftp") : false;
bool tracker = b_user.hasField("tracker")? b_user.getBoolField("tracker") : false;
bool xmpp = b_user.hasField("xmpp")? b_user.getBoolField("xmpp") : false;
bool api = b_user.hasField("api")? b_user.getBoolField("api") : false;
user_builder.append("admin", false);
user_builder.append("login", login.toStdString());
user_builder.append(b_user.getField("email"));
user_builder.append("password", QString::fromLatin1(password_hash.toHex()).toStdString());
user_builder.append("token", str_token.toStdString());
user_builder.append("ftp", BSON("token" << str_ftp_token.toStdString() << "activated" << ftp << "directory" << "default"));
user_builder.append("tracker" , BSON("token" << str_tracker_token.toStdString() << "activated" << tracker));
user_builder.append("xmpp" , BSON("token" << str_xmpp_token.toStdString() << "activated" << xmpp));
user_builder.append("api" , BSON("token" << str_api_token.toStdString() << "activated" << api));
BSONObj l_user = user_builder.obj();
mongodb_->Insert("users", l_user);
bodyMessage = buildResponse("token", str_token);
}
catch (mongo::MsgAssertionException &e)
{
std::cout << "error on parsing JSON : " << e.what() << std::endl;
bodyMessage = buildResponse("error", "error on parsing JSON");
}
}
qDebug() << bodyMessage;
postEvent(new QxtWebPageEvent(event->sessionID,
event->requestID,
bodyMessage.toUtf8()));
break;
}
case GET:
body["content"]="error 404";
page = new QxtWebPageEvent(event->sessionID,
event->requestID,
body.render().toUtf8());
page->status = 404;
qDebug() << "error 404";
postEvent(page);
break;
}
}
/********** FILE PUSH ************/
......
......@@ -59,6 +59,7 @@ public slots:
void file(QxtWebRequestEvent* event, QString action);
void node(QxtWebRequestEvent* event, QString token);
void workflow(QxtWebRequestEvent* event, QString action);
void user(QxtWebRequestEvent* event);
void staticfile(QxtWebRequestEvent* event, QString directory, QString filename);
private:
......
Supports Markdown
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