Skip to content

Commit 628bea7

Browse files
committed
FEAT: made helper API endpoints compliant to json API standard, by putting no resource record responses in the metadata
1 parent 044c4ce commit 628bea7

15 files changed

+40
-22
lines changed

src/inc/apiv2/common/AbstractBaseAPI.class.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ protected function object2Array(object $object, array $expands = []): array
659659
/**
660660
* Uniform conversion of php array to JSON output
661661
*/
662-
protected function ret2json(array $result): string
662+
protected static function ret2json(array $result): string
663663
{
664664
return json_encode($result, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR) . PHP_EOL;
665665
}
@@ -1159,7 +1159,7 @@ protected function getQueryParameterFamily(Request $request, string $family): ar
11591159
return $retval;
11601160
}
11611161

1162-
static function createJsonResponse(array $data = [], array $links = [], array $included = []) {
1162+
static function createJsonResponse(array $data = [], array $links = [], array $included = [], array $meta = []) {
11631163
$response = [
11641164
"jsonapi" => [
11651165
"version" => "1.1",
@@ -1173,6 +1173,10 @@ static function createJsonResponse(array $data = [], array $links = [], array $i
11731173
$response["links"] = $links;
11741174
}
11751175

1176+
if(!empty($meta)) {
1177+
$response["meta"] = $meta;
1178+
}
1179+
11761180
$response["data"] = $data;
11771181

11781182
if (!empty($included)) {
@@ -1251,6 +1255,15 @@ protected static function getOneResource(object $apiClass, object $object, Reque
12511255
//case of a POST request
12521256
}
12531257

1258+
//Meta response for helper functions that do not respond with resource records
1259+
protected static function getMetaResponse(array $meta, Request $request, Response $response, int $statusCode=200) {
1260+
$ret = self::createJsonResponse($meta=$meta);
1261+
$body = $response->getBody();
1262+
$body->write(self::ret2json($ret));
1263+
1264+
return $response->withStatus($statusCode)->withHeader("Content-Type", "application/vnd.api+json");
1265+
}
1266+
12541267
/**
12551268
* Override-able activated methods
12561269
*/

src/inc/apiv2/common/AbstractHelperAPI.class.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use DBA\User;
1818

1919
abstract class AbstractHelperAPI extends AbstractBaseAPI {
20-
abstract public function actionPost(array $data): object|null;
20+
abstract public function actionPost(array $data): object|array|null;
2121

2222

2323
/* Chunk API endpoint specific call to abort chunk */
@@ -43,9 +43,15 @@ public function processPost(Request $request, Response $response, array $args):
4343
return $response->withStatus(204);
4444
}
4545

46+
4647
/* Succesful executed action of create */
47-
$apiClass = new ($this->container->get('classMapper')->get($newObject::class))($this->container);
48-
return self::getOneResource($apiClass, $newObject, $request, $response);
48+
if (is_object($newObject)) {
49+
$apiClass = new ($this->container->get('classMapper')->get($newObject::class))($this->container);
50+
return self::getOneResource($apiClass, $newObject, $request, $response);
51+
/* A meta response of a helper function */
52+
} elseif (is_array($newObject)) {
53+
return self::getMetaResponse($newObject, $request, $response);
54+
}
4955
}
5056

5157
/**

src/inc/apiv2/helper/abortChunk.routes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function getFormFields(): array {
2424
];
2525
}
2626

27-
public function actionPost(array $data): object|null {
27+
public function actionPost(array $data): object|array|null {
2828
$chunk = self::getChunk($data[Chunk::CHUNK_ID]);
2929

3030
TaskUtils::abortChunk($chunk->getId(), $this->getCurrentUser());

src/inc/apiv2/helper/assignAgent.routes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function getFormFields(): array {
2525
];
2626
}
2727

28-
public function actionPost($data): array|null {
28+
public function actionPost($data): object|array|null {
2929
AgentUtils::assign($data[Agent::AGENT_ID], $data[Task::TASK_ID], $this->getCurrentUser());
3030

3131
# TODO: Check how to handle custom return messages that are not object, probably we want that to be in some kind of standardized form.

src/inc/apiv2/helper/createSuperHashlist.routes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function getFormFields(): array
3434
];
3535
}
3636

37-
public function actionPost($data): object|null {
37+
public function actionPost($data): object|array|null {
3838
/* Validate incoming hashlists */
3939
$hashlistIds = [];
4040
foreach($data["hashlistIds"] as $hashlistId) {

src/inc/apiv2/helper/createSupertask.routes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function getFormFields(): array
3434
];
3535
}
3636

37-
public function actionPost($data): object|null {
37+
public function actionPost($data): object|array|null {
3838
$supertaskTemplate = self::getSupertask($data["supertaskTemplateId"]);
3939
$hashlist = self::getHashlist($data[Hashlist::HASHLIST_ID]);
4040
$crackerBinary = self::getCrackerBinary($data["crackerVersionId"]);

src/inc/apiv2/helper/exportCrackedHashes.routes.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ public function getFormFields(): array
2626
];
2727
}
2828

29-
public function actionPost($data): array|null {
29+
public function actionPost($data): object|array|null {
3030
$hashlist = self::getHashlist($data[Hashlist::HASHLIST_ID]);
3131

3232
$file = HashlistUtils::export($hashlist->getId(), $this->getCurrentUser());
33-
return $this->object2Array($file);
33+
return $file;
3434
}
3535
}
3636

src/inc/apiv2/helper/exportLeftHashes.routes.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ public function getFormFields(): array
2626
];
2727
}
2828

29-
public function actionPost($data): array|null {
29+
public function actionPost($data): object|array|null {
3030
$hashlist = self::getHashlist($data[Hashlist::HASHLIST_ID]);
3131

3232
$file = HashlistUtils::leftlist($hashlist->getId(), $this->getCurrentUser());
3333

34-
return $this->object2Array($file);
34+
return $file;
3535
}
3636
}
3737

src/inc/apiv2/helper/exportWordlist.routes.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ public function getFormFields(): array
2626
];
2727
}
2828

29-
public function actionPost($data): array|null {
29+
public function actionPost($data): object|array|null {
3030
$hashlist = self::getHashlist($data[Hashlist::HASHLIST_ID]);
3131

3232
$arr = HashlistUtils::createWordlists($hashlist->getId(), $this->getCurrentUser());
3333

34-
return $this->object2Array($arr[2]);
34+
return $arr[2];
3535
}
3636
}
3737

src/inc/apiv2/helper/importCrackedHashes.routes.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@ public function getFormFields(): array {
2626
];
2727
}
2828

29-
public function actionPost($data): array|null {
29+
public function actionPost($data): object|array|null {
3030
$hashlist = self::getHashlist($data[Hashlist::HASHLIST_ID]);
3131

3232
$result = HashlistUtils::processZap($hashlist->getId(), $data["separator"], "paste", ["hashfield" => $data["sourceData"]], [], $this->getCurrentUser());
3333

34-
# TODO: Check how to handle custom return messages that are not object, probably we want that to be in some kind of standardized form.
3534
return [
3635
"totalLines" => $result[0],
3736
"newCracked" => $result[1],

0 commit comments

Comments
 (0)