Passed
Pull Request — master (#45)
by
unknown
13:46
created

LogoutController::getSession()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
nc 1
nop 0
cc 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\casserver\Controller;
6
7
use SimpleSAML\Auth\Simple;
8
use SimpleSAML\Compat\SspContainer;
9
use SimpleSAML\Configuration;
10
use SimpleSAML\Logger;
11
use SimpleSAML\Module;
12
use SimpleSAML\Module\casserver\Cas\Factories\TicketFactory;
13
use SimpleSAML\Module\casserver\Controller\Traits\UrlTrait;
14
use SimpleSAML\Session;
15
use Symfony\Component\HttpFoundation\RedirectResponse;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpKernel\Attribute\AsController;
18
use Symfony\Component\HttpKernel\Attribute\MapQueryParameter;
19
20
#[AsController]
21
class LogoutController
22
{
23
    use UrlTrait;
1 ignored issue
show
introduced by
The trait SimpleSAML\Module\casser...troller\Traits\UrlTrait requires some properties which are not provided by SimpleSAML\Module\casser...roller\LogoutController: $query, $request
Loading history...
24
25
    /** @var Logger */
26
    protected Logger $logger;
27
28
    /** @var Configuration */
29
    protected Configuration $casConfig;
30
31
    /** @var TicketFactory */
32
    protected TicketFactory $ticketFactory;
33
34
    /** @var Simple  */
35
    protected Simple $authSource;
36
37
    /** @var SspContainer */
38
    protected SspContainer $container;
39
40
    // this could be any configured ticket store
41
    /** @var mixed */
42
    protected mixed $ticketStore;
43
44
45
    /**
46
     * @param   Configuration|null  $casConfig
47
     * @param   Simple|null         $source
48
     * @param   SspContainer|null   $container
49
     *
50
     * @throws \Exception
51
     */
52
    public function __construct(
53
        // Facilitate testing
54
        Configuration $casConfig = null,
55
        Simple $source = null,
56
        SspContainer $container = null,
57
    ) {
58
        $this->casConfig = $casConfig ?? Configuration::getConfig('module_casserver.php');
59
        /* Instantiate ticket factory */
60
        $this->ticketFactory = new TicketFactory($this->casConfig);
61
        /* Instantiate ticket store */
62
        $ticketStoreConfig = $this->casConfig->getOptionalValue(
63
            'ticketstore',
64
            ['class' => 'casserver:FileSystemTicketStore'],
65
        );
66
        $ticketStoreClass = 'SimpleSAML\\Module\\casserver\\Cas\\Ticket\\'
67
            . explode(':', $ticketStoreConfig['class'])[1];
68
        $this->ticketStore = new $ticketStoreClass($this->casConfig);
69
        $this->authSource = $source ?? new Simple($this->casConfig->getValue('authsource'));
70
        $this->container = $container ?? new SspContainer();
71
    }
72
73
    /**
74
     *
75
     * @param   Request      $request
76
     * @param   string|null  $url
77
     *
78
     * @return RedirectResponse|null
79
     */
80
    public function logout(
81
        Request $request,
82
        #[MapQueryParameter] ?string $url = null,
83
    ): RedirectResponse|null {
84
        if (!$this->casConfig->getOptionalValue('enable_logout', false)) {
85
            $this->handleExceptionThrown('Logout not allowed');
86
        }
87
88
        // Skip Logout Page configuration
89
        $skipLogoutPage = $this->casConfig->getOptionalValue('skip_logout_page', false);
90
91
        if ($skipLogoutPage && $url === null) {
92
            $this->handleExceptionThrown('Required URL query parameter [url] not provided. (CAS Server)');
93
        }
94
95
        // Construct the logout redirect url
96
        if ($skipLogoutPage) {
97
            $logoutRedirectUrl = $url;
98
            $params = [];
99
        } else {
100
            $logoutRedirectUrl = Module::getModuleURL('casserver/loggedOut.php');
101
            $params =  $url === null ? []
102
                : ['url' => $url];
103
        }
104
105
        // Delete the ticket from the session
106
        $session = $this->getSession();
107
        if ($session !== null) {
108
            $this->ticketStore->deleteTicket($session->getSessionId());
109
        }
110
111
        // Redirect
112
        if (!$this->authSource->isAuthenticated()) {
113
            $this->container->redirect($logoutRedirectUrl, $params);
0 ignored issues
show
Bug introduced by
It seems like $logoutRedirectUrl can also be of type null; however, parameter $url of SimpleSAML\Compat\SspContainer::redirect() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

113
            $this->container->redirect(/** @scrutinizer ignore-type */ $logoutRedirectUrl, $params);
Loading history...
114
        }
115
116
        // Logout and redirect
117
        $this->authSource->logout($logoutRedirectUrl);
118
119
        // We should never get here
120
        return null;
121
    }
122
123
    /**
124
     * @param   string  $message
125
     *
126
     * @return void
127
     */
128
    protected function handleExceptionThrown(string $message): void
129
    {
130
        Logger::debug('casserver:' . $message);
131
        throw new \RuntimeException($message);
132
    }
133
134
    /**
135
     * Get the Session
136
     *
137
     * @return Session|null
138
     */
139
    protected function getSession(): ?Session
140
    {
141
        return Session::getSession();
142
    }
143
}
144