Skip to content

Commit

Permalink
better workaround for Logger issue, add React defaults to .env-dist, …
Browse files Browse the repository at this point in the history
…add token endpoint
  • Loading branch information
killua-eu committed Sep 27, 2023
1 parent 9916745 commit 716a3c3
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@
"post-update-cmd": [
"composer migrate",
"composer configure || echo \"[FAIL] Failed to configure glued. Please make sure all env variables are set. Rerun composer configure.\"",
"patch -s --reject-file=/dev/null -p1 vendor/monolog/monolog/src/Monolog/Logger.php < vendor/vaizard/glued-lib/src/Patches/Logger.patch",
"echo \"Run 'composer nginx' manually to pick restart this microservice\""
],
"backup": [
Expand All @@ -115,9 +114,11 @@
"vendor/vaizard/glued-lib/src/Scripts/migrate.sh"
],
"nginx": [
"patch -s --reject-file=/dev/null -p1 vendor/monolog/monolog/src/Monolog/Logger.php < vendor/vaizard/glued-lib/src/Patches/Logger.patch",
"vendor/vaizard/glued-lib/src/Scripts/nginx.sh",
"Glued\\Lib\\ComposerHooks::generateNginx",
"vendor/vaizard/glued-lib/src/Scripts/nginx-reload.sh"
"vendor/vaizard/glued-lib/src/Scripts/nginx-reload.sh",
"patch -Rs --reject-file=/dev/null -p1 vendor/monolog/monolog/src/Monolog/Logger.php < vendor/vaizard/glued-lib/src/Patches/Logger.patch"
],
"react": [
"glued/Config/React/deploy.sh"
Expand Down
2 changes: 2 additions & 0 deletions glued/Config/React/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ git clone --depth=1 https://github.com/vaizard/glued-react
pushd glued-react
git pull
cp -r ../../glued-core/.env ./.env
export $(echo $(cat .env | sed 's/#.*//g'| xargs) | envsubst);
export $(echo $(cat .env | sed 's/#.*//g'| xargs) | envsubst);
npm install
npm run build
cp -r ./build/* ../../glued-core/public
Expand Down
9 changes: 9 additions & 0 deletions glued/Config/routes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ routes:
methods:
get: Glued\Controllers\AuthController:users_r1
post: Glued\Controllers\AuthController:users_c1
be_core_auth_tokens_v1:
pattern: ${routes.be_core.path}/auth/tokens/v1
path: ${routes.be_core.path}/auth/tokens/v1
label: Manage tokens
dscr: Api endpoint for managing tokens.
service: core
methods:
get: Glued\Controllers\AuthController:tokens_r1
post: Glued\Controllers\AuthController:tokens_c1
be_core_auth_roles_v1:
pattern: ${routes.be_core.path}/auth/roles/v1
path: ${routes.be_core.path}/auth/roles/v1
Expand Down
44 changes: 44 additions & 0 deletions glued/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,50 @@ public function users_r0(Request $request, Response $response, array $args = [])
}


public function tokens_c1(Request $request, Response $response, array $args = []): Response
{
$rp = $request->getQueryParams();
$attr = json_decode($rp['attr'] ?? "{}", true);
if (!isset($attr['consumer']['type']) || !in_array($attr['consumer']['type'], ['svc', 'app'])) {
throw new \Exception("Invalid or missing 'consumer.type' attribute.");
}
if ($attr['consumer']['type'] === 'svc') {
$requiredKeys = ['name', 'host'];
foreach ($requiredKeys as $key) {
if (!isset($attr['consumer'][$key])) { throw new \Exception("For 'svc' type, 'consumer.$key' must be set."); }
}
}
$owner = $_SERVER['HTTP_X-GLUED-AUTH-UUID'] ?? 'anonymous';
if ($owner === 'anonymous') { throw new \Exception("Only authorized users can add tokens.", 403); }
$r = $this->auth->generate_api_token($owner, expiry: null, attributes: $attr);
return $response->withJson($r);
}



public function tokens_r1(Request $request, Response $response, array $args = []): Response {
$rp = $request->getQueryParams() ?? [];
$qs = '
SELECT
json_merge(
json_object (
"uuid", bin_to_uuid(tok.c_uuid, true),
"exp", c_expired_at,
"owner_handle", u.c_handle,
"owner_uuid", bin_to_uuid(tok.c_inherit, true)
), tok.c_attr) as res_rows
FROM t_core_tokens AS tok
LEFT JOIN t_core_users AS u ON tok.c_inherit = u.c_uuid
';
$qp = null;
$wm = [];
$qs = (new \Glued\Lib\QueryBuilder())->select($qs);
$this->utils->mysqlJsonQueryFromRequest(reqparams: $rp, qstring: $qs, qparams: $qp, wheremods: $wm, jsonkey: 'tok.c_attr');
$res = $this->db->rawQuery($qs, $qp);
return $this->utils->mysqlJsonResponse($response, $res);
}


function mysqlJsonQueryFromRequest(array $reqparams, QuerySelect &$qstring, &$qparams, array $wheremods = []) {

// define fallback where modifier for the 'uuid' reqparam.
Expand Down
3 changes: 2 additions & 1 deletion glued/Controllers/ServiceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public function health(Request $request, Response $response, array $args = []):
'service' => basename(__ROOT__),
'provided-for' => $_SERVER['HTTP_X-GLUED-AUTH-UUID'] ?? 'anonymous'
];
//if ($data['provided-for'] !== 'anonymous') { $this->generateApiKey($_SERVER['HTTP_X-GLUED-AUTH-UUID']); }
//$data['x'] = $this->auth->verify_token()
if ($data['provided-for'] !== 'anonymous') { $this->auth->generate_api_token($_SERVER['HTTP_X-GLUED-AUTH-UUID']); }
return $response->withJson($data, options: JSON_UNESCAPED_SLASHES);
}

Expand Down

0 comments on commit 716a3c3

Please sign in to comment.