Files
cwsvJudo/homepage/participo/api.apiKeys.add.php
2025-12-07 20:40:33 +01:00

48 lines
1.3 KiB
PHP

<?php
/** @var array $CONFIG basic configurations (defined via bootstraping) */
/** @var array $SECRETS passwords and other stuff worth of protection (defined via bootstraping) */
require_once "bootstrap.php";
require_once "participoLib/participo.php";
require_once "participoLib/apiKey.php";
participo::initDb(
$CONFIG["cwsvJudo"]["db"]["host"],
$CONFIG["cwsvJudo"]["db"]["name"],
$CONFIG["cwsvJudo"]["db"]["user"],
$SECRETS["cwsvJudo"]["db"][$CONFIG["cwsvJudo"]["db"]["user"]],
$CONFIG["cwsvJudo"]["db"]["dbCharset"],
$CONFIG["cwsvJudo"]["db"]["outCharset"],
);
$jsonPost = file_get_contents("php://input");
$call = json_decode($jsonPost, $associative = true);
if ($call) {
$allowKey = ApiKey::loadFromDb($call["apiKey"]);
if (!$allowKey) {
die(json_encode(["success" => false]));
}
if (!$allowKey->isValidFor("apiKeys.create")) {
die(json_encode(["success" => false]));
}
$newKey = ApiKey::create();
$newLoginApiKey = new ApiKey(
null,
$call["userId"],
$newKey,
"login",
$call["endDate"],
);
$newLoginApiKey->addToDb();
$insertedApiKey = ApiKey::loadFromDb($newKey);
if (!$insertedApiKey) {
die(json_encode(["success" => false]));
}
echo json_encode(["success" => true, "apiKey" => $newKey]);
}