aboutsummaryrefslogtreecommitdiff
path: root/lib/middleware/AuthenticationMiddleware.php
blob: 95d29ac34a39fd948d9ecccee9fd5d522faa03a8 (plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
/**
 * PSR 15 middleware Stud.IP Authentication
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * @author      André Noack <noack@data-quest.de>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 * @since       6.0
 */
namespace Studip\Middleware;

use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Studip\Authentication\Manager;

final class AuthenticationMiddleware implements MiddlewareInterface
{
    public function __construct(
        private readonly Manager $auth_manager,
        private readonly ResponseFactoryInterface $response_factory
    ) {
    }

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        if ($this->auth_manager->start()) {
            return $handler->handle($request);
        } else {
            if (!match_route('dispatch.php/start')) {
                $_SESSION['redirect_after_login'] ??= \Request::url();
            } else {
                unset($_SESSION['redirect_after_login']);
            }
            return $this->response_factory->createResponse(302)
                ->withHeader('Location', \URLHelper::getURL('dispatch.php/login'));
        }
    }
}