Skip to content

Commit 65eab06

Browse files
committed
fix: Add 401 response for non-public pages
Signed-off-by: provokateurin <kate@provokateurin.de>
1 parent 813750b commit 65eab06

8 files changed

Lines changed: 4512 additions & 146 deletions

generate-spec.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,6 @@
465465
$isIgnored = Helpers::classMethodHasAnnotationOrAttribute($methodFunction, 'IgnoreOpenAPI');
466466
$isPasswordConfirmation = Helpers::classMethodHasAnnotationOrAttribute($methodFunction, 'PasswordConfirmationRequired');
467467
$isExApp = Helpers::classMethodHasAnnotationOrAttribute($methodFunction, 'ExAppRequired');
468-
$isCORS = Helpers::classMethodHasAnnotationOrAttribute($methodFunction, 'CORS');
469468
$scopes = Helpers::getOpenAPIAttributeScopes($classMethod, $routeName);
470469

471470
if ($isIgnored) {
@@ -719,14 +718,14 @@
719718
$mergedContentTypeResponses[$contentType] = [];
720719
} else {
721720
$schema = Helpers::cleanEmptyResponseArray($contentTypeResponses[0]->type->toArray());
722-
$mergedContentTypeResponses[$contentType] = ['schema' => Helpers::wrapOCSResponse($route, $contentTypeResponses[0], $schema)];
721+
$mergedContentTypeResponses[$contentType] = ['schema' => $route->isOCS && $contentTypeResponses[0]->isOCS() ? Helpers::wrapOCSResponse($schema) : $schema];
723722
}
724723
} else {
725724
$mergedContentTypeResponses[$contentType] = [
726725
'schema' => [
727726
[$hasEmpty ? 'anyOf' : 'oneOf' => array_map(function (ControllerMethodResponse $response) use ($route): \stdClass|array {
728727
$schema = Helpers::cleanEmptyResponseArray($response->type->toArray());
729-
return Helpers::wrapOCSResponse($route, $response, $schema);
728+
return $route->isOCS && $response->isOCS() ? Helpers::wrapOCSResponse($schema) : $schema;
730729
}, $uniqueResponses)],
731730
],
732731
];
@@ -757,6 +756,27 @@
757756
if ($route->isPublic) {
758757
// Add empty authentication, meaning that it's optional. We can't know if there is a difference in behaviour for authenticated vs. unauthenticated access on public pages (e.g. capabilities)
759758
$security[] = new stdClass();
759+
} else {
760+
$mergedResponses[401] ??= [
761+
'description' => 'Current user is not logged in',
762+
'content' => [
763+
'application/json' => [
764+
'schema' => $route->isOCS
765+
? Helpers::wrapOCSResponse(new stdClass())
766+
: [
767+
'type' => 'object',
768+
'required' => [
769+
'message',
770+
],
771+
'properties' => [
772+
'message' => [
773+
'type' => 'string',
774+
],
775+
],
776+
],
777+
],
778+
],
779+
];
760780
}
761781
if (!$route->isCORS) {
762782
// Bearer auth is not allowed on CORS routes

src/ControllerMethodResponse.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@ public function __construct(
1919
public ?array $headers = null,
2020
) {
2121
}
22+
23+
public function isOCS(): bool {
24+
return $this->className === 'DataResponse' || (str_starts_with($this->className, 'OCS') && str_ends_with($this->className, 'Exception'));
25+
}
2226
}

src/Helpers.php

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -110,34 +110,28 @@ public static function mergeSchemas(array $schemas): mixed {
110110
return $result;
111111
}
112112

113-
public static function wrapOCSResponse(Route $route, ControllerMethodResponse $response, array|stdClass $schema): array|stdClass {
114-
if ($route->isOCS
115-
&& ($response->className === 'DataResponse'
116-
|| (str_starts_with($response->className, 'OCS') && str_ends_with($response->className, 'Exception')))) {
117-
return [
118-
'type' => 'object',
119-
'required' => [
120-
'ocs',
121-
],
122-
'properties' => [
123-
'ocs' => [
124-
'type' => 'object',
125-
'required' => [
126-
'meta',
127-
'data',
128-
],
129-
'properties' => [
130-
'meta' => [
131-
'$ref' => '#/components/schemas/OCSMeta',
132-
],
133-
'data' => $schema,
113+
public static function wrapOCSResponse(array|stdClass $schema): array|stdClass {
114+
return [
115+
'type' => 'object',
116+
'required' => [
117+
'ocs',
118+
],
119+
'properties' => [
120+
'ocs' => [
121+
'type' => 'object',
122+
'required' => [
123+
'meta',
124+
'data',
125+
],
126+
'properties' => [
127+
'meta' => [
128+
'$ref' => '#/components/schemas/OCSMeta',
134129
],
130+
'data' => $schema,
135131
],
136132
],
137-
];
138-
}
139-
140-
return $schema;
133+
],
134+
];
141135
}
142136

143137
public static function cleanEmptyResponseArray(array $schema): array|stdClass {

0 commit comments

Comments
 (0)