diff --git a/src/login.c b/src/login.c index 500bb58..accb2d7 100644 --- a/src/login.c +++ b/src/login.c @@ -17,7 +17,9 @@ */ #include +#include #include +#include "query.h" #include "base_page.h" #include "login.h" #include "../config.h" @@ -25,32 +27,56 @@ // Files #include "../static/login.chtml" +struct login_info +{ + char* username; + char* password; +}; + +static void authenticate(struct http_query_info* info, void* _args) +{ + struct login_info* login = _args; + + if (strcmp(info->key, "username") == 0) + login->username = info->val; + else if (strcmp(info->key, "password") == 0) + login->password = info->val; +} + void content_login(mastodont_t* api, char** data, size_t data_size) { + char* post_query; struct mstdnt_storage storage, oauth_store; struct mstdnt_app app; struct mstdnt_oauth_token token; + struct login_info info = { 0 }; - // Getting the client id/secret - struct mstdnt_app_register_args args_app = { - .client_name = "RatFE", - .redirect_uris = "http://localhost/", - .scopes = "read+write", - .website = NULL - }; + post_query = try_handle_post(authenticate, &info); + if (post_query) + { + // Getting the client id/secret + struct mstdnt_app_register_args args_app = { + .client_name = "RatFE", + .redirect_uris = "http://localhost/", + .scopes = "read+write", + .website = NULL + }; - struct mstdnt_oauth_token_args args_token = { - .grant_type = "password", - .client_id = app.client_id, - .client_secret = app.client_secret, - .username = "testuser", - .password = "password", - }; - - mastodont_register_app(api, &args_app, &storage, &app); + mastodont_register_app(api, &args_app, &storage, &app); - mastodont_obtain_oauth_token(api, &args_token, &oauth_store, - &token); + struct mstdnt_oauth_token_args args_token = { + .grant_type = "password", + .client_id = app.client_id, + .client_secret = app.client_secret, + .username = info.username, + .password = info.password + }; + + mastodont_obtain_oauth_token(api, &args_token, &oauth_store, + &token); + // TODO checking, also ^ returns non-zero + printf("Set-Cookie: access_token=%s; HttpOnly; SameSite=Strict;", token.access_token); + } struct base_page b = { .locale = L10N_EN_US, @@ -60,4 +86,7 @@ void content_login(mastodont_t* api, char** data, size_t data_size) // Output render_base_page(&b); + + // Cleanup + if (post_query) free(post_query); } diff --git a/src/page_config.c b/src/page_config.c index 5f9425f..3a74f6f 100644 --- a/src/page_config.c +++ b/src/page_config.c @@ -18,7 +18,6 @@ #include #include -#include #include #include "base_page.h" #include "../config.h" @@ -31,46 +30,25 @@ #include "../static/index.chtml" #include "../static/config.chtml" +static void config_post(struct http_query_info* info, void* args) +{ + (void)args; + + if (strcmp(info->key, "theme") == 0) + { + g_config.theme = info->val; + printf("Set-Cookie: %s=%s; HttpOnly; SameSite=Strict;", + "theme", info->val); + g_config.changed = 1; + } +} + void content_config(mastodont_t* api, char** data, size_t size) { + char* post_query; (void)api; // No need to use this - char* request_method = getenv("REQUEST_METHOD"); - char* post_query = NULL, * p_query_read; - struct http_query_info info; - // Handle POST - if (request_method && (strcmp("POST", request_method) == 0)) - { - int content_length = atoi(getenv("CONTENT_LENGTH")); - post_query = malloc(content_length + 1); - if (!post_query) - { - puts("Malloc error!"); - return; - } - read(STDIN_FILENO, post_query, content_length); - post_query[content_length] = '\0'; - - // For parse_query to shift through, so we can still free the original - p_query_read = post_query; - - // Parse - while (1) - { - p_query_read = parse_query(p_query_read, &info); - if (!(info.key && info.val)) break; - if (strcmp(info.key, "theme") == 0) - { - g_config.theme = info.val; - printf("Set-Cookie: %s=%s; HttpOnly; SameSite=Strict;", - "theme", info.val); - g_config.changed = 1; - } - - if (!p_query_read) break; - } - - } + post_query = try_handle_post(config_post, NULL); struct base_page b = { .locale = L10N_EN_US, diff --git a/src/query.c b/src/query.c index da30ef8..ec229e5 100644 --- a/src/query.c +++ b/src/query.c @@ -16,6 +16,10 @@ * along with this program. If not, see . */ +#include +#include +#include +#include #include "query.h" char* parse_query(char* begin, struct http_query_info* info) @@ -40,3 +44,39 @@ char* parse_query(char* begin, struct http_query_info* info) return end ? NULL : begin+1; } + +char* try_handle_post(void (*call)(struct http_query_info*, void*), void* arg) +{ + char* request_method = getenv("REQUEST_METHOD"); + char* post_query = NULL, * p_query_read; + struct http_query_info info; + + // Handle POST + if (request_method && (strcmp("POST", request_method) == 0)) + { + int content_length = atoi(getenv("CONTENT_LENGTH")); + post_query = malloc(content_length + 1); + if (!post_query) + { + puts("Malloc error!"); + return NULL; + } + read(STDIN_FILENO, post_query, content_length); + post_query[content_length] = '\0'; + + // For parse_query to shift through, so we can still free the original + p_query_read = post_query; + + // Parse + while (1) + { + p_query_read = parse_query(p_query_read, &info); + if (!(info.key && info.val)) break; + call(&info, arg); + + if (!p_query_read) break; + } + } + + return post_query; +} diff --git a/src/query.h b/src/query.h index 2fe5881..68ca8df 100644 --- a/src/query.h +++ b/src/query.h @@ -29,5 +29,6 @@ struct http_query_info /* A stupidly quick query parser */ char* parse_query(char* begin, struct http_query_info* info); +char* try_handle_post(void (*call)(struct http_query_info*, void*), void* arg); #endif // QUERY_H