Issues (66)

src/CryptTrait.php (1 issue)

1
<?php
2
3
/**
4
 * Encrypt/decrypt with encryptionKey.
5
 *
6
 * @author      Alex Bilbie <[email protected]>
7
 * @copyright   Copyright (c) Alex Bilbie
8
 * @license     http://mit-license.org/
9
 *
10
 * @link        https://github.com/thephpleague/oauth2-server
11
 */
12
13
declare(strict_types=1);
14
15
namespace League\OAuth2\Server;
16
17
use Defuse\Crypto\Crypto;
18
use Defuse\Crypto\Exception\EnvironmentIsBrokenException;
19
use Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException;
20
use Defuse\Crypto\Key;
21
use Exception;
22
use InvalidArgumentException;
23
use LogicException;
24
use SensitiveParameter;
0 ignored issues
show
The type SensitiveParameter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
26
use function is_string;
27
28
trait CryptTrait
29
{
30
    protected string|Key|null $encryptionKey = null;
31
32
    /**
33
     * Encrypt data with encryptionKey.
34
     *
35
     * @throws LogicException
36
     */
37 45
    protected function encrypt(string $unencryptedData): string
38
    {
39
        try {
40 45
            if ($this->encryptionKey instanceof Key) {
41 1
                return Crypto::encrypt($unencryptedData, $this->encryptionKey);
42
            }
43
44 44
            if (is_string($this->encryptionKey)) {
45 44
                return Crypto::encryptWithPassword($unencryptedData, $this->encryptionKey);
46
            }
47
48
            throw new LogicException('Encryption key not set when attempting to encrypt');
49
        } catch (Exception $e) {
50
            throw new LogicException($e->getMessage(), 0, $e);
51
        }
52
    }
53
54
    /**
55
     * Decrypt data with encryptionKey.
56
     *
57
     * @throws LogicException
58
     */
59 35
    protected function decrypt(string $encryptedData): string
60
    {
61
        try {
62 35
            if ($this->encryptionKey instanceof Key) {
63 1
                return Crypto::decrypt($encryptedData, $this->encryptionKey);
64
            }
65
66 34
            if (is_string($this->encryptionKey)) {
67 33
                return Crypto::decryptWithPassword($encryptedData, $this->encryptionKey);
68
            }
69
70 1
            throw new LogicException('Encryption key not set when attempting to decrypt');
71 3
        } catch (WrongKeyOrModifiedCiphertextException $e) {
72 2
            $exceptionMessage = 'The authcode or decryption key/password used '
73 2
                . 'is not correct';
74
75 2
            throw new InvalidArgumentException($exceptionMessage, 0, $e);
76 1
        } catch (EnvironmentIsBrokenException $e) {
77
            $exceptionMessage = 'Auth code decryption failed. This is likely '
78
                . 'due to an environment issue or runtime bug in the '
79
                . 'decryption library';
80
81
            throw new LogicException($exceptionMessage, 0, $e);
82 1
        } catch (Exception $e) {
83 1
            throw new LogicException($e->getMessage(), 0, $e);
84
        }
85
    }
86
87 104
    public function setEncryptionKey(
88
        #[SensitiveParameter]
89
        Key|string|null $key = null
90
    ): void {
91 104
        $this->encryptionKey = $key;
92
    }
93
}
94