-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.cpp
More file actions
186 lines (159 loc) · 6.63 KB
/
client.cpp
File metadata and controls
186 lines (159 loc) · 6.63 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <bits/stdc++.h>
#include <nlohmann/json.hpp>
#include <stdio.h> /* printf, sprintf */
#include <stdlib.h> /* exit, atoi, malloc, free */
#include <unistd.h> /* read, write, close */
#include <string.h> /* memcpy, memset */
#include <sys/socket.h> /* socket, connect */
#include <netinet/in.h> /* struct sockaddr_in, struct sockaddr */
#include <netdb.h> /* struct hostent, gethostbyname */
#include <arpa/inet.h>
#include "helpers.h"
#include "requests.h"
#include "functions.h"
#define IP "3.8.116.10"
#define PORT 8080
using namespace std;
using json = nlohmann::json;
enum CommandSet {
regist, login, enter_library, get_books, get_book, add_book,
delete_book, logout, ext
};
// Initialize the commands map with all the required commands
void initialize_map(unordered_map <string, CommandSet> &commands) {
commands["register"] = CommandSet::regist;
commands["login"] = CommandSet::login;
commands["enter_library"] = CommandSet::enter_library;
commands["get_books"] = CommandSet::get_books;
commands["get_book"] = CommandSet::get_book;
commands["add_book"] = CommandSet::add_book;
commands["delete_book"] = CommandSet::delete_book;
commands["logout"] = CommandSet::logout;
commands["exit"] = CommandSet::ext;
}
int main(int argc, char *argv[]) {
char *message;
char *response;
int sockfd;
int recv_commands = 1;
string session_cookie;
string library_jwt;
// To be able to use switch instead of if
unordered_map<string, CommandSet> valid_commands;
initialize_map(valid_commands);
char* type = "application/json";
while (recv_commands) {
// Connect to the server
sockfd = open_connection(IP, PORT, AF_INET, SOCK_STREAM, 0);
// Read the current command
string current_command;
cin >> current_command;
// If the current command is register
switch(valid_commands[current_command]) {
case CommandSet::regist : {
cout << get_initial_response("/api/v1/tema/auth/register", sockfd, type) << "\n";
break;
}
case CommandSet::login : {
response = get_initial_response("/api/v1/tema/auth/login", sockfd, type);
cout << response << "\n";
session_cookie = cookie(response);
free(response);
break;
}
case CommandSet::enter_library : {
if (session_cookie.size() != 0) {
char *response = get_cookie_response("/api/v1/tema/library/access",
sockfd, session_cookie, 0);
cout << response << "\n";
library_jwt = jwt_token(response);
free(response);
} else {
cout << "No previous login found. Please retry!\n\n";
}
break;
}
case CommandSet::get_books : {
if (library_jwt.size() != 0) {
string auth = "Authorization: Bearer " + library_jwt.substr(10, library_jwt.size() - 12);
char *response = get_cookie_response("/api/v1/tema/library/books", sockfd, auth, 1);
cout << response << "\n";
free(response);
} else {
cout << "No token found. Please retry!\n\n";
}
break;
}
case CommandSet::get_book : {
if (library_jwt.size() != 0) {
string id;
cout << "id=";
cin >> id;
string auth = "Authorization: Bearer " + library_jwt.substr(10, library_jwt.size() - 12);
string string_url = "/api/v1/tema/library/books/" + id;
char* url = (char *) malloc(string_url.size() + 1);
strcpy(url, string_url.c_str());
char *response = get_cookie_response(url, sockfd, auth, 1);
cout << response;
free(response);
free(url);
} else {
cout << "No token found. Please retry!\n\n";
}
break;
}
case CommandSet::add_book : {
if (library_jwt.size() != 0) {
string auth = "Authorization: Bearer " + library_jwt.substr(10, library_jwt.size() - 12);
char *response = get_book_response("/api/v1/tema/library/books", sockfd, type, auth, 1);
cout << response;
free(response);
} else {
cout << "No token found. Please retry!\n\n";
}
break;
}
case CommandSet::delete_book : {
if (library_jwt.size() != 0) {
string id;
cout << "id=";
cin >> id;
string auth = "Authorization: Bearer " + library_jwt.substr(10, library_jwt.size() - 12);
string string_url = "/api/v1/tema/library/books/" + id;
char* url = (char *) malloc(string_url.size() + 1);
strcpy(url, string_url.c_str());
char* response = get_delete_response(url, sockfd, auth, 1);
cout << response;
free(url);
free(response);
} else {
cout << "No token found. Please retry!\n\n";
}
break;
}
case CommandSet::logout : {
if (session_cookie.size() != 0) {
char* response = get_logout_response("/api/v1/auth/logout", sockfd, type, session_cookie);
cout << response;
// Remove token and cookies
library_jwt = "";
session_cookie = "";
cout << "The current session has ended.\n\n";
} else {
cout << "No login found. Please login first!\n\n";
}
break;
}
case CommandSet::ext : {
recv_commands = 0;
break;
}
default : {
cout << "Invalid command. Please retry!\n\n";
break;
}
}
close_connection(sockfd);
}
return 0;
}