Skip to content

Commit 814b8c6

Browse files
refactor: DRY bearer token validation and middleware usage
1 parent 1b5c1be commit 814b8c6

File tree

3 files changed

+27
-28
lines changed

3 files changed

+27
-28
lines changed

src/classes/Api/Middlewares.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,31 @@ public static function tryResolvePage(array $context, array $args)
9595
}
9696

9797
/**
98-
* Validates the bearer token sent with the request
98+
* Validates the bearer token sent with the request (without Panel redirect)
99+
*/
100+
public static function hasBearerTokenWithoutRedirect()
101+
{
102+
return fn (array $context, array $args) => static::validateBearerToken(false);
103+
}
104+
105+
/**
106+
* Validates the bearer token sent with the request (with Panel redirect)
99107
*/
100108
public static function hasBearerToken()
109+
{
110+
return fn (array $context, array $args) => static::validateBearerToken(true);
111+
}
112+
113+
/**
114+
* Validates the bearer token sent with the request
115+
*/
116+
public static function validateBearerToken(bool $panelRedirect = false)
101117
{
102118
$kirby = App::instance();
103119
$token = $kirby->option('headless.token');
104120
$authorization = $kirby->request()->header('Authorization');
105121

106-
if ($kirby->option('headless.panel.redirect', false) && empty($authorization)) {
122+
if ($panelRedirect && $kirby->option('headless.panel.redirect', false) && empty($authorization)) {
107123
go(Panel::url('site'));
108124
}
109125

src/extensions/api.php

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,15 @@
11
<?php
22

33
use JohannSchopplich\Headless\Api\Api;
4+
use JohannSchopplich\Headless\Api\Middlewares;
45
use Kirby\Cms\App;
56
use Kirby\Data\Json;
67
use Kirby\Exception\NotFoundException;
78
use Kirby\Http\Url;
89
use Kirby\Toolkit\Str;
910

10-
$validateOptionalBearerToken = function (array $context, array $args) {
11-
$kirby = App::instance();
12-
$token = $kirby->option('headless.token');
13-
$authorization = $kirby->request()->header('Authorization');
14-
15-
if (
16-
!empty($token) &&
17-
(empty($authorization) || $authorization !== 'Bearer ' . $token)
18-
) {
19-
return Api::createResponse(401);
20-
}
21-
};
22-
2311
return [
24-
'routes' => function (App $kirby) use ($validateOptionalBearerToken) {
12+
'routes' => function (App $kirby) {
2513
$kqlAuthMethod = $kirby->option('kql.auth', true);
2614

2715
return [
@@ -44,17 +32,12 @@
4432
'auth' => !in_array($kqlAuthMethod, [false, 'bearer'], true),
4533
'action' => Api::createHandler(
4634
// Middleware to validate the bearer token
47-
function (array $context, array $args) use ($kirby, $kqlAuthMethod) {
35+
function (array $context, array $args) use ($kqlAuthMethod) {
4836
if ($kqlAuthMethod !== 'bearer') {
4937
return;
5038
}
5139

52-
$token = $kirby->option('headless.token');
53-
$authorization = $kirby->request()->header('Authorization');
54-
55-
if ($authorization !== 'Bearer ' . $token) {
56-
return Api::createResponse(401);
57-
}
40+
return Middlewares::validateBearerToken();
5841
},
5942
// Middleware to run queries and cache their results
6043
function (array $context, array $args) use ($kirby) {
@@ -96,7 +79,7 @@ function (array $context, array $args) use ($kirby) {
9679
'method' => 'GET',
9780
'auth' => false,
9881
'action' => Api::createHandler(
99-
$validateOptionalBearerToken,
82+
Middlewares::hasBearerTokenWithoutRedirect(...),
10083
function (array $context, array $args) use ($kirby) {
10184
$data = $kirby->cache('pages')->getOrSet(
10285
'sitemap.headless.json',
@@ -173,7 +156,7 @@ function () use ($kirby) {
173156
'method' => 'GET|POST',
174157
'auth' => false,
175158
'action' => Api::createHandler(
176-
$validateOptionalBearerToken,
159+
Middlewares::hasBearerTokenWithoutRedirect(...),
177160
function (array $context, array $args) use ($kirby) {
178161
$templateName = $args[0] ?? null;
179162

src/extensions/routes.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
'pattern' => '(:all)',
2222
'language' => '*',
2323
'action' => Api::createHandler(
24-
[Middlewares::class, 'tryResolveFiles'],
25-
[Middlewares::class, 'hasBearerToken'],
26-
[Middlewares::class, 'tryResolvePage']
24+
Middlewares::tryResolveFiles(...),
25+
Middlewares::hasBearerToken(...),
26+
Middlewares::tryResolvePage(...)
2727
)
2828
]
2929
];

0 commit comments

Comments
 (0)