Skip to content

Commit 00615f1

Browse files
committed
fix: don't return HTML code for a 401 in an API call (#3313)
1 parent e9be9db commit 00615f1

File tree

5 files changed

+77
-9
lines changed

5 files changed

+77
-9
lines changed

docs/openapi.json

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"openapi": "3.0.0",
33
"info": {
4-
"title": "REST API for phpMyFAQ 4.0",
4+
"title": "REST API for phpMyFAQ 4.1",
55
"description": "phpMyFAQ includes a REST API and offers APIs for various services like fetching the phpMyFAQ version or doing a search against the phpMyFAQ installation.",
66
"contact": {
77
"name": "phpMyFAQ Team",
@@ -719,7 +719,7 @@
719719
"content": {
720720
"application/json": {
721721
"schema": {},
722-
"example": "[\n {\n \"question\": \"How can I survive without phpMyFAQ?\",\n \"url\": \"https://www.example.org/index.php?action=faq&cat=1&id=36&artlang=de\",\n \"id\": \"8\",\n \"order\": \"1\"\n },\n {\n \"question\": \"Is there life after death?\",\n \"url\": \"https://www.example.org/index.php?action=faq&cat=1&id=1&artlang=en\",\n \"id\": \"10\",\n \"order\": \"2\"\n }\n ]"
722+
"example": "[\n {\n \"question\": \"How can I survive without phpMyFAQ?\",\n \"url\": \"https://www.example.org/index.php?action=faq&cat=1&id=36&artlang=de\",\n \"id\": 8,\n \"order\": 1\n },\n {\n \"question\": \"Is there life after death?\",\n \"url\": \"https://www.example.org/index.php?action=faq&cat=1&id=1&artlang=en\",\n \"id\": 10,\n \"order\": 2\n }\n ]"
723723
}
724724
}
725725
},
@@ -1641,7 +1641,43 @@
16411641
"content": {
16421642
"application/json": {
16431643
"schema": {},
1644-
"example": "phpMyFAQ Codename Pontus"
1644+
"example": "phpMyFAQ Codename Porus"
1645+
}
1646+
}
1647+
}
1648+
}
1649+
}
1650+
},
1651+
"/api/v3.1/update": {
1652+
"post": {
1653+
"tags": ["Endpoints with Authentication"],
1654+
"operationId": "triggerUpdate",
1655+
"responses": {
1656+
"200": {
1657+
"description": "Returns the new and updated phpMyFAQ version number as string.",
1658+
"headers": {
1659+
"x-pmf-token": {
1660+
"description": "phpMyFAQ client API Token, generated in admin backend",
1661+
"schema": {
1662+
"type": "string"
1663+
}
1664+
}
1665+
},
1666+
"content": {
1667+
"application/json": {
1668+
"schema": {},
1669+
"example": "4.0.0"
1670+
}
1671+
}
1672+
},
1673+
"401": {
1674+
"description": "If the user is not authenticated and/or does not have sufficient permissions.",
1675+
"headers": {
1676+
"x-pmf-token": {
1677+
"description": "phpMyFAQ client API Token, generated in admin backend",
1678+
"schema": {
1679+
"type": "string"
1680+
}
16451681
}
16461682
}
16471683
}

docs/openapi.yaml

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
openapi: 3.0.0
22
info:
3-
title: 'REST API for phpMyFAQ 4.0'
3+
title: 'REST API for phpMyFAQ 4.1'
44
description: 'phpMyFAQ includes a REST API and offers APIs for various services like fetching the phpMyFAQ version or doing a search against the phpMyFAQ installation.'
55
contact:
66
name: 'phpMyFAQ Team'
@@ -484,7 +484,7 @@ paths:
484484
content:
485485
application/json:
486486
schema: {}
487-
example: "[\n {\n \"question\": \"How can I survive without phpMyFAQ?\",\n \"url\": \"https://www.example.org/index.php?action=faq&cat=1&id=36&artlang=de\",\n \"id\": \"8\",\n \"order\": \"1\"\n },\n {\n \"question\": \"Is there life after death?\",\n \"url\": \"https://www.example.org/index.php?action=faq&cat=1&id=1&artlang=en\",\n \"id\": \"10\",\n \"order\": \"2\"\n }\n ]"
487+
example: "[\n {\n \"question\": \"How can I survive without phpMyFAQ?\",\n \"url\": \"https://www.example.org/index.php?action=faq&cat=1&id=36&artlang=de\",\n \"id\": 8,\n \"order\": 1\n },\n {\n \"question\": \"Is there life after death?\",\n \"url\": \"https://www.example.org/index.php?action=faq&cat=1&id=1&artlang=en\",\n \"id\": 10,\n \"order\": 2\n }\n ]"
488488
'404':
489489
description: "If there's not one sticky FAQ."
490490
headers:
@@ -1114,7 +1114,31 @@ paths:
11141114
content:
11151115
application/json:
11161116
schema: {}
1117-
example: 'phpMyFAQ Codename Pontus'
1117+
example: 'phpMyFAQ Codename Porus'
1118+
/api/v3.1/update:
1119+
post:
1120+
tags:
1121+
- 'Endpoints with Authentication'
1122+
operationId: triggerUpdate
1123+
responses:
1124+
'200':
1125+
description: 'Returns the new and updated phpMyFAQ version number as string.'
1126+
headers:
1127+
x-pmf-token:
1128+
description: 'phpMyFAQ client API Token, generated in admin backend'
1129+
schema:
1130+
type: string
1131+
content:
1132+
application/json:
1133+
schema: {}
1134+
example: 4.0.0
1135+
'401':
1136+
description: 'If the user is not authenticated and/or does not have sufficient permissions.'
1137+
headers:
1138+
x-pmf-token:
1139+
description: 'phpMyFAQ client API Token, generated in admin backend'
1140+
schema:
1141+
type: string
11181142
/api/v3.0/version:
11191143
get:
11201144
tags:

phpmyfaq/api/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
$loader = new PhpFileLoader($container, new FileLocator(__DIR__));
3030
try {
3131
$loader->load('../src/services.php');
32-
} catch (\Exception $e) {
32+
} catch (Exception $e) {
3333
echo $e->getMessage();
3434
}
3535

phpmyfaq/src/phpMyFAQ/Application.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,15 @@ private function handleRequest(RouteCollection $routeCollection): void
116116
Response::HTTP_NOT_FOUND
117117
);
118118
} catch (UnauthorizedHttpException) {
119-
$response = new RedirectResponse('/login');
119+
if (str_contains($urlMatcher->getContext()->getBaseUrl(), '/api')) {
120+
$response = new Response(
121+
json_encode(['error' => 'Unauthorized access']),
122+
Response::HTTP_UNAUTHORIZED,
123+
['Content-Type' => 'application/json']
124+
);
125+
} else {
126+
$response = new RedirectResponse('/login');
127+
}
120128
} catch (BadRequestException $exception) {
121129
$response = new Response(
122130
sprintf(

phpmyfaq/src/phpMyFAQ/Controller/AbstractController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
version: '3.0',
4141
description: 'phpMyFAQ includes a REST API and offers APIs for various services like fetching the phpMyFAQ ' .
4242
'version or doing a search against the phpMyFAQ installation.',
43-
title: 'REST API for phpMyFAQ 4.0',
43+
title: 'REST API for phpMyFAQ 4.1',
4444
contact: new OA\Contact(
4545
name: 'phpMyFAQ Team',
4646
email: 'support@phpmyfaq.de'

0 commit comments

Comments
 (0)