Passed
Pull Request — master (#45)
by
unknown
15:40
created

LogoutController::logout()   B

Complexity

Conditions 8
Paths 48

Size

Total Lines 41
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 41
rs 8.4444
cc 8
nc 48
nop 2
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
        $this->authSource = $source ?? new Simple($this->casConfig->getValue('authsource'));
60
        $this->container = $container ?? new SspContainer();
61
62
        /* Instantiate ticket factory */
63
        $this->ticketFactory = new TicketFactory($this->casConfig);
64
        /* Instantiate ticket store */
65
        $ticketStoreConfig = $this->casConfig->getOptionalValue(
66
            'ticketstore',
67
            ['class' => 'casserver:FileSystemTicketStore'],
68
        );
69
        $ticketStoreClass = 'SimpleSAML\\Module\\casserver\\Cas\\Ticket\\'
70
            . explode(':', $ticketStoreConfig['class'])[1];
71
        $this->ticketStore = new $ticketStoreClass($this->casConfig);
72
    }
73
74
    /**
75
     *
76
     * @param   Request      $request
77
     * @param   string|null  $url
78
     *
79
     * @return RedirectResponse|null
80
     */
81
    public function logout(
82
        Request $request,
83
        #[MapQueryParameter] ?string $url = null,
84
    ): RedirectResponse|null {
85
        if (!$this->casConfig->getOptionalValue('enable_logout', false)) {
86
            $this->handleExceptionThrown('Logout not allowed');
87
        }
88
89
        // Skip Logout Page configuration
90
        $skipLogoutPage = $this->casConfig->getOptionalValue('skip_logout_page', false);
91
92
        if ($skipLogoutPage && $url === null) {
93
            $this->handleExceptionThrown('Required URL query parameter [url] not provided. (CAS Server)');
94
        }
95
96
        // Construct the logout redirect url
97
        if ($skipLogoutPage) {
98
            $logoutRedirectUrl = $url;
99
            $params = [];
100
        } else {
101
            $logoutRedirectUrl = Module::getModuleURL('casserver/loggedOut.php');
102
            $params =  $url === null ? []
103
                : ['url' => $url];
104
        }
105
106
        // Delete the ticket from the session
107
        $session = $this->getSession();
108
        if ($session !== null) {
109
            $this->ticketStore->deleteTicket($session->getSessionId());
110
        }
111
112
        // Redirect
113
        if (!$this->authSource->isAuthenticated()) {
114
            $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

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