forked from pkp/pkp-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PKPAppKey.php
183 lines (156 loc) · 4.66 KB
/
PKPAppKey.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
* @file classes/core/PKPAppKey.php
*
* Copyright (c) 2014-2024 Simon Fraser University
* Copyright (c) 2000-2024 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class PKPAppKey
*
* @ingroup core
*
* @brief Class to manage app key related behaviours
*/
namespace PKP\core;
use Exception;
use PKP\config\Config;
use Illuminate\Support\Str;
use PKP\config\ConfigParser;
use Illuminate\Encryption\Encrypter;
class PKPAppKey
{
/**
* The supported cipher algorithms and their properties.
*
* @var array
*/
private static $supportedCiphers = [
'aes-128-cbc' => ['size' => 16, 'aead' => false],
'aes-256-cbc' => ['size' => 32, 'aead' => false],
'aes-128-gcm' => ['size' => 16, 'aead' => true],
'aes-256-gcm' => ['size' => 32, 'aead' => true],
];
/**
* The default cipher algorithms
*
* @var string
*/
private static $defaultCipher = 'aes-256-cbc';
/**
* Get the list of supported ciphers
*/
public static function getSupportedCiphers(): array
{
return self::$supportedCiphers;
}
/**
* Get the defined cipher
*/
public static function getCipher(): string
{
return Config::getVar('security', 'cipher', self::$defaultCipher);
}
/**
* Has the app key defined in config file
*/
public static function hasKey(): bool
{
return !empty(Config::getVar('general', 'app_key', ''));
}
/**
* Has the app key variable defined in config file
*/
public static function hasKeyVariable(): bool
{
return Config::hasVar('general', 'app_key');
}
/**
* Get the app key defined in config file
*/
public static function getKey(): string
{
return Config::getVar('general', 'app_key', '');
}
/**
* Validate a given cipher
*/
public static function validateCipher(string $cipher): string
{
$cipher = strtolower($cipher);
if (!in_array($cipher, array_keys(static::getSupportedCiphers()))) {
$ciphers = implode(', ', array_keys(static::getSupportedCiphers()));
throw new Exception(
sprintf(
'Invalid cipher %s provided, must be among [%s]',
$cipher,
$ciphers
)
);
}
return $cipher;
}
/**
* Validate given or config defined app
*/
public static function validate(string $key = null, string $cipher = null): bool
{
$config = app('config')->get('app');
return Encrypter::supported(
static::parseKey($key ?? $config['key']),
static::validateCipher($cipher ?? $config['cipher'])
);
}
/**
* Generate a new app key
*/
public static function generate(string $cipher = null): string
{
$config = app('config')->get('app');
return 'base64:'.base64_encode(
Encrypter::generateKey(static::validateCipher($cipher ?? $config['cipher']))
);
}
/**
* Write the given app key in the config file
*/
public static function writeToConfig(string $key): bool
{
if (!static::validate($key)) {
$ciphers = implode(', ', array_keys(static::getSupportedCiphers()));
// Error invalid app key
throw new Exception(
"Unsupported cipher or incorrect key length. Supported ciphers are: {$ciphers}."
);
}
$configParser = new ConfigParser;
$configParams = [
'general' => [
'app_key' => $key,
],
];
if (!static::hasKeyVariable()) {
// Error if the config key `app_key` not defined under `general` section
throw new Exception('Config variable named `app_key` not defined in the `general` section');
}
if (!$configParser->updateConfig(Config::getConfigFileName(), $configParams)) {
// Error reading config file
throw new Exception('Unable to read the config file');
}
if (!$configParser->writeConfig(Config::getConfigFileName())) {
// Error writing config file
throw new Exception('Unable to write the app key in the config file');
}
return true;
}
/**
* Parse the given app key and return the real key value
*/
public static function parseKey(string $key): string
{
if (Str::startsWith($key, $prefix = 'base64:')) {
$key = base64_decode(Str::after($key, $prefix));
}
return $key;
}
}