about summary refs log tree commit diff stats
path: root/classes/AuthMiddleware.php
blob: 4f1f11d1b79cd55582a2702f1608ca2130390799 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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);
    }
}