1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Encrypt/decrypt with encryptionKey. |
4
|
|
|
* |
5
|
|
|
* @author Alex Bilbie <[email protected]> |
6
|
|
|
* @copyright Copyright (c) Alex Bilbie |
7
|
|
|
* @license http://mit-license.org/ |
8
|
|
|
* |
9
|
|
|
* @link https://github.com/thephpleague/oauth2-server |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace League\OAuth2\Server; |
13
|
|
|
|
14
|
|
|
use Defuse\Crypto\Crypto; |
15
|
|
|
use Defuse\Crypto\Key; |
16
|
|
|
|
17
|
|
|
trait CryptTrait |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string|Key |
21
|
|
|
*/ |
22
|
|
|
protected $encryptionKey; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Encrypt data with encryptionKey. |
26
|
|
|
* |
27
|
|
|
* @param string $unencryptedData |
28
|
|
|
* |
29
|
|
|
* @throws \LogicException |
30
|
|
|
* |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
34 |
|
protected function encrypt($unencryptedData) |
34
|
|
|
{ |
35
|
|
|
try { |
36
|
34 |
|
if ($this->encryptionKey instanceof Key) { |
|
|
|
|
37
|
1 |
|
return Crypto::encrypt($unencryptedData, $this->encryptionKey); |
38
|
|
|
} |
39
|
|
|
|
40
|
33 |
|
return Crypto::encryptWithPassword($unencryptedData, $this->encryptionKey); |
41
|
|
|
} catch (\Exception $e) { |
42
|
|
|
throw new \LogicException($e->getMessage()); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Decrypt data with encryptionKey. |
48
|
|
|
* |
49
|
|
|
* @param string $encryptedData |
50
|
|
|
* |
51
|
|
|
* @throws \LogicException |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
28 |
|
protected function decrypt($encryptedData) |
56
|
|
|
{ |
57
|
|
|
try { |
58
|
28 |
|
if ($this->encryptionKey instanceof Key) { |
|
|
|
|
59
|
|
|
try { |
60
|
3 |
|
return Crypto::decrypt($encryptedData, $this->encryptionKey); |
61
|
2 |
|
} catch (\Exception $e) { |
62
|
|
|
// This may have been encrypted with encryptWithPassword(), pull |
63
|
|
|
// the data out of the key and use that to decrypt. |
64
|
|
|
// $this->encryptionKey should not be assigned to the raw bytes so |
65
|
|
|
// subsequent calls to decrypt can work with the key object. |
66
|
2 |
|
return Crypto::decryptWithPassword($encryptedData, $this->encryptionKey->getRawBytes()); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
25 |
|
return Crypto::decryptWithPassword($encryptedData, $this->encryptionKey); |
71
|
3 |
|
} catch (\Exception $e) { |
72
|
3 |
|
throw new \LogicException($e->getMessage()); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set the encryption key |
78
|
|
|
* |
79
|
|
|
* @param string|Key $key |
80
|
|
|
*/ |
81
|
84 |
|
public function setEncryptionKey($key = null) |
82
|
|
|
{ |
83
|
84 |
|
$this->encryptionKey = $key; |
84
|
84 |
|
} |
85
|
|
|
} |
86
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.