Skip to content

Commit e3ae61b

Browse files
committed
fix: Add 401 response for non-public pages
Signed-off-by: provokateurin <[email protected]>
1 parent 813750b commit e3ae61b

File tree

7 files changed

+4559
-179
lines changed

7 files changed

+4559
-179
lines changed

generate-spec.php

Lines changed: 9 additions & 1 deletion
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) {
@@ -757,6 +756,15 @@
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' => Helpers::addOCSResponseWrapper(new stdClass()),
765+
],
766+
],
767+
];
760768
}
761769
if (!$route->isCORS) {
762770
// Bearer auth is not allowed on CORS routes

src/Helpers.php

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -114,32 +114,36 @@ public static function wrapOCSResponse(Route $route, ControllerMethodResponse $r
114114
if ($route->isOCS
115115
&& ($response->className === 'DataResponse'
116116
|| (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,
134-
],
135-
],
136-
],
137-
];
117+
return self::addOCSResponseWrapper($schema);
138118
}
139119

140120
return $schema;
141121
}
142122

123+
public static function addOCSResponseWrapper(array|stdClass $schema): array {
124+
return [
125+
'type' => 'object',
126+
'required' => [
127+
'ocs',
128+
],
129+
'properties' => [
130+
'ocs' => [
131+
'type' => 'object',
132+
'required' => [
133+
'meta',
134+
'data',
135+
],
136+
'properties' => [
137+
'meta' => [
138+
'$ref' => '#/components/schemas/OCSMeta',
139+
],
140+
'data' => $schema,
141+
],
142+
],
143+
],
144+
];
145+
}
146+
143147
public static function cleanEmptyResponseArray(array $schema): array|stdClass {
144148
if (array_key_exists('type', $schema) && $schema['type'] === 'array' && array_key_exists('maxItems', $schema) && $schema['maxItems'] === 0) {
145149
return new stdClass();

0 commit comments

Comments
 (0)