This commit is contained in:
Micah Moore 2022-12-11 17:12:25 -05:00
parent aeb235246d
commit 00151f2e18
2 changed files with 17 additions and 15 deletions

View File

@ -475,6 +475,8 @@ post_handle_login(struct http_transaction *ta)
if (!strcmp(username, "user0") && !strcmp(password, "thepassword")) if (!strcmp(username, "user0") && !strcmp(password, "thepassword"))
{ {
int iat = time(NULL);
int exp = iat + token_expiration_time;
// Create cookie. // Create cookie.
jwt_t* cookie; jwt_t* cookie;
int rc = jwt_new(&cookie); int rc = jwt_new(&cookie);
@ -486,13 +488,13 @@ post_handle_login(struct http_transaction *ta)
if (rc) if (rc)
return send_error(ta, HTTP_INTERNAL_ERROR, "jwt_add_grant"); return send_error(ta, HTTP_INTERNAL_ERROR, "jwt_add_grant");
time_t age = time(NULL); //time_t age = time(NULL);
rc = jwt_add_grant_int(cookie, "iat", age); rc = jwt_add_grant_int(cookie, "iat", iat);
if (rc) if (rc)
return send_error(ta, HTTP_INTERNAL_ERROR, "jwt_add_grant"); return send_error(ta, HTTP_INTERNAL_ERROR, "jwt_add_grant");
time_t max_age = age + 3600 * 24; //time_t max_age = age + 3600 * 24;
rc = jwt_add_grant_int(cookie, "exp", max_age); rc = jwt_add_grant_int(cookie, "exp", exp);
if (rc) if (rc)
return send_error(ta, HTTP_INTERNAL_ERROR, "jwt_add_grant"); return send_error(ta, HTTP_INTERNAL_ERROR, "jwt_add_grant");
@ -507,7 +509,7 @@ post_handle_login(struct http_transaction *ta)
return send_error(ta, HTTP_INTERNAL_ERROR, "jwt_encode_str"); return send_error(ta, HTTP_INTERNAL_ERROR, "jwt_encode_str");
// Add Set-Cookie header. // Add Set-Cookie header.
http_add_header(&ta->resp_headers, "Set-Cookie", "auth_token=%s; Path=%s; Max-Age=%d; HttpOnly", token, "/", max_age); http_add_header(&ta->resp_headers, "Set-Cookie", "auth_token=%s; Path=%s; Max-Age=%d; HttpOnly", token, "/", token_expiration_time);
// Get claim (formatted grants). // Get claim (formatted grants).
char *grants = jwt_get_grants_json(cookie, NULL); char *grants = jwt_get_grants_json(cookie, NULL);

View File

@ -35,13 +35,13 @@ int token_expiration_time = 24 * 60 * 60;
// root from which static files are served // root from which static files are served
char * server_root; char * server_root;
static void *thr_func(void *cs) { static void *thr_func(void *c) {
int client_socket = *(int*) cs;
struct http_client client; struct http_client *client = c;
http_setup_client(&client, bufio_create(client_socket));
while (http_handle_transaction(&client)){}; while (http_handle_transaction(client)){};
bufio_close(client.bufio); bufio_close(client->bufio);
return NULL; return NULL;
} }
@ -55,7 +55,7 @@ static void *thr_func(void *cs) {
static void static void
server_loop(char *port_string) server_loop(char *port_string)
{ {
int max_clients = 16; int max_clients = 8;
int num_clients = 0; int num_clients = 0;
pthread_t *threads = malloc(max_clients * sizeof(pthread_t)); pthread_t *threads = malloc(max_clients * sizeof(pthread_t));
@ -67,10 +67,10 @@ server_loop(char *port_string)
if (client_socket == -1){ if (client_socket == -1){
return; return;
} }
//~ struct http_client *client = malloc(sizeof(struct http_client)); struct http_client *client = malloc(sizeof(struct http_client));
//~ http_setup_client(client, bufio_create(client_socket)); http_setup_client(client, bufio_create(client_socket));
int rc = pthread_create(&threads[num_clients++], NULL, thr_func, &client_socket); int rc = pthread_create(&threads[num_clients++], NULL, thr_func, client);
if (rc) { if (rc) {
perror(NULL); perror(NULL);