-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuthServer.cpp
333 lines (287 loc) · 10.1 KB
/
AuthServer.cpp
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
Authorization Server code for CMPT 276, Spring 2016.
*/
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include <cpprest/http_listener.h>
#include <cpprest/json.h>
#include <was/common.h>
#include <was/table.h>
#include "TableCache.h"
#include "make_unique.h"
#include "ClientUtils.h"
#include "azure_keys.h"
using azure::storage::storage_exception;
using azure::storage::cloud_table;
using azure::storage::cloud_table_client;
using azure::storage::edm_type;
using azure::storage::entity_property;
using azure::storage::table_entity;
using azure::storage::table_operation;
using azure::storage::table_request_options;
using azure::storage::table_result;
using azure::storage::table_shared_access_policy;
using std::cin;
using std::cout;
using std::endl;
using std::getline;
using std::make_pair;
using std::pair;
using std::string;
using std::unordered_map;
using std::vector;
using web::http::http_headers;
using web::http::http_request;
using web::http::methods;
using web::http::status_code;
using web::http::status_codes;
using web::http::uri;
using web::json::value;
using web::http::experimental::listener::http_listener;
using prop_str_vals_t = vector<pair<string,string>>;
constexpr const char* def_url = "http://localhost:34570";
const string auth_table_name {"AuthTable"};
const string auth_table_userid_partition {"Userid"};
const string auth_table_password_prop {"Password"};
const string auth_table_partition_prop {"DataPartition"};
const string auth_table_row_prop {"DataRow"};
const string data_table_name {"DataTable"};
const string get_read_token_op {"GetReadToken"};
const string get_update_token_op {"GetUpdateToken"};
const string get_update_data_op {"GetUpdateData"};
/*
Cache of opened tables
*/
TableCache table_cache {};
/*
Convert properties represented in Azure Storage type
to prop_str_vals_t type.
*/
prop_str_vals_t get_string_properties (const table_entity::properties_type& properties) {
prop_str_vals_t values {};
for (const auto v : properties) {
if (v.second.property_type() == edm_type::string) {
values.push_back(make_pair(v.first,v.second.string_value()));
}
else {
// Force the value as string in any case
values.push_back(make_pair(v.first, v.second.str()));
}
}
return values;
}
/*
Given an HTTP message with a JSON body, return the JSON
body as an unordered map of strings to strings.
Note that all types of JSON values are returned as strings.
Use C++ conversion utilities to convert to numbers or dates
as necessary.
*/
unordered_map<string,string> get_json_body(http_request message) {
unordered_map<string,string> results {};
const http_headers& headers {message.headers()};
auto content_type (headers.find("Content-Type"));
if (content_type == headers.end() ||
content_type->second != "application/json")
return results;
value json{};
message.extract_json(true)
.then([&json](value v) -> bool
{
json = v;
return true;
})
.wait();
if (json.is_object()) {
for (const auto& v : json.as_object()) {
if (v.second.is_string()) {
results[v.first] = v.second.as_string();
}
else {
results[v.first] = v.second.serialize();
}
}
}
return results;
}
/*
Return a token for 24 hours of access to the specified table,
for the single entity defind by the partition and row.
permissions: A bitwise OR ('|') of table_shared_access_poligy::permission
constants.
For read-only:
table_shared_access_policy::permissions::read
For read and update:
table_shared_access_policy::permissions::read |
table_shared_access_policy::permissions::update
*/
pair<status_code,string> do_get_token (const cloud_table& data_table,
const string& partition,
const string& row,
uint8_t permissions) {
cout << "Retrieving token from /" + partition + "/" + row << endl;
utility::datetime exptime {utility::datetime::utc_now() + utility::datetime::from_days(1)};
try {
string limited_access_token {
data_table.get_shared_access_signature(table_shared_access_policy {
exptime,
permissions},
string(), // Unnamed policy
// Start of range (inclusive)
partition,
row,
// End of range (inclusive)
partition,
row)
// Following token allows read access to entire table
//table.get_shared_access_signature(table_shared_access_policy {exptime, permissions})
};
cout << "Token " << limited_access_token << endl;
return make_pair(status_codes::OK, limited_access_token);
}
catch (const storage_exception& e) {
cout << "Azure Table Storage error: " << e.what() << endl;
cout << e.result().extended_error().message() << endl;
return make_pair(status_codes::InternalError, string{});
}
}
/*
Top-level routine for processing all HTTP GET requests.
*/
void handle_get(http_request message) {
string path {uri::decode(message.relative_uri().path())};
cout << endl << "**** AuthServer GET " << path << endl;
auto paths = uri::split_path(path);
// Need at least an operation and userid
if (paths.size() < 2) {
message.reply(status_codes::BadRequest);
return;
}
//CONTAINS THE PASSWORD SO WE NEED ONE OF THESE CALLS
unordered_map<string,string> json_body {get_json_body(message)};
table_cache.init(storage_connection_string);
cloud_table table {table_cache.lookup_table(auth_table_name)};
if ( ! table.exists()) {
message.reply(status_codes::NotFound);
return;
}
table_operation retrieve_operation {table_operation::retrieve_entity(auth_table_userid_partition, paths[1])};
table_result retrieve_result {table.execute(retrieve_operation)};
cout << "HTTP code: " << retrieve_result.http_status_code() << endl;
if (retrieve_result.http_status_code() == status_codes::NotFound) { //COULD BE A DIFFERENT STATUS CODE, something about having security issues if an id isnt on the list/if the password is wrong
message.reply(status_codes::NotFound);
return;
}
table_entity entity {retrieve_result.entity()};
table_entity::properties_type properties {entity.properties()};
prop_str_vals_t values (get_string_properties(properties));
if (json_body[auth_table_password_prop] != std::get<1>(values[0])) {
message.reply(status_codes::NotFound); //If the passwords dont match (Dont know what the status code needs to be)
return;
}
cloud_table table2 {table_cache.lookup_table(data_table_name)};
if ( ! table2.exists()) {
message.reply(status_codes::NotFound);
return;
}
//Checks to see if pass, DataPartition and DataRow exists
if (values.size() < 3) {
message.reply(status_codes::BadRequest);
return;
}
string partition {std::get<1>(values[2])};
string row {std::get<1>(values[1])};
uint8_t permission {};
if (get_update_token_op == paths[0] || get_update_data_op == paths[0]) {
permission = table_shared_access_policy::permissions::read |
table_shared_access_policy::permissions::update;
}
else if (get_read_token_op == paths[0]) {
permission = table_shared_access_policy::permissions::read;
}
else {
message.reply(status_codes::NotFound);
return;
}
pair<status_code, string> token {
do_get_token (table2,
partition,
row,
permission
)
};
if (paths[0] == get_update_data_op) {
message.reply(status_codes::OK, value::object(vector<pair<string,value>> {
make_pair ("token", value::string (token.second)),
make_pair ("DataPartition", value::string (partition)),
make_pair ("DataRow", value::string (row))
}));
return;
}
else {
value v {value::string(token.second)};
cout << "HTTP code: " << token.first << endl;
if (token.first == status_codes::OK) {
message.reply(status_codes::OK, v);
return;
}
else {
message.reply(status_codes::NotFound);
return;
}
}
//message.reply(status_codes::NotImplemented);
}
/*
Top-level routine for processing all HTTP POST requests.
*/
void handle_post(http_request message) {
string path {uri::decode(message.relative_uri().path())};
cout << endl << "**** POST " << path << endl;
}
/*
Top-level routine for processing all HTTP PUT requests.
*/
void handle_put(http_request message) {
string path {uri::decode(message.relative_uri().path())};
cout << endl << "**** PUT " << path << endl;
}
/*
Top-level routine for processing all HTTP DELETE requests.
*/
void handle_delete(http_request message) {
string path {uri::decode(message.relative_uri().path())};
cout << endl << "**** DELETE " << path << endl;
}
/*
Main authentication server routine
Install handlers for the HTTP requests and open the listener,
which processes each request asynchronously.
Note that, unlike BasicServer, AuthServer only
installs the listeners for GET. Any other HTTP
method will produce a Method Not Allowed (405)
response.
If you want to support other methods, uncomment
the call below that hooks in a the appropriate
listener.
Wait for a carriage return, then shut the server down.
*/
int main (int argc, char const * argv[]) {
cout << "AuthServer: Parsing connection string" << endl;
table_cache.init (storage_connection_string);
cout << "AuthServer: Opening listener" << endl;
http_listener listener {def_url};
listener.support(methods::GET, &handle_get);
//listener.support(methods::POST, &handle_post);
//listener.support(methods::PUT, &handle_put);
//listener.support(methods::DEL, &handle_delete);
listener.open().wait(); // Wait for listener to complete starting
cout << "Enter carriage return to stop AuthServer." << endl;
string line;
getline(std::cin, line);
// Shut it down
listener.close().wait();
cout << "AuthServer closed" << endl;
}