about summary refs log tree commit diff stats
path: root/classes/AuthMiddleware.php
diff options
context:
space:
mode:
authorAndrew Dolgov <fox@fakecake.org>2026-05-24 14:16:07 +0300
committerAndrew Dolgov <fox@fakecake.org>2026-05-24 14:16:07 +0300
commit1bdf80397c19362c019cdd231e8813e251764370 (patch)
tree126c1e0ffcc5664ac2b5883bec64007070411a9b /classes/AuthMiddleware.php
parent1917ab77550613654620b6f5d4ad8570704246de (diff)
downloadthe-epube-1bdf80397c19362c019cdd231e8813e251764370.tar.gz
the-epube-1bdf80397c19362c019cdd231e8813e251764370.tar.xz
switch to PSR-4 router with namespaced handlers
Diffstat (limited to 'classes/AuthMiddleware.php')
-rw-r--r--classes/AuthMiddleware.php29
1 files changed, 29 insertions, 0 deletions
diff --git a/classes/AuthMiddleware.php b/classes/AuthMiddleware.php
new file mode 100644
index 0000000..4f1f11d
--- /dev/null
+++ b/classes/AuthMiddleware.php
@@ -0,0 +1,29 @@
+<?php
+
+class AuthMiddleware implements MiddlewareInterface
+{
+    private array $csrfIgnore = [
+        "cover", "download", "getpagination", "getlastread"
+    ];
+
+    public function process(Request $request, callable $next): Response
+    {
+        if (empty($_SESSION['owner'])) {
+            return $next($request);
+        }
+
+        $op = $request->getAttribute('op');
+
+        $csrfToken = $request->getAttribute('csrf_token', '');
+
+        if (!in_array($op, $this->csrfIgnore) && !validate_csrf($csrfToken)) {
+            $response = (new Response())
+                ->withStatus(401)
+                ->withBody("Unauthorized (CSRF)");
+            $response->send();
+            exit;
+        }
+
+        return $next($request);
+    }
+}