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

LogoutController::logout()   B

Complexity

Conditions 8
Paths 48

Size

Total Lines 40
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 40
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\Configuration;
9
use SimpleSAML\Logger;
10
use SimpleSAML\Module;
11
use SimpleSAML\Module\casserver\Cas\Factories\TicketFactory;
12
use SimpleSAML\Module\casserver\Controller\Traits\UrlTrait;
13
use SimpleSAML\Session;
14
use Symfony\Component\HttpFoundation\RedirectResponse;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpKernel\Attribute\AsController;
17
use Symfony\Component\HttpKernel\Attribute\MapQueryParameter;
18
19
#[AsController]
20
class LogoutController
21
{
22
    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...
23
24
    /** @var Logger */
25
    protected Logger $logger;
26
27
    /** @var Configuration */
28
    protected Configuration $casConfig;
29
30
    /** @var TicketFactory */
31
    protected TicketFactory $ticketFactory;
32
33
    /** @var Simple  */
34
    protected Simple $authSource;
35
36
    // this could be any configured ticket store
37
    /** @var mixed */
38
    protected mixed $ticketStore;
39
40
    /**
41
     * Controller constructor.
42
     *
43
     * It initializes the global configuration for the controllers implemented here.
44
     *
45
     */
46
    public function __construct()
47
    {
48
        $this->casConfig = Configuration::getConfig('module_casserver.php');
49
        /* Instantiate ticket factory */
50
        $this->ticketFactory = new TicketFactory($this->casConfig);
51
        /* Instantiate ticket store */
52
        $ticketStoreConfig = $this->casConfig->getOptionalValue(
53
            'ticketstore',
54
            ['class' => 'casserver:FileSystemTicketStore'],
55
        );
56
        $ticketStoreClass = 'SimpleSAML\\Module\\casserver\\Cas\\Ticket\\'
57
            . explode(':', $ticketStoreConfig['class'])[1];
58
        $this->ticketStore = new $ticketStoreClass($this->casConfig);
59
        $this->authSource = new Simple($this->casConfig->getValue('authsource'));
60
    }
61
62
    /**
63
     *
64
     * @param   Request      $request
65
     * @param   string|null  $url
66
     *
67
     * @return RedirectResponse|null
68
     */
69
    public function logout(
70
        Request $request,
71
        #[MapQueryParameter] ?string $url = null,
72
    ): RedirectResponse|null {
73
        if (!$this->casConfig->getOptionalValue('enable_logout', false)) {
74
            $this->handleExceptionThrown('Logout not allowed');
75
        }
76
77
        // Skip Logout Page configuration
78
        $skipLogoutPage = $this->casConfig->getOptionalValue('skip_logout_page', false);
79
80
        if ($skipLogoutPage && $url === null) {
81
            $this->handleExceptionThrown('Required URL query parameter [url] not provided. (CAS Server)');
82
        }
83
84
        // Construct the logout redirect url
85
        if ($skipLogoutPage) {
86
            $logoutRedirectUrl = $url;
87
        } else {
88
            $loggedOutUrl = Module::getModuleURL('casserver/loggedOut.php');
89
            $logoutRedirectUrl =  $url === null ? $loggedOutUrl
90
                : $loggedOutUrl . '?' . http_build_query(['url' => $url]);
91
        }
92
93
        // Delete the ticket from the session
94
        $session = $this->getSession();
95
        if ($session !== null) {
96
            $this->ticketStore->deleteTicket($session->getSessionId());
97
        }
98
99
        // Redirect
100
        if (!$this->authSource->isAuthenticated()) {
101
            $this->redirect($logoutRedirectUrl);
0 ignored issues
show
Bug introduced by
The method redirect() does not exist on SimpleSAML\Module\casser...roller\LogoutController. ( Ignorable by Annotation )

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

101
            $this->/** @scrutinizer ignore-call */ 
102
                   redirect($logoutRedirectUrl);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
102
        }
103
104
        // Logout and redirect
105
        $this->authSource->logout($logoutRedirectUrl);
106
107
        // We should never get here
108
        return null;
109
    }
110
111
    /**
112
     * @param   string  $message
113
     *
114
     * @return void
115
     */
116
    protected function handleExceptionThrown(string $message): void
117
    {
118
        Logger::debug('casserver:' . $message);
119
        throw new \RuntimeException($message);
120
    }
121
122
    /**
123
     * Get the Session
124
     *
125
     * @return Session|null
126
     */
127
    protected function getSession(): ?Session
128
    {
129
        return Session::getSession();
130
    }
131
}
132