1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Invoiced\OAuth1\Client\Server; |
4
|
|
|
|
5
|
|
|
use League\OAuth1\Client\Credentials\CredentialsException; |
6
|
|
|
use League\OAuth1\Client\Credentials\ClientCredentials; |
7
|
|
|
|
8
|
|
|
class RsaClientCredentials extends ClientCredentials |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
protected $rsaPublicKeyFile; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $rsaPrivateKeyFile; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var resource|null |
22
|
|
|
*/ |
23
|
|
|
protected $rsaPublicKey; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var resource|null |
27
|
|
|
*/ |
28
|
|
|
protected $rsaPrivateKey; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Sets the path to the RSA public key. |
32
|
|
|
* |
33
|
|
|
* @param string $filename |
34
|
|
|
* |
35
|
|
|
* @return self |
36
|
|
|
*/ |
37
|
|
|
public function setRsaPublicKey($filename) |
38
|
|
|
{ |
39
|
|
|
$this->rsaPublicKeyFile = $filename; |
40
|
|
|
$this->rsaPublicKey = null; |
41
|
|
|
|
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Sets the path to the RSA private key. |
47
|
|
|
* |
48
|
|
|
* @param string $filename |
49
|
|
|
* |
50
|
|
|
* @return self |
51
|
|
|
*/ |
52
|
|
|
public function setRsaPrivateKey($filename) |
53
|
|
|
{ |
54
|
|
|
$this->rsaPrivateKeyFile = $filename; |
55
|
|
|
$this->rsaPrivateKey = null; |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Gets the RSA public key. |
62
|
|
|
* |
63
|
|
|
* @throws CredentialsException when the key could not be loaded. |
64
|
|
|
* |
65
|
|
|
* @return resource |
66
|
|
|
*/ |
67
|
|
View Code Duplication |
public function getRsaPublicKey() |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
if ($this->rsaPublicKey) { |
70
|
|
|
return $this->rsaPublicKey; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (!file_exists($this->rsaPublicKeyFile)) { |
74
|
|
|
throw new CredentialsException('Could not read the public key file.'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->rsaPublicKey = openssl_get_publickey(file_get_contents($this->rsaPublicKeyFile)); |
78
|
|
|
|
79
|
|
|
if (!$this->rsaPublicKey) { |
80
|
|
|
throw new CredentialsException('Cannot access public key for signing'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this->rsaPublicKey; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Gets the RSA private key. |
88
|
|
|
* |
89
|
|
|
* @throws CredentialsException when the key could not be loaded. |
90
|
|
|
* |
91
|
|
|
* @return resource |
92
|
|
|
*/ |
93
|
|
View Code Duplication |
public function getRsaPrivateKey() |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
if ($this->rsaPrivateKey) { |
96
|
|
|
return $this->rsaPrivateKey; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if (!file_exists($this->rsaPrivateKeyFile)) { |
100
|
|
|
throw new CredentialsException('Could not read the private key file.'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$this->rsaPrivateKey = openssl_pkey_get_private(file_get_contents($this->rsaPrivateKeyFile)); |
104
|
|
|
|
105
|
|
|
if (!$this->rsaPrivateKey) { |
106
|
|
|
throw new CredentialsException('Cannot access private key for signing'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $this->rsaPrivateKey; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function __destruct() |
113
|
|
|
{ |
114
|
|
|
if ($this->rsaPublicKey) { |
115
|
|
|
openssl_free_key($this->rsaPublicKey); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if ($this->rsaPrivateKey) { |
119
|
|
|
openssl_free_key($this->rsaPrivateKey); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.