Skip to content

Commit d30d7ef

Browse files
authored
Use safe methods for OAuth\Server
1 parent 4287534 commit d30d7ef

File tree

2 files changed

+20
-30
lines changed

2 files changed

+20
-30
lines changed

.phpstan-baseline.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5941,24 +5941,6 @@
59415941
'count' => 2,
59425942
'path' => __DIR__ . '/src/Glpi/OAuth/Server.php',
59435943
];
5944-
$ignoreErrors[] = [
5945-
'message' => '#^Function file_put_contents is unsafe to use\\. It can return FALSE instead of throwing an exception\\. Please add \'use function Safe\\\\file_put_contents;\' at the beginning of the file to use the variant provided by the \'thecodingmachine/safe\' library\\.$#',
5946-
'identifier' => 'theCodingMachineSafe.function',
5947-
'count' => 1,
5948-
'path' => __DIR__ . '/src/Glpi/OAuth/Server.php',
5949-
];
5950-
$ignoreErrors[] = [
5951-
'message' => '#^Function openssl_pkey_export_to_file is unsafe to use\\. It can return FALSE instead of throwing an exception\\. Please add \'use function Safe\\\\openssl_pkey_export_to_file;\' at the beginning of the file to use the variant provided by the \'thecodingmachine/safe\' library\\.$#',
5952-
'identifier' => 'theCodingMachineSafe.function',
5953-
'count' => 1,
5954-
'path' => __DIR__ . '/src/Glpi/OAuth/Server.php',
5955-
];
5956-
$ignoreErrors[] = [
5957-
'message' => '#^Function openssl_pkey_new is unsafe to use\\. It can return FALSE instead of throwing an exception\\. Please add \'use function Safe\\\\openssl_pkey_new;\' at the beginning of the file to use the variant provided by the \'thecodingmachine/safe\' library\\.$#',
5958-
'identifier' => 'theCodingMachineSafe.function',
5959-
'count' => 1,
5960-
'path' => __DIR__ . '/src/Glpi/OAuth/Server.php',
5961-
];
59625944
$ignoreErrors[] = [
59635945
'message' => '#^Function unlink is unsafe to use\\. It can return FALSE instead of throwing an exception\\. Please add \'use function Safe\\\\unlink;\' at the beginning of the file to use the variant provided by the \'thecodingmachine/safe\' library\\.$#',
59645946
'identifier' => 'theCodingMachineSafe.function',

src/Glpi/OAuth/Server.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,14 @@
4343
use League\OAuth2\Server\Grant\RefreshTokenGrant;
4444
use League\OAuth2\Server\ResourceServer;
4545
use RuntimeException;
46+
use Safe\Exceptions\FilesystemException;
47+
use Safe\Exceptions\OpensslException;
4648
use Throwable;
4749

50+
use function Safe\file_put_contents;
51+
use function Safe\openssl_pkey_export_to_file;
52+
use function Safe\openssl_pkey_new;
53+
4854
final class Server
4955
{
5056
private const PRIVATE_KEY_PATH = GLPI_CONFIG_DIR . '/oauth.pem';
@@ -226,16 +232,17 @@ private static function doGenerateKeys(): void
226232
];
227233

228234
// Generate key
229-
$key = openssl_pkey_new($config);
230-
if ($key === false) {
231-
$error = openssl_error_string();
232-
throw new RuntimeException("Unable to generate keys: $error");
235+
try {
236+
$key = openssl_pkey_new($config);
237+
} catch (OpensslException $e) {
238+
throw new RuntimeException("Unable to generate keys: " . $e->getMessage());
233239
}
234240

235241
// Export private key to file
236-
if (!openssl_pkey_export_to_file($key, self::PRIVATE_KEY_PATH)) {
237-
$error = openssl_error_string();
238-
throw new RuntimeException("Unable to export private key: $error");
242+
try {
243+
openssl_pkey_export_to_file($key, self::PRIVATE_KEY_PATH);
244+
} catch (OpensslException $e) {
245+
throw new RuntimeException("Unable to export private key: " . $e->getMessage());
239246
}
240247

241248
// Get public key
@@ -246,11 +253,12 @@ private static function doGenerateKeys(): void
246253
}
247254

248255
// Export public key to file
249-
$written_bytes = file_put_contents(self::PUBLIC_KEY_PATH, $pubkey['key']);
250-
if (
251-
$written_bytes === false
252-
|| $written_bytes !== strlen($pubkey['key'])
253-
) {
256+
try {
257+
$written_bytes = file_put_contents(self::PUBLIC_KEY_PATH, $pubkey['key']);
258+
} catch (FilesystemException $e) {
259+
throw new RuntimeException("Unable to export public key: " . $e->getMessage());
260+
}
261+
if ($written_bytes !== strlen($pubkey['key'])) {
254262
throw new RuntimeException('Unable to export public key');
255263
}
256264

0 commit comments

Comments
 (0)