Passed
Push — devel-3.0 ( 218a48...a00b1f )
by Rubén
03:54
created

SecureKeyCookie::getSecuredKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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\Core\Crypt;
26
27
use Defuse\Crypto\Exception\CryptoException;
28
use Defuse\Crypto\Key;
29
use SP\Http\Request;
30
31
/**
32
 * Class SecureKeyCookie
33
 *
34
 * @package SP\Core\Crypt
35
 */
36
final class SecureKeyCookie extends Cookie
37
{
38
    /**
39
     * Nombre de la cookie
40
     */
41
    const COOKIE_NAME = 'SYSPASS_SK';
42
    /**
43
     * Llave usada para encriptar los datos
44
     *
45
     * @var Key
46
     */
47
    private $securedKey;
48
    /**
49
     * @var string
50
     */
51
    private $cypher;
52
53
    /**
54
     * @param Request $request
55
     *
56
     * @return SecureKeyCookie
57
     */
58
    public static function factory(Request $request)
59
    {
60
        $self = new self(self::COOKIE_NAME, $request);
61
        $self->cypher = $self->getCypher();
62
63
        return $self;
64
    }
65
66
    /**
67
     * Devolver la llave de cifrado para los datos de la cookie
68
     *
69
     * @return string
70
     */
71
    public function getCypher()
72
    {
73
        return sha1($this->request->getHeader('User-Agent') . $this->request->getClientAddress());
74
    }
75
76
    /**
77
     * Obtener una llave de encriptación
78
     *
79
     * @return Key|false|string
80
     */
81
    public function getKey()
82
    {
83
        $cookie = $this->getCookie();
84
85
        if ($cookie !== false) {
86
            $data = $this->getCookieData($cookie, $this->cypher);
0 ignored issues
show
Bug introduced by
It seems like $cookie can also be of type true; however, parameter $data of SP\Core\Crypt\Cookie::getCookieData() 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

86
            $data = $this->getCookieData(/** @scrutinizer ignore-type */ $cookie, $this->cypher);
Loading history...
87
88
            if ($data !== false) {
89
                /** @var Vault $vault */
90
                $vault = unserialize($data, ['allowed_classes' => Vault::class]);
91
92
                if ($vault !== false
93
                    && ($vault instanceof Vault) === true
94
                ) {
95
                    try {
96
                        $this->securedKey = Key::loadFromAsciiSafeString($vault->getData($this->cypher));
97
98
                        return $this->securedKey;
99
                    } catch (CryptoException $e) {
100
                        logger($e->getMessage(), 'EXCEPTION');
101
                    }
102
103
                    return false;
104
                }
105
            } else {
106
                logger('Cookie verification error', 'ERROR');
107
            }
108
        } elseif (($this->securedKey instanceof Key) === true) {
109
            return $this->securedKey;
110
        }
111
112
        return $this->saveKey() ? $this->securedKey : false;
113
    }
114
115
    /**
116
     * Guardar una llave de encriptación
117
     *
118
     * @return Key|false
119
     */
120
    public function saveKey()
121
    {
122
        try {
123
            if ($this->setCookie($this->sign($this->generateSecuredData()->getSerialized(), $this->cypher)) === false) {
124
                logger('Could not generate session\'s key cookie', 'ERROR');
125
126
                unset($this->securedKey);
127
128
                return false;
129
            }
130
131
            logger('Generating a new session\'s key cookie');
132
133
            return true;
0 ignored issues
show
Bug Best Practice introduced by
The expression return true returns the type true which is incompatible with the documented return type false|Defuse\Crypto\Key.
Loading history...
134
        } catch (CryptoException $e) {
135
            logger($e->getMessage(), 'EXCEPTION');
136
        }
137
138
        return false;
139
    }
140
141
    /**
142
     * @return Vault
143
     * @throws CryptoException
144
     * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException
145
     */
146
    public function generateSecuredData()
147
    {
148
        $this->securedKey = Key::createNewRandomKey();
149
150
        return (new Vault())
151
            ->saveData($this->securedKey->saveToAsciiSafeString(), $this->cypher);
152
    }
153
154
    /**
155
     * @return Key
156
     */
157
    public function getSecuredKey()
158
    {
159
        return $this->securedKey;
160
    }
161
}