auth: Auto-login fixes.
Some checks failed
Build Tilde Friends / Build-All (push) Has been cancelled

This commit is contained in:
Cory McWilliams 2025-02-12 19:37:06 -05:00
parent 15df4ac236
commit e491798ff1

View File

@ -2190,30 +2190,32 @@ typedef struct _auto_login_t
static void _httpd_auto_login_work(tf_ssb_t* ssb, void* user_data) static void _httpd_auto_login_work(tf_ssb_t* ssb, void* user_data)
{ {
tf_printf("AUTO LOGIN\n");
auto_login_t* request = user_data; auto_login_t* request = user_data;
request->users = tf_ssb_db_get_property(ssb, "auth", "users"); request->users = tf_ssb_db_get_property(ssb, "auth", "users");
if (request->users && strcmp(request->users, "[]") == 0) if (request->users && strcmp(request->users, "[]") == 0)
{ {
tf_printf("AUTO LOGIN NO USERS\n");
tf_free((void*)request->users); tf_free((void*)request->users);
request->users = NULL; request->users = NULL;
} }
if (!request->users) if (!request->users)
{ {
sqlite3* db = tf_ssb_acquire_db_writer(ssb);
JSMallocFunctions funcs = { 0 }; JSMallocFunctions funcs = { 0 };
tf_get_js_malloc_functions(&funcs); tf_get_js_malloc_functions(&funcs);
JSRuntime* runtime = JS_NewRuntime2(&funcs, NULL); JSRuntime* runtime = JS_NewRuntime2(&funcs, NULL);
JSContext* context = JS_NewContext(runtime); JSContext* context = JS_NewContext(runtime);
tf_ssb_db_register_account(tf_ssb_get_loop(ssb), db, context, "mobile", "mobile"); static const char* k_account_name = "mobile";
sqlite3* db = tf_ssb_acquire_db_writer(ssb);
bool registered = tf_ssb_db_register_account(tf_ssb_get_loop(ssb), db, context, k_account_name, k_account_name);
tf_ssb_release_db_writer(ssb, db); tf_ssb_release_db_writer(ssb, db);
if (registered)
{
_make_administrator_if_first(ssb, context, k_account_name, true);
}
JS_FreeContext(context); JS_FreeContext(context);
JS_FreeRuntime(runtime); JS_FreeRuntime(runtime);
request->users = tf_ssb_db_get_property(ssb, "auth", "users"); request->users = tf_ssb_db_get_property(ssb, "auth", "users");
tf_printf("AUTO LOGIN USERS = %s\n", request->users);
} }
} }
@ -2227,7 +2229,6 @@ static void _httpd_auto_login_after_work(tf_ssb_t* ssb, int status, void* user_d
JSValue json = JS_ParseJSON(context, work->users, strlen(work->users), NULL); JSValue json = JS_ParseJSON(context, work->users, strlen(work->users), NULL);
JSValue user = JS_GetPropertyUint32(context, json, 0); JSValue user = JS_GetPropertyUint32(context, json, 0);
const char* user_string = JS_ToCString(context, user); const char* user_string = JS_ToCString(context, user);
tf_printf("AUTO LOGIN %s\n", user_string);
session_token = _make_session_jwt(context, ssb, user_string); session_token = _make_session_jwt(context, ssb, user_string);
JS_FreeCString(context, user_string); JS_FreeCString(context, user_string);
JS_FreeValue(context, user); JS_FreeValue(context, user);
@ -2236,7 +2237,6 @@ static void _httpd_auto_login_after_work(tf_ssb_t* ssb, int status, void* user_d
if (session_token) if (session_token)
{ {
const char* cookie = _make_set_session_cookie_header(work->request, session_token); const char* cookie = _make_set_session_cookie_header(work->request, session_token);
tf_printf("COOKIE = %s\n", session_token);
tf_free((void*)session_token); tf_free((void*)session_token);
const char* headers[] = { const char* headers[] = {
"Set-Cookie", "Set-Cookie",
@ -2249,7 +2249,6 @@ static void _httpd_auto_login_after_work(tf_ssb_t* ssb, int status, void* user_d
} }
else else
{ {
tf_printf("NO COOKIE FOR YOU\n");
const char* headers[] = { const char* headers[] = {
"Location", "Location",
"/", "/",
@ -2257,6 +2256,7 @@ static void _httpd_auto_login_after_work(tf_ssb_t* ssb, int status, void* user_d
tf_http_respond(work->request, 303, headers, tf_countof(headers) / 2, NULL, 0); tf_http_respond(work->request, 303, headers, tf_countof(headers) / 2, NULL, 0);
} }
tf_http_request_unref(work->request); tf_http_request_unref(work->request);
tf_free((void*)work->users);
tf_free(work); tf_free(work);
} }