CryptTrait::decrypt()   A
last analyzed

Complexity

Conditions 6
Paths 14

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6.4689

Importance

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