backend/source/helpers.ts

170 lines
3.3 KiB
TypeScript

namespace _espe.helpers
{
/**
*/
/*
export function yaml_encode(
thing : any
) : string
{
}
*/
/**
* @todo outsource
*/
export async function bcrypt_compute(
input : string,
options : {
rounds ?: int;
} = {}
) : Promise<string>
{
options = Object.assign(
{
"rounds": 12,
},
options
);
const nm_bcrypt = require("bcrypt");
const salt : string = await nm_bcrypt.genSalt(options.rounds)
const result : string = await nm_bcrypt.hash(input, salt);
return result;
}
/**
* @todo outsource
*/
export async function bcrypt_compare(
password_shall_image : string,
password_is : string
) : Promise<boolean>
{
const nm_bcrypt = require("bcrypt");
const result = await nm_bcrypt.compare(password_is, password_shall_image);
return result;
}
/**
*/
export function database_implementation(
) : lib_plankton.database.type_database
{
switch (_espe.conf.get().database.kind) {
case "sqlite": {
return lib_plankton.database.sqlite_database(
{
"path": _espe.conf.get().database.data["path"],
"verbose": false,
}
);
break;
}
default: {
throw (new Error("database implementation not available: " + _espe.conf.get().database.kind));
}
}
}
/**
*/
export async function verification_get(
data : any
) : Promise<string>
{
const secret : (null | string) = _espe.conf.get().general.verification_secret;
if (secret === null) {
return Promise.reject<string>(new Error("no verification secret specified; add in conf as 'general.verification_secret'!"));
}
else {
return lib_plankton.sha256.get(
lib_plankton.json.encode(data),
secret
);
}
}
/**
*/
export async function verification_check(
data : any,
verification : string
) : Promise<boolean>
{
const secret : (null | string) = _espe.conf.get().general.verification_secret;
if (secret === null) {
return Promise.reject<boolean>(new Error("no verification secret specified; add in conf as 'general.verification_secret'!"));
}
else {
const verification_expected : string = lib_plankton.sha256.get(
lib_plankton.json.encode(data),
secret
);
return (verification === verification_expected);
}
}
/**
*/
export async function email_send(
receivers : Array<string>,
subject : string,
content : string
) : Promise<void>
{
const mode : string = _espe.conf.get().email_sending.mode;
lib_plankton.log.info(
"email_send",
{
"mode": mode,
"receivers": receivers,
"subject": subject,
}
);
switch (mode) {
case "regular": {
if (_espe.conf.get().email_sending.smtp_credentials === null) {
return Promise.reject<void>("no smtp credentials specified; add in conf as 'email_sending.smtp_credentials'!");
}
else {
// TODO
}
break;
}
case "redirect": {
if (_espe.conf.get().email_sending.smtp_credentials === null) {
return Promise.reject<void>("no smtp credentials specified; add in conf as 'email_sending.smtp_credentials'!");
}
else {
// TODO
}
break;
}
case "console": {
lib_plankton.log.info(
"email_send_content",
{
"content": content,
}
);
break;
}
case "drop": {
// do nothing
break;
}
default: {
throw (new Error("unhandled email mode: " + mode));
break;
}
}
}
}