Passed
Push — master ( 221005...26261c )
by Rubén
03:12
created

ControllerTrait::sessionLogout()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 21
nc 13
nop 3
dl 0
loc 32
rs 8.9617
c 0
b 0
f 0
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Mvc\Controller;
26
27
use SP\Bootstrap;
28
use SP\Config\ConfigData;
29
use SP\Core\Exceptions\SPException;
30
use SP\Http\Json;
31
use SP\Http\JsonResponse;
32
use SP\Http\Request;
33
use SP\Http\Uri;
34
use SP\Util\Util;
35
36
37
/**
38
 * Trait ControllerTrait
39
 *
40
 * @package SP\Mvc\Controller
41
 * @property ConfigData $configData
42
 */
43
trait ControllerTrait
44
{
45
    /**
46
     * @return string
47
     */
48
    protected function getControllerName()
49
    {
50
        $class = static::class;
51
52
        return substr($class, strrpos($class, '\\') + 1, -strlen('Controller')) ?: '';
53
    }
54
55
    /**
56
     * Logout from current session
57
     *
58
     * @param Request    $request
59
     * @param ConfigData $configData
60
     * @param \Closure   $onRedirect
61
     */
62
    protected function sessionLogout(Request $request, ConfigData $configData, \Closure $onRedirect)
63
    {
64
        if ($request->isJson()) {
65
            $jsonResponse = new JsonResponse(__u('Session not started or timed out'));
66
            $jsonResponse->setStatus(10);
67
68
            Json::fromDic()->returnJson($jsonResponse);
69
        } elseif ($request->isAjax()) {
70
            Util::logout();
71
        } else {
72
            try {
73
                // Analyzes if there is any direct route within the URL
74
                // then it computes the route HMAC to build a signed URI
75
                // which would be used during logging in
76
                $route = $request->analyzeString('r');
77
                $hash = $request->analyzeString('h');
78
79
                $uri = new Uri(Bootstrap::$WEBROOT . Bootstrap::$SUBURI);
80
                $uri->addParam('_r', 'login');
81
82
                if ($route && $hash) {
83
                    $key = $configData->getPasswordSalt();
84
                    $request->verifySignature($key);
85
86
                    $uri->addParam('from', $route);
87
88
                    $onRedirect->call($this, $uri->getUriSigned($key));
89
                } else {
90
                    $onRedirect->call($this, $uri->getUri());
91
                }
92
            } catch (SPException $e) {
93
                processException($e);
94
            }
95
        }
96
    }
97
98
    /**
99
     * @param string  $previousToken
100
     * @param Request $request
101
     *
102
     * @throws SPException
103
     */
104
    protected function checkSecurityToken($previousToken, Request $request)
105
    {
106
        if ($request->analyzeString('h') !== null
107
            && $request->analyzeString('from') === null
108
            && isset($this->configData)
109
        ) {
110
            $request->verifySignature($this->configData->getPasswordSalt());
111
        } else {
112
            $sk = $request->analyzeString('sk');
113
114
            if (!$sk || $previousToken !== $sk) {
115
                throw new SPException(
116
                    __u('Invalid Action'),
117
                    SPException::ERROR,
118
                    null,
119
                    1
120
                );
121
            }
122
        }
123
    }
124
125
    /**
126
     * Acción no disponible
127
     */
128
    protected function invalidAction()
129
    {
130
        Json::fromDic()->returnJson(new JsonResponse(__u('Invalid Action')));
131
    }
132
}