Make auth use JWTs.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3991 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2022-09-28 23:52:44 +00:00
parent 5b3ae3f006
commit 113a82b382
4 changed files with 232 additions and 36 deletions

View File

@ -4,8 +4,8 @@
#include "task.h"
#include "trace.h"
#include "quickjs-libc.h"
#include <base64c.h>
#include <quickjs-libc.h>
#include <uv.h>
#include <string.h>
@ -65,6 +65,42 @@ JSValue tf_util_utf8_decode(JSContext* context, JSValue value)
return _util_utf8_decode(context, JS_NULL, 1, &value);
}
static JSValue _util_base64_encode(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
JSValue result = JS_UNDEFINED;
size_t length = 0;
const char* value = JS_ToCStringLen(context, &length, argv[0]);
char* encoded = tf_malloc(length * 4);
int r = base64c_encode((const uint8_t*)value, length, (uint8_t*)encoded, length * 4);
if (r >= 0)
{
result = JS_NewStringLen(context, encoded, r);
}
tf_free(encoded);
JS_FreeCString(context, value);
return result;
}
static JSValue _util_base64_decode(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
JSValue result = JS_UNDEFINED;
size_t length = 0;
const char* value = JS_ToCStringLen(context, &length, argv[0]);
char* encoded = tf_malloc(length);
int r = base64c_decode((const uint8_t*)value, length, (uint8_t*)encoded, length);
if (r >= 0)
{
result = JS_NewStringLen(context, encoded, r);
}
tf_free(encoded);
JS_FreeCString(context, value);
return result;
}
uint8_t* tf_util_try_get_array_buffer(JSContext* context, size_t* psize, JSValueConst obj)
{
uint8_t* result = JS_GetArrayBuffer(context, psize, obj);
@ -197,6 +233,8 @@ void tf_util_register(JSContext* context)
JSValue global = JS_GetGlobalObject(context);
JS_SetPropertyStr(context, global, "utf8Decode", JS_NewCFunction(context, _util_utf8_decode, "utf8Decode", 1));
JS_SetPropertyStr(context, global, "utf8Encode", JS_NewCFunction(context, _util_utf8_encode, "utf8Encode", 1));
JS_SetPropertyStr(context, global, "base64Decode", JS_NewCFunction(context, _util_base64_decode, "base64Decode", 1));
JS_SetPropertyStr(context, global, "base64Encode", JS_NewCFunction(context, _util_base64_encode, "base64Encode", 1));
JS_SetPropertyStr(context, global, "print", JS_NewCFunction(context, _util_print, "print", 1));
JS_SetPropertyStr(context, global, "setTimeout", JS_NewCFunction(context, _util_setTimeout, "setTimeout", 2));
JS_FreeValue(context, global);