From f81c1fba968ed8318abbf6d64886fa7b043b54ce Mon Sep 17 00:00:00 2001 From: "me@ow.nekobit.net" Date: Sun, 27 Feb 2022 05:39:34 +0000 Subject: [PATCH] Correct login FossilOrigin-Name: b604906872b289329c2b57028b5d64b4463e65d5dee1e26283e9663c0e13ec53 --- Makefile | 2 ++ src/error.c | 32 ++++++++++++++++++++++++++++++++ src/error.h | 25 +++++++++++++++++++++++++ src/index.c | 4 ++-- src/lists.c | 2 +- src/login.c | 36 ++++++++++++++++++++++++++++-------- static/error.html | 1 + 7 files changed, 91 insertions(+), 11 deletions(-) create mode 100644 src/error.c create mode 100644 src/error.h create mode 100644 static/error.html diff --git a/Makefile b/Makefile index 859e3d9..6bc02d4 100644 --- a/Makefile +++ b/Makefile @@ -42,6 +42,8 @@ $(PAGES_DIR)/list.chtml: $(PAGES_DIR)/list.html ./filec $< data_list_html > $@ $(PAGES_DIR)/lists.chtml: $(PAGES_DIR)/lists.html ./filec $< data_lists_html > $@ +$(PAGES_DIR)/error.chtml: $(PAGES_DIR)/error.html + ./filec $< data_error_html > $@ $(MASTODONT_DIR): git clone $(MASTODONT_URL) || true diff --git a/src/error.c b/src/error.c new file mode 100644 index 0000000..723bd6e --- /dev/null +++ b/src/error.c @@ -0,0 +1,32 @@ +/* + * RatFE - Lightweight frontend for Pleroma + * Copyright (C) 2022 Nekobit + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include "error.h" +#include "easprintf.h" + +// Pages +#include "../static/error.chtml" + +char* construct_error(char* error, size_t* size) +{ + char* error_html; + size_t s = easprintf(&error_html, data_error_html, + error); + if (size) *size = s; + return error_html; +} diff --git a/src/error.h b/src/error.h new file mode 100644 index 0000000..96c8aa1 --- /dev/null +++ b/src/error.h @@ -0,0 +1,25 @@ +/* + * RatFE - Lightweight frontend for Pleroma + * Copyright (C) 2022 Nekobit + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#ifndef ERROR_H +#define ERROR_H +#include + +char* construct_error(char* message, size_t* size); + +#endif // ERROR_H diff --git a/src/index.c b/src/index.c index a21f8ba..e24035f 100644 --- a/src/index.c +++ b/src/index.c @@ -34,10 +34,10 @@ void content_index(mastodont_t* api) int cleanup = 0; size_t status_count, statuses_html_count; struct mstdnt_status* statuses; - struct mstdnt_storage storage; + struct mstdnt_storage storage = { 0 }; char* status_format, *post_box; char* output = NULL; - + try_post_status(api); if (mastodont_timeline_public(api, NULL, &storage, &statuses, &status_count)) diff --git a/src/lists.c b/src/lists.c index e487ee3..f47d665 100644 --- a/src/lists.c +++ b/src/lists.c @@ -66,7 +66,7 @@ void content_lists(mastodont_t* api, char** data, size_t size) int cleanup = 0; struct mstdnt_list* lists; size_t size_list; - struct mstdnt_storage storage; + struct mstdnt_storage storage = { 0 }; char* lists_format; char* lists_page = NULL; diff --git a/src/login.c b/src/login.c index 073054d..4fa4cab 100644 --- a/src/login.c +++ b/src/login.c @@ -22,6 +22,8 @@ #include "query.h" #include "base_page.h" #include "login.h" +#include "error.h" +#include "easprintf.h" #include "../config.h" // Files @@ -29,9 +31,11 @@ void content_login(mastodont_t* api, char** data, size_t data_size) { - struct mstdnt_storage storage, oauth_store; + struct mstdnt_storage storage = { 0 }, oauth_store = { 0 }; struct mstdnt_app app; struct mstdnt_oauth_token token; + char* error = NULL; + char* page; if (post.username && post.password) { @@ -49,23 +53,39 @@ void content_login(mastodont_t* api, char** data, size_t data_size) .grant_type = "password", .client_id = app.client_id, .client_secret = app.client_secret, + .redirect_uri = NULL, + .scope = NULL, + .code = NULL, .username = post.username, .password = post.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;\r\n", token.access_token); - printf("Set-Cookie: logged_in=t; SameSite=Strict\r\n"); + + if (mastodont_obtain_oauth_token(api, &args_token, &oauth_store, + &token) == 1) + { + error = construct_error(oauth_store.error, NULL); + } + else { + // TODO checking, also ^ returns non-zero + printf("Set-Cookie: access_token=%s; HttpOnly; SameSite=Strict;\r\n", token.access_token); + printf("Set-Cookie: logged_in=t; SameSite=Strict\r\n"); + printf("Location: %s/\r\n", config_url_prefix); + } } + easprintf(&page, "%s%s", error ? error : "", data_login_html); struct base_page b = { .locale = L10N_EN_US, - .content = data_login_html, + .content = page, .sidebar_right = NULL }; // Output render_base_page(&b); + + // Cleanup + mastodont_storage_cleanup(&storage); + mastodont_storage_cleanup(&oauth_store); + if (error) free(error); + if (page) free(page); } diff --git a/static/error.html b/static/error.html new file mode 100644 index 0000000..eb5f005 --- /dev/null +++ b/static/error.html @@ -0,0 +1 @@ +%s