-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuser_controller.cc
123 lines (96 loc) · 3.25 KB
/
user_controller.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <json/json.h>
#include "pgsql.h"
#include <iomanip>
#include <openssl/sha.h>
#include "jwt_controller.h"
#include "msg_controller.h"
using namespace drogon;
typedef void (*HandlerFunc)(const Json::Value &, std::string *, int *);
void userInit(const std::string& username, const std::string& nonce)
{
std::string sender="FlypenTeam";
std::string message="Welcome to flypen! We are glad to see you here!";
sql_addhistory( sender, username, message, nonce, "0");
return ;
}
void Handle(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, HandlerFunc handler)
{
auto body = req->getBody();
Json::Value req_json, res_json;
Json::Reader reader;
std::string bodyStr(body);
if (!reader.parse(bodyStr, req_json))
{
callback(HttpResponse::newHttpResponse());
return;
}
Json::FastWriter writer;
std::string msg;
int code;
handler(req_json, &msg, &code);
res_json["msg"] = msg;
res_json["code"] = code;
if (msg.find("Success") != std::string::npos)
{
res_json["token"] = jwtGen(req_json);
res_json["username"] = req_json["username"].asString();
}
auto output = writer.write(res_json);
auto res = HttpResponse::newHttpResponse();
res->addHeader("Access-Control-Allow-Origin", "*");
res->setBody(output);
callback(res);
}
std::string sha256(const std::string& str)
{
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, str.c_str(), str.size());
SHA256_Final(hash, &sha256);
std::stringstream ss;
for (unsigned char i : hash)
ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(i);
return ss.str();
}
void registerUser(const Json::Value &req_json, std::string *msg, int *code)
{
if (sql_check(req_json["username"].asString()))
{
sql_add(req_json["username"].asString(), sha256(req_json["password"].asString()), req_json["avatar"].asInt(), req_json["public_key"].asString());
*msg = "Sign up Success";
std::string nonce = req_json["nonce"].asString();
userInit(req_json["username"].asString(), nonce);
*code = 200;
}
else
{
*msg = "User already exist";
*code = 409;
}
}
void loginUser(const Json::Value &req_json, std::string *msg, int *code)
{
if (sql_check(req_json["username"].asString(), sha256(req_json["password"].asString())))
{
*msg = "Login Success";
*code = 200;
std::cout<<"INFO: "<<req_json["username"].asString()<<" login"<<std::endl;
}
else
{
*msg = "Login Failed";
*code = 401;
}
}
void avatar(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
{
auto res = HttpResponse::newHttpResponse();
res->addHeader("Access-Control-Allow-Origin", "*");
if (!jwtVerify(req))
res->setBody("No Authorization");
std::string receiver = jwtDecrypt(req->getHeader("Authorization").substr(7));
set_avatar(receiver, stoi(req->getParameter("avatar")));
res->setBody("Success");
callback(res);
}