Cleanup POST code, send login cookie
FossilOrigin-Name: 4aaf4df947f9798719bdafca69e59d5375402cd552e5da2308120c084e3a42a6
This commit is contained in:
parent
b6fcd4b05d
commit
f75a31fc38
4 changed files with 103 additions and 55 deletions
65
src/login.c
65
src/login.c
|
@ -17,7 +17,9 @@
|
|||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#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);
|
||||
}
|
||||
|
|
|
@ -18,7 +18,6 @@
|
|||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#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,
|
||||
|
|
40
src/query.c
40
src/query.c
|
@ -16,6 +16,10 @@
|
|||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue