89 lines
2.5 KiB
C++
89 lines
2.5 KiB
C++
#include "Request.h"
|
|
#include "Response.h"
|
|
#include "Session.h"
|
|
#include "Util.h"
|
|
#include "json.hpp"
|
|
using namespace std;
|
|
using json=nlohmann::json;
|
|
|
|
int main()
|
|
{
|
|
Request req;
|
|
Session se(req);
|
|
Response res;
|
|
json j;
|
|
|
|
auto jsonfail=[&](int errcode,const std::string& errmsg)
|
|
{
|
|
j["success"]=0;
|
|
j["errcode"]=errcode;
|
|
j["errmsg"]=errmsg;
|
|
};
|
|
|
|
if(req.requestMethod=="POST")
|
|
{
|
|
if(req.post["username"].empty() ||
|
|
req.post["password"].empty())
|
|
{
|
|
jsonfail(2,"Missing Parameter");
|
|
}
|
|
else
|
|
{
|
|
DBInfo db;
|
|
if(db.readConfig()<0)
|
|
{
|
|
jsonfail(3,"Failed to read configure");
|
|
}
|
|
else
|
|
{
|
|
string uname=req.post["username"];
|
|
string upass=req.post["password"];
|
|
MySQLConn conn;
|
|
if(db.connectProxy(conn)<0)
|
|
{
|
|
jsonfail(4,"Failed to connect DB");
|
|
}
|
|
else
|
|
{
|
|
bool authpass=false;
|
|
if(conn.exec(make_str("select count(username) from bs_user where username='",uname,"' and password='",upass,"'"),
|
|
[&](MySQLResult& res)
|
|
{
|
|
res.stepRow([&](char** val,unsigned long* len)
|
|
{
|
|
if(string(val[0])=="1")
|
|
{
|
|
authpass=true;
|
|
}
|
|
});
|
|
})<0)
|
|
{
|
|
jsonfail(5,"Failed to execute SQL.");
|
|
}
|
|
else
|
|
{
|
|
/// Try to mark session logged in
|
|
if(se.setUser(uname)<0)
|
|
{
|
|
jsonfail(6,"Failed to set session.");
|
|
}
|
|
else
|
|
{
|
|
j["success"]=1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
jsonfail(1,"Request Method Not Supported");
|
|
}
|
|
|
|
res.content.append(j.dump());
|
|
res.show();
|
|
|
|
return 0;
|
|
}
|