backend/source/api/base.ts

260 lines
6.6 KiB
TypeScript
Raw Normal View History

2024-09-12 00:03:29 +02:00
namespace _zeitbild.api
{
/**
* @todo zu plankton auslagern?
*/
type type_stuff = {
version: (null | string);
headers: Record<string, string>;
path_parameters: Record<string, string>;
query_parameters: Record<string, string>;
};
/**
*/
export async function session_from_stuff(
stuff : {headers : Record<string, string>;}
) : Promise<{key : string; value : lib_plankton.session.type_session}>
{
const key : string = (stuff.headers["X-Session-Key"] || stuff.headers["X-Session-Key".toLowerCase()]);
const value : lib_plankton.session.type_session = await lib_plankton.session.get(key);
return {"key": key, "value": value};
}
2024-11-14 19:57:13 +01:00
/**
* @todo outsource?
*/
export async function web_auth(
authorization_string : (null | string)
) : Promise<(null | _zeitbild.type_user_id)>
{
if (authorization_string === null) {
return Promise.resolve<(null | _zeitbild.type_user_id)>(null);
}
else {
const parts : Array<string> = authorization_string.split(" ");
const strategy : string = parts[0];
const data_raw : string = parts.slice(1).join(" ");
switch (strategy) {
default: {
lib_plankton.log.notice(
"zeitbild.web_auth.unhandled_strategy",
{
"strategy": strategy,
}
);
return Promise.resolve<(null | _zeitbild.type_user_id)>(null);
break;
}
case "Basic": {
const data_raw_decoded : string = lib_plankton.base64.decode(data_raw);
const parts_ : Array<string> = data_raw_decoded.split(":");
const username : string = parts_[0];
const password_is : string = parts_.slice(1).join(":");
const {"value": user_id, "error": error} = await lib_plankton.call.try_catch_wrap_async<_zeitbild.type_user_id>(
() => _zeitbild.service.user.identify(username)
);
if (error !== null) {
lib_plankton.log.notice(
"zeitbild.web_auth.unknown_user",
{
"username": username,
}
);
return Promise.resolve<(null | _zeitbild.type_user_id)>(null);
}
else {
const password_shall : string = lib_plankton.sha256.get(
username,
_zeitbild.conf.get()["misc"]["auth_salt"]
);
if (! (password_is === password_shall)) {
/**
* @todo remove
*/
lib_plankton.log.notice(
"zeitbild.web_auth.wrong_pasword",
{
"shall": password_shall,
"is": password_is,
}
);
return Promise.resolve<(null | _zeitbild.type_user_id)>(null);
}
else {
return Promise.resolve<(null | _zeitbild.type_user_id)>(user_id);
}
}
break;
}
}
}
}
2024-09-13 17:49:32 +02:00
/**
*/
2024-10-30 07:20:13 +01:00
export const restriction_logged_in : lib_plankton.rest_caldav.type_restriction<any> = (
2024-09-13 17:49:32 +02:00
(stuff) => (
session_from_stuff(stuff)
.then(() => Promise.resolve<boolean>(true))
.catch(() => Promise.resolve<boolean>(false))
)
);
2024-11-14 19:57:13 +01:00
/**
*/
export const restriction_basic_auth : lib_plankton.rest_caldav.type_restriction<any> = (
(stuff) => (
web_auth(
stuff.headers["Authorization"]
??
stuff.headers["authorization"]
??
null
)
.then<boolean>(
(user_id) => Promise.resolve<boolean>(
(user_id !== null)
)
)
)
);
2024-09-12 00:03:29 +02:00
/**
*/
2024-10-30 07:20:13 +01:00
export const restriction_none : lib_plankton.rest_caldav.type_restriction<any> = (
2024-09-12 00:03:29 +02:00
(stuff) => Promise.resolve<boolean>(true)
);
/**
*/
export function register<type_input, type_output>(
2024-10-30 07:20:13 +01:00
rest_subject : lib_plankton.rest_caldav.type_rest,
http_method : lib_plankton.caldav.enum_method,
2024-09-12 00:03:29 +02:00
path : string,
options : {
2024-11-28 23:08:24 +01:00
active ?: ((version : (null | string)) => boolean);
2024-10-30 07:20:13 +01:00
restriction ?: (null | lib_plankton.rest_caldav.type_restriction<type_input>);
execution ?: lib_plankton.rest_caldav.type_execution<type_input, type_output>;
2024-09-12 00:03:29 +02:00
title ?: (null | string);
description ?: (null | string);
2024-09-19 13:34:07 +02:00
query_parameters ?: ((version : (null | string)) => Array<
2024-09-12 00:03:29 +02:00
{
name : string;
description : (null | string);
required : boolean;
}
2024-09-19 13:34:07 +02:00
>);
2024-10-30 07:20:13 +01:00
input_schema ?: ((version: (null | string)) => lib_plankton.rest_caldav.type_oas_schema);
output_schema ?: ((version: (null | string)) => lib_plankton.rest_caldav.type_oas_schema);
2024-11-28 23:08:24 +01:00
request_body_mimetype ?: (
(version : (null | string))
=>
string
);
request_body_decode ?: (
(version : (null | string))
=>
(http_request_body : Buffer, http_request_header_content_type : (null | string))
=>
Promise<any>
);
2024-09-12 00:03:29 +02:00
response_body_mimetype ?: string;
2024-11-28 23:08:24 +01:00
response_body_encode ?: (
(output : any)
=>
Promise<Buffer>
);
2024-09-12 00:03:29 +02:00
} = {}
) : void
{
options = Object.assign(
{
},
options
);
2024-10-30 07:20:13 +01:00
lib_plankton.rest_caldav.register<type_input, type_output>(
2024-09-12 00:03:29 +02:00
rest_subject,
http_method,
(_zeitbild.conf.get().server.path_base + path),
2024-11-28 23:08:24 +01:00
{
"active": options.active,
/**
* @todo heed version
*/
"restriction": (
((options.restriction === undefined) || (options.restriction === null))
?
undefined
:
(version) => (options.restriction as lib_plankton.rest_caldav.type_restriction<type_input>)
),
/**
* @todo heed version
*/
"execution": (
((options.execution === undefined) || (options.execution === null))
?
undefined
:
(version) => (options.execution as lib_plankton.rest_caldav.type_execution<type_input, type_output>)
),
/**
* @todo heed version
*/
"title": (
((options.title === undefined) || (options.title === null))
?
(version) => null
:
(version) => (options.title as string)
),
/**
* @todo heed version
*/
"description": (
((options.description === undefined) || (options.description === null))
?
(version) => null
:
(version) => (options.description as string)
),
"query_parameters": options.query_parameters,
"input_schema": options.input_schema,
"output_schema": options.output_schema,
"request_body_mimetype": options.request_body_mimetype,
"request_body_decode": options.request_body_decode,
/**
* @todo heed version
*/
"response_body_mimetype": (
((options.response_body_mimetype === undefined) || (options.response_body_mimetype === null))
?
undefined
:
(version) => (options.response_body_mimetype as string)
),
/**
* @todo heed version
*/
"response_body_encode": (
((options.response_body_encode === undefined) || (options.response_body_encode === null))
?
undefined
:
(version) => (options.response_body_encode as ((output : any) => Promise<Buffer>))
),
}
2024-09-12 00:03:29 +02:00
);
}
}