-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocols_server.cpp
More file actions
317 lines (267 loc) · 11.3 KB
/
Copy pathprotocols_server.cpp
File metadata and controls
317 lines (267 loc) · 11.3 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include <rpc/rpc.h>
#include <stdio.h>
#include "tema.h"
#include "token.h"
#include "utils.hpp"
#include <iostream>
/**
* @brief
*
* Function to tokenize a string by a given delimiter
*
*
* @param str - the string to be tokenized
* @param delim - the delimiter(',' is the default)
* @return a vector of tokens
*/
vector<string> tokenize(string s, string del = ",")
{
int start, end = -1*del.size();
vector<string> tokens;
do {
start = end + del.size();
end = s.find(del, start);
tokens.push_back(s.substr(start, end - start));
} while (end != -1);
return tokens;
}
/**
* @brief
*
* Function to request authorization from the server. It checks if the user is
* registered in the database. If the user is registered, it calls the function
* given in the skel to generate the request token. Otherwise it return an error
* message. Finally, it prints the required message(begin <userId> authorization).
*
*
* @param request - the request structure which contains the user id
* @return The function returns the request token or a message if the user is
* not registered in the response structure.
*/
struct request_response * request_authorization_1_svc(request_info *request, struct svc_req *){
struct request_response *response = (struct request_response *) malloc(sizeof(struct request_response));
string user = request->userid;
response->token_access = strdup("");
response->token_refresh = strdup("");
printf("BEGIN %s AUTHZ\n", request->userid);
fflush(stdout);
if (userIDs.find(string(request->userid)) == userIDs.end()) {
response->token_req = strdup("USER_NOT_FOUND");
return response;
}
response->token_req = (char*)generate_access_token(request->userid);
return response;
}
/**
* @brief
*
* Function to get the permissions from the user. It symbolically "signs" the
* request token. It increments the index in the approval vector to simulate the
* read of users input. It prints the required message(request token = <token>).
*
*
* @param request - the request structure which contains the request token
* @return The function returns the request token in the response structure.
*/
struct request_response * approve_request_token_1_svc(request_info *request, struct svc_req *) {
struct request_response *response = (struct request_response *) malloc(sizeof(struct request_response));
response->token_access = strdup("");
response->token_refresh = strdup("");
response->token_req = strdup(request->token_req);
currentApprovals++;
printf(" RequestToken = %s\n", request->token_req);
fflush(stdout);
return response;
}
/**
* @brief
*
* Function to get the access token.
* It starts by updating the permissions for this user by inserting(or updating)
* the informations about the user in the database(tokens, autorefresh,
* permissions, etc).
* It checks if the user gave any permission. If it didn't(known by checking for
* any '*' entry in the approvals line, meaning there is no permission for any
* resource), the request is denied, the function return an error message.
* If the user gave permissions, the function generates the access token and, if
* required, the refresh token. It stores them in the database and in the response
* structure and prints them.
*
*
* @param request - the request structure, itcontains the request token and user id
* @return The function returns the acces, refresh(if any) tokens and the TTl
* in the response structure
*/
struct request_response * request_access_token_1_svc(request_info *request, struct svc_req *) {
struct request_response *response = (struct request_response *) malloc(sizeof(struct request_response));
map<string, string> resources;
response->token_req = strdup(ttl.c_str());
if (users.find(request->userid) != users.end()) {
vector<string> tokens = tokenize(approvals[currentApprovals]);
map<string, string> resources;
for (int i = 0; i < tokens.size(); i += 2) {
resources[tokens[i]] = tokens[i + 1];
}
users[request->userid].resources = resources;
users[request->userid].autorefresh = request->autorefresh;
users[request->userid].ttl = atoi(ttl.c_str());
} else {
user_info info;
vector<string> tokens = tokenize(approvals[currentApprovals]);
map<string, string> resources;
info.token_access = "";
info.token_refresh = "";
info.autorefresh = request->autorefresh;
for (int i = 0; i < tokens.size(); i += 2) {
resources[tokens[i]] = tokens[i + 1];
}
info.resources = resources;
info.ttl = atoi(ttl.c_str());
users[request->userid] = info;
}
resources = users[request->userid].resources;
if (resources.find("*") != resources.end()) {
response->token_access = strdup("REQUEST_DENIED");
response->token_refresh = strdup("");
return response;
} else {
users[request->userid].token_access = (char*)generate_access_token(request->token_req);
response->token_access = strdup(users[request->userid].token_access.c_str());
if (users[request->userid].autorefresh) {
users[request->userid].token_refresh = (char*)generate_access_token(response->token_access);
response->token_refresh = strdup(users[request->userid].token_refresh.c_str());
} else {
response->token_refresh = strdup("");
}
}
printf(" AccessToken = %s\n", response->token_access);
if (users[request->userid].autorefresh) {
printf(" RefreshToken = %s\n", response->token_refresh);
}
fflush(stdout);
return response;
}
/**
* @brief
*
* Function to validate operation. First, it transforms the operation into an
* easier to use format(one letter, as stored in the db). Then, it searches for
* user using the access token. If it doesn't find the user, it returns an error
* and prints a message.
* If the user is found, it does the verifications specified in the assignment:
* 1. Check if the token is still valid
* 2. Checks if the resource exists
* 3. Check if the user has the required permission(if it doesn't have the
* required permission or no permission on that resource)
* If it passes all the verifications, it returns a message and prints a message.
* Otherwise, it returns an error and prints a message. For any case but the user
* not being found, the TTl is updated.
*
*
* @param validate - the validation structure, it contains the access token, the
* operation that needs to be checked and the resource on which
* the operation is performed
* @return The function returns the acces, refresh(if any) tokens and the TTl
* in the response structure
*/
struct validate_response * validate_delegated_action_1_svc(validate_info *validate, struct svc_req *) {
struct validate_response *response = (struct validate_response *) malloc(sizeof(struct validate_response));
char operation;
int foundResource = 0;
response->ttl = 0;
response->token_access = strdup("");
response->token_refresh = strdup("");
if (!strcmp(validate->op,"READ")) {
operation = 'R';
} else if (!strcmp(validate->op,"INSERT")) {
operation = 'I';
} else if (!strcmp(validate->op,"DELETE")) {
operation = 'D';
} else if (!strcmp(validate->op,"MODIFY")) {
operation = 'M';
} else if (!strcmp(validate->op,"EXECUTE")) {
operation = 'X';
} else {
operation = ' ';
}
for (auto it = users.begin(); it != users.end(); it++) {
if (it->second.token_access == validate->token_access) {
if (it->second.ttl < 1) {
printf("DENY (%s,%s,%s,%d)\n", validate->op, validate->resource, "", 0);
fflush(stdout);
response->message = strdup("TOKEN_EXPIRED");
return response;
}
for (int i = 0; i < resources.size(); i++) {
if (validate->resource == resources[i]) {
foundResource = 1;
break;
}
}
if (!foundResource) {
it->second.ttl--;
printf("DENY (%s,%s,%s,%d)\n", validate->op, validate->resource, validate->token_access, it->second.ttl);
fflush(stdout);
response->message = strdup("RESOURCE_NOT_FOUND");
return response;
}
if (it->second.resources.find(validate->resource) != it->second.resources.end()) {
const char *permission = (it->second.resources[validate->resource]).c_str();
if (!strchr(permission, operation)) {
it->second.ttl--;
printf("DENY (%s,%s,%s,%d)\n", validate->op, validate->resource, validate->token_access, it->second.ttl);
fflush(stdout);
response->message = strdup("OPERATION_NOT_PERMITTED");
return response;
}
} else {
it->second.ttl--;
printf("DENY (%s,%s,%s,%d)\n", validate->op, validate->resource, validate->token_access, it->second.ttl);
fflush(stdout);
response->message = strdup("OPERATION_NOT_PERMITTED");
return response;
}
it->second.ttl--;
printf("PERMIT (%s,%s,%s,%d)\n", validate->op, validate->resource, validate->token_access, it->second.ttl);
fflush(stdout);
response->message = strdup("PERMISSION_GRANTED");
return response;
}
}
printf("DENY (%s,%s,%s,%d)\n", validate->op, validate->resource, validate->token_access, 0);
fflush(stdout);
response->message = strdup("PERMISSION_DENIED");
return response;
}
/**
* @brief
*
* This function is used to refresh the access token. It searches for the user
* and when it finds it, it generates a new access token and refresh token. It
* also updates the TTl. It stores the new values in the datebase, prints the
* new values and return them in the special structure.
*
*
* @param validate - the validation structure, it contains the refresh token
* @return The updated access and refresh tokens and the TTL
*/
struct validate_response * request_refresh_token_1_svc(validate_info *validate, struct svc_req *) {
struct validate_response *response = (struct validate_response *) malloc(sizeof(struct validate_response));
response->ttl = atoi(ttl.c_str());
response->message = strdup("");
for (auto it = users.begin(); it != users.end(); it++) {
if (!strcmp(it->second.token_refresh.c_str(), validate->token_access)) {
response->token_access = (char*)generate_access_token(validate->token_access);
it->second.token_access = strdup(response->token_access);
response->token_refresh = strdup((char*)generate_access_token(response->token_access));
it->second.token_refresh = strdup(response->token_refresh);
it->second.ttl = response->ttl;
printf("BEGIN %s AUTHZ REFRESH\n", it->first.c_str());
printf(" AccessToken = %s\n", response->token_access);
printf(" RefreshToken = %s\n", response->token_refresh);
fflush(stdout);
return response;
}
}
return NULL;
}