Compare commits

..

No commits in common. "243f1bb155475ca112996c7cad4471dd43842278" and "671cbfea012cc44e1fe69d59fa7b939b8391c3a3" have entirely different histories.

11 changed files with 113 additions and 281 deletions

View file

@ -1,11 +1,11 @@
/**
* @author fenris
*/
type int = number;
declare type int = number;
/**
* @author fenris
*/
type float = number;
declare type float = number;
declare var process: any;
declare var require: any;
declare class Buffer {
@ -22,7 +22,7 @@ declare namespace lib_plankton.base {
/**
* @author fenris
*/
type type_pseudopointer<type_value> = {
declare type type_pseudopointer<type_value> = {
value: type_value;
};
/**
@ -2315,7 +2315,7 @@ declare namespace lib_plankton.storage.memory {
clear(): Promise<void>;
write(key: any, value: any): Promise<boolean>;
delete(key: any): Promise<void>;
read(key: any): Promise<Awaited<type_item>>;
read(key: any): Promise<type_item>;
search(term: any): Promise<{
key: string;
preview: string;
@ -3073,25 +3073,22 @@ declare namespace lib_plankton.session {
};
/**
*/
function begin(name: string, { "lifetime": lifetime, "data": data, }?: {
function begin(name: string, options?: {
lifetime?: int;
data?: any;
}): Promise<string>;
/**
*/
function get(key: string, { "prolongation": prolongation, }?: {
prolongation?: (null | int);
}): Promise<type_session>;
function get(key: string): Promise<type_session>;
/**
*/
function end(key: string): Promise<void>;
/**
*/
function setup({ "key_length": key_length, "key_max_attempts": key_max_attempts, "default_lifetime": default_lifetime, "default_prolongation": default_prolongation, "data_chest": data_chest, "clear": clear, }?: {
function setup(options?: {
key_length?: int;
key_max_attempts?: int;
default_lifetime?: int;
default_prolongation?: (null | int);
data_chest?: lib_plankton.storage.type_chest<string, any, void, string, string>;
clear?: boolean;
}): Promise<void>;

View file

@ -1486,7 +1486,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
@ -6814,7 +6814,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
@ -10266,7 +10266,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
@ -10325,37 +10325,21 @@ var lib_plankton;
}
/**
*/
function session_encode(session) {
return {
"key": session.key,
"name": session.name,
"expiry": Math.floor(session.expiry),
"data": JSON.stringify(session.data)
};
}
/**
*/
function session_decode(session_raw) {
return {
"key": session_raw.key,
"name": session_raw.name,
"expiry": Math.floor(session_raw.expiry),
"data": JSON.parse(session_raw.data)
};
}
/**
*/
function begin(name, _a) {
var _b = _a === void 0 ? {} : _a, _c = _b["lifetime"], lifetime = _c === void 0 ? _conf.default_lifetime : _c, _d = _b["data"], data = _d === void 0 ? null : _d;
function begin(name, options) {
if (options === void 0) { options = {}; }
return __awaiter(this, void 0, void 0, function () {
var key, attempts, session_old, error_1, session_2, session_raw;
return __generator(this, function (_e) {
switch (_e.label) {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
options = Object.assign({
"lifetime": _conf.default_lifetime,
"data": null
}, options);
check_conf();
key = null;
attempts = 0;
_e.label = 1;
_a.label = 1;
case 1:
if (!true) return [3 /*break*/, 6];
attempts += 1;
@ -10365,15 +10349,15 @@ var lib_plankton;
function (x) { return x.join(""); },
]);
session_old = null;
_e.label = 2;
_a.label = 2;
case 2:
_e.trys.push([2, 4, , 5]);
_a.trys.push([2, 4, , 5]);
return [4 /*yield*/, _conf.data_chest.read(key)];
case 3:
session_old = _e.sent();
session_old = _a.sent();
return [3 /*break*/, 5];
case 4:
error_1 = _e.sent();
error_1 = _a.sent();
session_old = null;
return [3 /*break*/, 5];
case 5:
@ -10399,45 +10383,26 @@ var lib_plankton;
session_2 = {
"key": key,
"name": name,
"expiry": (lib_plankton.base.get_current_timestamp() + lifetime),
"data": data
"expiry": (lib_plankton.base.get_current_timestamp() + options.lifetime),
"data": options.data
};
session_raw = session_encode(session_2);
/*
lib_plankton.call.timeout(
() => {
lib_plankton.log.info(
"session.dropping_due_to_being_expired",
{
session_raw = {
"key": session_2.key,
"name": session_2.name,
"expiry": Math.floor(session_2.expiry),
"data": JSON.stringify(session_2.data)
};
lib_plankton.call.timeout(function () {
lib_plankton.log.info("session_dropping_due_to_being_expired", {
"key": key,
"name": name,
"lifetime": lifetime,
}
);
"lifetime": options.lifetime
});
end(key);
},
lifetime
);
*/
}, options.lifetime);
return [4 /*yield*/, _conf.data_chest.write(key, session_raw)];
case 8:
/*
lib_plankton.call.timeout(
() => {
lib_plankton.log.info(
"session.dropping_due_to_being_expired",
{
"key": key,
"name": name,
"lifetime": lifetime,
}
);
end(key);
},
lifetime
);
*/
_e.sent();
_a.sent();
return [2 /*return*/, Promise.resolve(key)];
}
});
@ -10446,51 +10411,29 @@ var lib_plankton;
session_1.begin = begin;
/**
*/
function get(key, _a) {
var _b = _a === void 0 ? {} : _a, _c = _b["prolongation"], prolongation = _c === void 0 ? _conf.default_prolongation : _c;
return __awaiter(this, void 0, void 0, function () {
var session_raw, session, now, expiry_old, expiry_new;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
function get(key) {
check_conf();
return [4 /*yield*/, _conf.data_chest.read(key)];
case 1:
session_raw = _d.sent();
session = session_decode(session_raw);
now = lib_plankton.base.get_current_timestamp();
if (!(now <= session.expiry)) return [3 /*break*/, 6];
if (!(prolongation === null)) return [3 /*break*/, 2];
return [3 /*break*/, 5];
case 2:
expiry_old = session.expiry;
expiry_new = Math.floor(now + prolongation);
if (!(expiry_new < session.expiry)) return [3 /*break*/, 3];
return [3 /*break*/, 5];
case 3:
lib_plankton.log.info("session.prolongating", {
"key": key,
"now": now,
"prolongation": prolongation,
"expiry_old": expiry_old,
"expiry_new": expiry_new
});
session.expiry = expiry_new;
return [4 /*yield*/, _conf.data_chest.write(key, session_encode(session))];
case 4:
_d.sent();
_d.label = 5;
case 5: return [2 /*return*/, Promise.resolve(session)];
case 6:
lib_plankton.log.info("session.dropping_due_to_being_stale", {
return (_conf.data_chest.read(key)
.then(function (session_raw) {
var session = {
"key": session_raw["key"],
"name": session_raw["name"],
"expiry": session_raw["expiry"],
"data": JSON.parse(session_raw["data"])
};
var now = lib_plankton.base.get_current_timestamp();
if (now > session.expiry) {
lib_plankton.log.info("session_dropping_due_to_being_stale", {
"key": session.key,
"name": session.name
});
end(key);
return [2 /*return*/, Promise.reject()];
return Promise.reject();
}
});
});
else {
return Promise.resolve(session);
}
}));
}
session_1.get = get;
/**
@ -10502,23 +10445,29 @@ var lib_plankton;
session_1.end = end;
/**
*/
function setup(_a) {
var _b = _a === void 0 ? {} : _a, _c = _b["key_length"], key_length = _c === void 0 ? 16 : _c, _d = _b["key_max_attempts"], key_max_attempts = _d === void 0 ? 3 : _d, _e = _b["default_lifetime"], default_lifetime = _e === void 0 ? 900 : _e, _f = _b["default_prolongation"], default_prolongation = _f === void 0 ? null : _f, _g = _b["data_chest"], data_chest = _g === void 0 ? lib_plankton.storage.memory.implementation_chest({}) : _g, _h = _b["clear"], clear = _h === void 0 ? false : _h;
function setup(options) {
if (options === void 0) { options = {}; }
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_j) {
switch (_j.label) {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
options = Object.assign({
"key_length": 16,
"key_max_attempts": 3,
"default_lifetime": 900,
"data_chest": lib_plankton.storage.memory.implementation_chest({}),
"clear": false
}, options);
_conf = {
"key_length": key_length,
"key_max_attempts": key_max_attempts,
"default_lifetime": default_lifetime,
"default_prolongation": default_prolongation,
"data_chest": data_chest
"key_length": options.key_length,
"key_max_attempts": options.key_max_attempts,
"default_lifetime": options.default_lifetime,
"data_chest": options.data_chest
};
if (!clear) return [3 /*break*/, 2];
if (!options.clear) return [3 /*break*/, 2];
return [4 /*yield*/, _conf.data_chest.clear()];
case 1:
_j.sent();
_a.sent();
return [3 /*break*/, 2];
case 2: return [2 /*return*/, Promise.resolve(undefined)];
}
@ -15199,7 +15148,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {

View file

@ -65,30 +65,23 @@ namespace _zeitbild.api
}),
"restriction": () => restriction_none,
"execution": () => async ({"input": input}) => {
if (input === null)
{
if (input === null) {
return Promise.reject(new Error("impossible"));
}
else
{
else {
const passed : boolean = await _zeitbild.service.auth_internal.check(input.name, input.password);
if (! passed)
{
return Promise.resolve(
{
if (! passed) {
return Promise.resolve({
"status_code": 403,
"data": null,
}
);
});
}
else {
const session_key : string = await lib_plankton.session.begin(input.name);
return Promise.resolve(
{
return Promise.resolve({
"status_code": 201,
"data": session_key,
}
);
});
}
}
},

View file

@ -43,12 +43,10 @@ namespace _zeitbild.api
"execution": async (stuff) => {
const session : {key : string; value : lib_plankton.session.type_session} = await session_from_stuff(stuff);
await lib_plankton.session.end(session.key);
return Promise.resolve(
{
return Promise.resolve({
"status_code": 200,
"data": null,
}
);
});
},
}
);

View file

@ -80,18 +80,15 @@ namespace _zeitbild.api
(stuff.headers["Cookie"] ?? stuff.headers["cookie"] ?? null),
stuff.query_parameters
);
if (data.userinfo.name === null)
{
if (data.userinfo.name === null) {
return Promise.reject(
new Error(
"IDP did not return user name"
)
);
}
else
{
try
{
else {
try {
await _zeitbild.service.user.add(
{
"name": data.userinfo.name,
@ -106,8 +103,7 @@ namespace _zeitbild.api
}
);
}
catch (error)
{
catch (error) {
// do nothing
}
const session_key : string = await lib_plankton.session.begin(

View file

@ -1,80 +0,0 @@
/*
This file is part of »zeitbild«.
Copyright 2025 'kcf' <fenris@folksprak.org>
»zeitbild« is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
»zeitbild« is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with »zeitbild«. If not, see <http://www.gnu.org/licenses/>.
*/
namespace _zeitbild.api
{
/**
*/
export function register_session_status(
rest_subject : lib_plankton.rest_http.type_rest
) : void
{
register<
null,
{
logged_in : boolean;
}
>(
rest_subject,
lib_plankton.http.enum_method.get,
"/session/status",
{
"description": "gibt Information über den Nutzer aus",
"output_schema": () => ({
"nullable": false,
"type": "object",
"additionalProperties": false,
"properties": {
"logged_in": {
"nullable": false,
"type": "boolean",
},
},
"required": [
"logged_in",
],
}),
"restriction": restriction_none,
"execution": async (stuff) => {
const user_id : (null | _zeitbild.type_user_id) = await (
session_from_stuff(stuff)
.then(
(session : {key : string; value : lib_plankton.session.type_session;}) => (
_zeitbild.service.user.identify(session.value.name)
.catch(x => Promise.resolve(null))
)
)
.catch(x => Promise.resolve(null))
);
return Promise.resolve(
{
"status_code": 200,
"data": {
"logged_in": (user_id !== null),
}
}
);
}
}
);
}
}

View file

@ -49,7 +49,6 @@ namespace _zeitbild.api
_zeitbild.api.register_session_begin(rest_subject);
_zeitbild.api.register_session_end(rest_subject);
_zeitbild.api.register_session_oidc(rest_subject);
_zeitbild.api.register_session_status(rest_subject);
}
// user
{

View file

@ -207,13 +207,8 @@ namespace _zeitbild.conf
"lifetime": {
"nullable": false,
"type": "integer",
"default": 3600,
},
"prolongation": {
"nullable": true,
"type": "integer",
"default": 300,
},
"default": 900
}
},
"required": [
],

View file

@ -366,10 +366,8 @@ async function main(
);
}
else {
switch (args["action"])
{
default:
{
switch (args["action"]) {
default: {
lib_plankton.log.error(
"main_invalid_action",
{
@ -378,8 +376,7 @@ async function main(
);
break;
}
case "conf-schema":
{
case "conf-schema": {
process.stdout.write(
JSON.stringify(
_zeitbild.conf.schema(),
@ -391,8 +388,7 @@ async function main(
);
break;
}
case "conf-expose":
{
case "conf-expose": {
process.stdout.write(
JSON.stringify(
_zeitbild.conf.get(),
@ -404,8 +400,7 @@ async function main(
);
break;
}
case "api-doc":
{
case "api-doc": {
lib_plankton.log.set_main_logger([]);
const rest_subject : lib_plankton.rest_http.type_rest = _zeitbild.api.make();
process.stdout.write(
@ -417,8 +412,7 @@ async function main(
);
break;
}
case "fill":
{
case "fill": {
await data_init(
lib_plankton.json.decode(
await lib_plankton.file.read(args.data_path)
@ -427,23 +421,16 @@ async function main(
process.stdout.write("-- done\n");
break;
}
case "serve":
{
case "serve": {
// prepare database
await _zeitbild.database.check();
/**
* @todo clear old sessions
*/
await lib_plankton.session.setup(
{
"data_chest": (
_zeitbild.conf.get().session_management.in_memory
?
lib_plankton.storage.memory.implementation_chest<lib_plankton.session.type_session>({})
:
lib_plankton.call.convey(
? lib_plankton.storage.memory.implementation_chest<lib_plankton.session.type_session>({})
: lib_plankton.call.convey(
lib_plankton.storage.sql_table_common.chest(
{
"database_implementation": _zeitbild.database.get_implementation(),
@ -465,7 +452,6 @@ async function main(
)
),
"default_lifetime": _zeitbild.conf.get().session_management.lifetime,
"default_prolongation": _zeitbild.conf.get().session_management.prolongation,
}
);

View file

@ -64,7 +64,6 @@ ${dir_temp}/zeitbild-unlinked.js: \
${dir_source}/api/actions/session_begin.ts \
${dir_source}/api/actions/session_oidc.ts \
${dir_source}/api/actions/session_end.ts \
${dir_source}/api/actions/session_status.ts \
${dir_source}/api/actions/users.ts \
${dir_source}/api/actions/user_dav_conf.ts \
${dir_source}/api/actions/user_dav_token.ts \

View file

@ -43,8 +43,8 @@ mkdir -p ${dir}
mkdir /tmp/sandbox -p
cd /tmp/sandbox
ptk fetch node ${modules}
schwamm --include=/tmp/sandbox/plankton.swm.json --output=dump:logic-decl > ${dir}/plankton.d.ts
schwamm --include=/tmp/sandbox/plankton.swm.json --output=dump:logic-impl > ${dir}/plankton.js
schwamm --include=/tmp/sandboxplankton.swm.json --output=dump:logic-decl > ${dir}/plankton.d.ts
schwamm --include=/tmp/sandboxplankton.swm.json --output=dump:logic-impl > ${dir}/plankton.js
exit
mkdir -p ${dir}