<commit_before><?php

/*
 * eduVPN - End-user friendly VPN.
 *
 * Copyright: 2016-2018, The Commons Conservancy eduVPN Programme
 * SPDX-License-Identifier: AGPL-3.0+
 */

namespace SURFnet\VPN\Common\Http;

use fkooman\SeCookie\SessionInterface;

class LogoutModule implements ServiceModuleInterface
{
    /** @var \fkooman\SeCookie\SessionInterface */
    private $session;

    /** @var null|string */
    private $logoutUrl;

    /**
     * @param \fkooman\SeCookie\SessionInterface $session
     * @param null|string                        $logoutUrl
     */
    public function __construct(SessionInterface $session, $logoutUrl = null)
    {
        $this->session = $session;
        $this->logoutUrl = $logoutUrl;
    }

    /**
     * @param \SURFnet\VPN\Common\Http\Service $service
     *
     * @return void
     */
    public function init(Service $service)
    {
        // new URL since we introduce SAML / Mellon logout
        $service->post(
            '/_logout',
            /**
             * @return \SURFnet\VPN\Common\Http\Response
             */
            function (Request $request, array $hookData) {
                $this->session->destroy();
                $httpReferrer = $request->requireHeader('HTTP_REFERER');
                if (null !== $this->logoutUrl) {
                    // a logout URL is defined, this is used by SAML/Mellon
                    return new RedirectResponse(
                        sprintf(
                            '%s?%s',
                            $this->logoutUrl,
                            http_build_query(
                                [
                                    'ReturnTo' => $httpReferrer,
                                ]
                            )
                        )
                    );
                }

                return new RedirectResponse($httpReferrer);
            }
        );
    }
}<commit_msg>do not prematurely destroy the session<commit_after><?php

/*
 * eduVPN - End-user friendly VPN.
 *
 * Copyright: 2016-2018, The Commons Conservancy eduVPN Programme
 * SPDX-License-Identifier: AGPL-3.0+
 */

namespace SURFnet\VPN\Common\Http;

use fkooman\SeCookie\SessionInterface;

class LogoutModule implements ServiceModuleInterface
{
    /** @var \fkooman\SeCookie\SessionInterface */
    private $session;

    /** @var null|string */
    private $logoutUrl;

    /**
     * @param \fkooman\SeCookie\SessionInterface $session
     * @param null|string                        $logoutUrl
     */
    public function __construct(SessionInterface $session, $logoutUrl = null)
    {
        $this->session = $session;
        $this->logoutUrl = $logoutUrl;
    }

    /**
     * @param \SURFnet\VPN\Common\Http\Service $service
     *
     * @return void
     */
    public function init(Service $service)
    {
        // new URL since we introduce SAML / Mellon logout
        $service->post(
            '/_logout',
            /**
             * @return \SURFnet\VPN\Common\Http\Response
             */
            function (Request $request, array $hookData) {
                $httpReferrer = $request->requireHeader('HTTP_REFERER');
                if (null !== $this->logoutUrl) {
                    // a logout URL is defined, this is used by SAML/Mellon
                    return new RedirectResponse(
                        sprintf(
                            '%s?%s',
                            $this->logoutUrl,
                            http_build_query(
                                [
                                    'ReturnTo' => $httpReferrer,
                                ]
                            )
                        )
                    );
                }

                // we only destroy the session here, and not for the
                // "SAML backends" as they may store stuff in the session as
                // well, defer to those authtication mechanisms...
                $this->session->destroy();

                return new RedirectResponse($httpReferrer);
            }
        );
    }
}
{'fraction_non_alphanumeric': 0.10277164581800344, 'fraction_numerical': 0.0049055678194751045, 'mean_word_length': 1.3091732729331824, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1174625', 'n_tokens_mistral': 1129, 'n_tokens_neox': 1064, 'n_words': 232}