Skip to content

Commit a70089b

Browse files
authored
Merge branch 'main' into main
2 parents 3f15a82 + 32b4355 commit a70089b

File tree

10 files changed

+394
-377
lines changed

10 files changed

+394
-377
lines changed

.DS_Store

2 KB
Binary file not shown.

jwt_controller.cc

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
#include "jwt_controller.h"
21
#include <iostream>
2+
#include <drogon/drogon.h>
33
#include <jwt-cpp/jwt.h>
44
#include <cstring>
55
#include <chrono>
6+
#include "jwt_controller.h"
7+
68
using namespace jwt;
9+
710
std::string jwtGen(const Json::Value& req_json)
811
{
912
auto now = std::chrono::system_clock::now();
@@ -16,11 +19,6 @@ std::string jwtGen(const Json::Value& req_json)
1619
.sign(jwt::algorithm::hs256{"secret"});
1720
return std::string(token);
1821
}
19-
// std::string jwtDecode(const std::string& token)
20-
// {
21-
// auto decoded_token = jwt::decode(token);
22-
// return decoded_token.get_payload_claim("name").as_string();
23-
// }
2422

2523
std::string jwtDecrypt(const std::string& token)
2624
{
@@ -35,4 +33,21 @@ std::string jwtDecrypt(const std::string& token)
3533
std::cout<<"Failed to decrypt JWT: " + std::string(e.what())<<std::endl;
3634
throw std::runtime_error("Failed to decrypt JWT");
3735
}
36+
}
37+
38+
bool jwtVerify(const drogon::HttpRequestPtr &req){
39+
std::string authHeader = req->getHeader("Authorization");
40+
if (authHeader.substr(0, 7) == "Bearer ") {
41+
std::string bearerToken = authHeader.substr(7);
42+
try {
43+
std::string sender = jwtDecrypt(bearerToken);
44+
return true;
45+
} catch (const std::exception &e) {
46+
std::cout << "Wrong token" << std::endl;
47+
return false;
48+
}
49+
} else {
50+
std::cout << "No Authorization" << std::endl;
51+
return false;
52+
}
3853
}

jwt_controller.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
#ifndef _JWT_CONTROLLER_H_
22
#define _JWT_CONTROLLER_H_
3+
4+
#include <drogon/drogon.h>
35
#include <json/json.h>
46
#include <cstring>
7+
58
std::string jwtGen(const Json::Value& req_json);
69
std::string jwtDecrypt(const std::string& token);
10+
bool jwtVerify(const drogon::HttpRequestPtr &req);
11+
712
#endif

main.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ int main() {
3030
drogon::app().registerHandler("/api/info", [](const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) {
3131
info(req, std::move(callback));
3232
});
33+
3334
drogon::app().registerHandler("/api/file/tree", [](const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) {
3435
genTree(req, std::move(callback));
36+
});
37+
drogon::app().registerHandler("/api/avatar", [](const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) {
38+
avatar(req, std::move(callback));
39+
3540
});
3641
drogon::app().run();
3742
return 0;

msg_controller.cc

Lines changed: 67 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -1,182 +1,111 @@
1-
#include "msg_controller.h"
2-
31
#include <drogon/drogon.h>
42
#include <json/json.h>
5-
63
#include "jwt_controller.h"
4+
#include "msg_controller.h"
75
#include "mysql.h"
6+
87
using namespace drogon;
9-
void chat(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
10-
{
8+
9+
// send a message
10+
void chat(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) {
1111
auto body = req->getBody();
1212
Json::Value req_json, res_json;
1313
Json::Reader reader;
1414
std::string bodyStr(body);
15-
if (!reader.parse(bodyStr, req_json))
16-
{
15+
if (!reader.parse(bodyStr, req_json)) {
1716
callback(HttpResponse::newHttpResponse());
1817
return;
1918
}
2019
Json::FastWriter writer;
21-
std::string authHeader = req->getHeader("Authorization");
22-
if (authHeader.substr(0, 7) == "Bearer ")
23-
{
24-
std::string bearerToken = authHeader.substr(7);
25-
// 在此处使用Bearer Token进行身份验证
26-
try
27-
{
28-
std::string sender = jwtDecrypt(bearerToken);
29-
std::string content = req_json["content"].asString();
30-
std::string receiver = req_json["receiver"].asString();
31-
std::cout << "Connect success: " << sender << std::endl;
32-
sql_addhistory(sender, receiver, content, "0");
33-
}
34-
catch (const std::exception &e)
35-
{
36-
std::cerr << e.what() << '\n';
37-
std::cout << "Wrong token" << std::endl;
38-
}
39-
}
40-
else
41-
{
42-
// 连接没有Authorization头部Bearer Token
43-
std::cout << "No Authorization" << std::endl;
44-
}
45-
std::string msg = req_json["content"].asString();
46-
auto output = writer.write(res_json);
4720
auto res = HttpResponse::newHttpResponse();
4821
res->addHeader("Access-Control-Allow-Origin", "*");
49-
res->setBody(output);
22+
if (jwtVerify(req)) {
23+
std::string sender = jwtDecrypt(req->getHeader("Authorization").substr(7));
24+
std::string content = req_json["content"].asString();
25+
std::string receiver = req_json["receiver"].asString();
26+
sql_addhistory(sender, receiver, content, "0");
27+
std::string msg = req_json["content"].asString();
28+
auto output = writer.write(res_json);
29+
res->setBody(output);
30+
} else {
31+
res->setBody("No Authorization");
32+
}
5033
callback(res);
5134
}
52-
void check(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
53-
{
35+
// get message history
36+
void check(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) {
5437
Json::Value res_json;
5538
Json::Reader reader;
5639
std::string me;
5740
Json::FastWriter writer;
58-
std::string authHeader = req->getHeader("Authorization");
59-
if (authHeader.substr(0, 7) == "Bearer ")
60-
{
61-
std::string bearerToken = authHeader.substr(7);
62-
// 在此处使用Bearer Token进行身份验证
63-
try
64-
{
65-
me = jwtDecrypt(bearerToken);
66-
}
67-
catch (const std::exception &e)
68-
{
69-
std::cerr << e.what() << '\n';
70-
std::cout << "Wrong token" << std::endl;
71-
}
72-
}
73-
else
74-
{
75-
// 连接没有Authorization头部Bearer Token
76-
std::cout << "No Authorization" << std::endl;
77-
}
78-
7941
auto res = HttpResponse::newHttpResponse();
8042
res->addHeader("Access-Control-Allow-Origin", "*");
81-
auto output = writer.write(sql_find_my_msg(me));
82-
res->setBody(output);
43+
if (jwtVerify(req)) {
44+
me = jwtDecrypt(req->getHeader("Authorization").substr(7));
45+
auto output = writer.write(sql_find_my_msg(me));
46+
res->setBody(output);
47+
} else {
48+
res->setBody("No Authorization");
49+
}
8350
callback(res);
8451
}
85-
86-
void friend_operation(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
87-
{
88-
std::string authHeader = req->getHeader("Authorization");
89-
if (authHeader.substr(0, 7) == "Bearer ")
90-
{
91-
std::string bearerToken = authHeader.substr(7);
92-
// 在此处使用Bearer Token进行身份验证
93-
try
94-
{
95-
std::string sender = jwtDecrypt(bearerToken);
96-
std::string receiver = req->getParameter("username");
97-
std::string operation = req->getParameter("operation");
98-
if(operation=="add")
52+
// request new friend or cancel request
53+
void friend_operation(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) {
54+
auto res = HttpResponse::newHttpResponse();
55+
res->addHeader("Access-Control-Allow-Origin", "*");
56+
if (jwtVerify(req)) {
57+
std::string sender = jwtDecrypt(req->getHeader("Authorization").substr(7));
58+
std::string receiver = req->getParameter("username");
59+
std::string operation = req->getParameter("operation");
60+
if (operation == "add")
9961
sql_addrequest(sender, receiver);
100-
else sql_delete_operation(sender,receiver);
101-
}
102-
catch (const std::exception &e)
103-
{
104-
std::cerr << e.what() << '\n';
105-
std::cout << "Wrong token" << std::endl;
106-
}
62+
else
63+
sql_delete_operation(sender, receiver);
64+
res->setBody("Success");
65+
} else {
66+
res->setBody("No Authorization");
10767
}
108-
auto res = HttpResponse::newHttpResponse();
109-
res->setBody("Success");
11068
callback(res);
11169
}
112-
void request_processing(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
113-
{
114-
std::string authHeader = req->getHeader("Authorization");
115-
if (authHeader.substr(0, 7) == "Bearer ")
116-
{
117-
std::string bearerToken = authHeader.substr(7);
118-
// 在此处使用Bearer Token进行身份验证
119-
try
120-
{
121-
std::string receiver = jwtDecrypt(bearerToken);
122-
std::string sender = req->getParameter("username");
123-
std::string attitude = req->getParameter("info");
124-
sql_process_request(sender, receiver, attitude);
125-
}
126-
catch (const std::exception &e)
127-
{
128-
std::cerr << e.what() << '\n';
129-
std::cout << "Wrong token" << std::endl;
130-
}
131-
}
70+
// handle new friend request
71+
void request_processing(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) {
13272
auto res = HttpResponse::newHttpResponse();
133-
res->setBody("Success");
73+
res->addHeader("Access-Control-Allow-Origin", "*");
74+
if (jwtVerify(req)) {
75+
std::string receiver = jwtDecrypt(req->getHeader("Authorization").substr(7));
76+
std::string sender = req->getParameter("username");
77+
std::string attitude = req->getParameter("info");
78+
sql_process_request(sender, receiver, attitude);
79+
res->setBody("Success");
80+
} else {
81+
res->setBody("No Authorization");
82+
}
83+
callback(res);
13484
}
135-
void info(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
136-
{
85+
// get chat info
86+
void info(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) {
13787
auto body = req->getBody();
13888
Json::Value req_json, res_json;
13989
Json::Reader reader;
14090
std::string bodyStr(body);
141-
if (!reader.parse(bodyStr, req_json))
142-
{
91+
if (!reader.parse(bodyStr, req_json)) {
14392
callback(HttpResponse::newHttpResponse());
14493
return;
14594
}
14695
std::string me, who_send_me;
14796
Json::FastWriter writer;
148-
std::string authHeader = req->getHeader("Authorization");
149-
if (authHeader.substr(0, 7) == "Bearer ")
150-
{
151-
std::string bearerToken = authHeader.substr(7);
152-
// 在此处使用Bearer Token进行身份验证
153-
try
154-
{
155-
me = jwtDecrypt(bearerToken);
156-
}
157-
catch (const std::exception &e)
158-
{
159-
std::cerr << e.what() << '\n';
160-
std::cout << "Wrong token" << std::endl;
161-
}
162-
}
163-
else
164-
{
165-
// 连接没有Authorization头部Bearer Token
166-
std::cout << "No Authorization" << std::endl;
167-
}
168-
16997
auto res = HttpResponse::newHttpResponse();
17098
res->addHeader("Access-Control-Allow-Origin", "*");
171-
if (req_json["person"].asString() == "")
172-
{
173-
res->setBody(writer.write(get_chat_info(me, "")));
174-
callback(res);
175-
}
176-
else
177-
{
178-
who_send_me = req_json["person"].asString();
179-
res->setBody(writer.write(get_chat_info(me, who_send_me)));
180-
callback(res);
99+
if (jwtVerify(req)) {
100+
me = jwtDecrypt(req->getHeader("Authorization").substr(7));
101+
if (req_json["person"].asString() == "") {
102+
res->setBody(writer.write(get_chat_info(me, "")));
103+
} else {
104+
who_send_me = req_json["person"].asString();
105+
res->setBody(writer.write(get_chat_info(me, who_send_me)));
106+
}
107+
} else {
108+
res->setBody("No Authorization");
181109
}
110+
callback(res);
182111
}

0 commit comments

Comments
 (0)