-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpc_client.cpp
More file actions
182 lines (163 loc) · 5.3 KB
/
Copy pathrpc_client.cpp
File metadata and controls
182 lines (163 loc) · 5.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
#include "rpc.h"
#include "utils.hpp"
#include <iostream>
#include <rpc/rpc.h>
#include <fstream>
#include <vector>
#include <unordered_map>
using namespace std;
// structure to store user information
typedef struct user_data
{
unordered_map<string, string> resource_permissions;
bool autorefresh;
string access_token;
string refresh_token;
int ttl;
} user_data;
CLIENT *handle;
unordered_map<string, user_data> users;
// An easer way to convert enum to string
unordered_map<status_t, string> enum_to_string = {
{status_t::OK, "OK"},
{status_t::REQUEST_DENIED, "REQUEST_DENIED"},
{status_t::USER_NOT_FOUND, "USER_NOT_FOUND"},
{status_t::PERMISSION_DENIED, "PERMISSION_DENIED"},
{status_t::TOKEN_EXPIRED, "TOKEN_EXPIRED"},
{status_t::RESOURCE_NOT_FOUND, "RESOURCE_NOT_FOUND"},
{status_t::OPERATION_NOT_PERMITTED, "OPERATION_NOT_PERMITTED"},
{status_t::PERMISSION_GRANTED, "PERMISSION_GRANTED"}};
/// @brief - Function that gives an access token to the user based on the OAuth protocol
/// It first checks if the user exists and if they do, it generates a request token that is
/// used to get the permissions for the user and afterwards it generates an access token and
/// a refresh token if needed with a certain time to live.
/// The functions also outputs the tokens to the screen.
/// @param user_id - the id of the user in userIDs.db
/// @param autorefresh - a flag that tells if the user wants to autorefresh the token
void request_command(string user_id, bool autorefresh)
{
user_data user;
request_auth_response_t *response;
char *user_id_c = (char *)user_id.c_str();
response = request_auth_0xcafebabe(&user_id_c, handle);
if (response->status != status_t::OK)
{
cout << enum_to_string[response->status] << endl;
return;
}
approve_request_token_response_t *response1;
response1 = approve_request_token_0xcafebabe(&response->request_token, handle);
string request_token = response1->request_token;
request_access_token_request_t request;
request.user_id = user_id_c;
request.request_token = (char *)request_token.c_str();
request.autorefresh = autorefresh;
request_access_token_response_t *response2;
response2 = request_access_token_0xcafebabe(&request, handle);
if (response2->status != status_t::OK)
{
cout << enum_to_string[response2->status] << endl;
return;
}
user.access_token = response2->access_token;
user.ttl = response2->ttl;
user.autorefresh = autorefresh;
user.refresh_token = response2->refresh_token;
users[user_id] = user;
if (autorefresh)
{
cout << request_token << " -> "
<< users[user_id].access_token << ","
<< users[user_id].refresh_token << endl;
}
else
cout << request_token << " -> "
<< users[user_id].access_token << endl;
}
/// @brief - Function that validates an operation done on resource by a user
/// It checks if the user is registered and has a non-expired token and sends a request to the server
/// to validate the operation. If the status code of the response is anything other then a PERMISSION_DENIED,
/// the ttl of the token is decresed. If the token has expired and the user has autorefresh enabled, a new
/// acces token is generated. The functions outputs the status of the validation to the screen.
/// @param user_id - the id of the user in userIDs.db
/// @param operation - the operation that the user wants to do on the resource
/// @param resource - the resource from resources.db
void validate_command(string user_id, string operation, string resource)
{
user_data user;
validate_request_t request;
status_t *response;
request.access_token = "";
if (users.find(user_id) != users.end())
{
user = users[user_id];
if (user.autorefresh && user.ttl <= 0)
{
validate_response_t *response2;
char *refresh_token = (char *)user.refresh_token.c_str();
response2 = request_refresh_token_0xcafebabe(&refresh_token, handle);
user.refresh_token = response2->refresh_token;
user.access_token = response2->access_token;
user.ttl = response2->ttl;
users[user_id] = user;
}
request.access_token = (char *)user.access_token.c_str();
}
request.operation = (char *)operation.c_str();
request.resource = (char *)resource.c_str();
response = validate_delegated_action_0xcafebabe(&request, handle);
cout << enum_to_string[*response] << endl;
if (*response != status_t::PERMISSION_DENIED)
{
if (users.find(user_id) != users.end())
{
users[user_id].ttl--;
}
}
}
/// @brief - Strategy-type function that executes the command based on the command type
/// @param tokens - the tokens from the input line
/// @param command - the command type
void execute_command(vector<string> tokens, string command)
{
if (command == "REQUEST")
{
bool autorefresh = stoi(tokens[2]) == 1;
request_command(tokens[0], autorefresh);
}
else
validate_command(tokens[0], tokens[1], tokens[2]);
}
int main(int argc, char *argv[])
{
if (argc != 3)
{
cerr << "Usage: " << argv[0] << " <server_address> <path_to_client>" << endl;
exit(1);
}
handle = clnt_create(
argv[1],
RPC_OAUTH,
RPC_OAUTH_1,
"tcp");
if (handle == NULL)
{
cerr << "Error connecting to server" << endl;
exit(1);
}
ifstream fin(argv[2]);
if (!fin.is_open())
{
cerr << "Error opening file" << endl;
exit(1);
}
string line;
while (getline(fin, line))
{
auto words = split(line, ',');
execute_command(words, words[1]);
}
fin.close();
clnt_destroy(handle);
return 0;
}