1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bytesfield\KeyManager\Models; |
4
|
|
|
|
5
|
|
|
use Bytesfield\KeyManager\Traits\WithSearchableEncryptedAttribute; |
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
7
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory; |
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
9
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
10
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
11
|
|
|
|
12
|
|
|
class ApiCredential extends Model |
13
|
|
|
{ |
14
|
|
|
use SoftDeletes; |
15
|
|
|
use HasFactory; |
16
|
|
|
use WithSearchableEncryptedAttribute; |
17
|
|
|
|
18
|
|
|
protected $guarded = []; |
19
|
|
|
|
20
|
|
|
protected $table = 'key_api_credentials'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Statues for api credential. |
24
|
|
|
*/ |
25
|
|
|
public const STATUSES = [ |
26
|
|
|
'ACTIVE' => 'active', |
27
|
|
|
'SUSPENDED' => 'suspended', |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Public key prefix. |
32
|
|
|
*/ |
33
|
|
|
public const PUBLIC_KEY_PREFIX = 'api_key_pub'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Private key prefix. |
37
|
|
|
*/ |
38
|
|
|
public const PRIVATE_KEY_PREFIX = 'api_key_prv'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The hidden columns. |
42
|
|
|
* |
43
|
|
|
* @var string[] |
44
|
|
|
*/ |
45
|
|
|
protected $hidden = [ |
46
|
|
|
'secret_hash', 'deleted_at', |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The encrypted field or column. |
51
|
|
|
* |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
protected $encrypted = 'private_key'; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* The blind index field or column. |
58
|
|
|
* |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
protected $blindIndex = 'secret_hash'; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get the client for the api key. |
65
|
|
|
* |
66
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
67
|
|
|
*/ |
68
|
|
|
public function client(): BelongsTo |
69
|
|
|
{ |
70
|
|
|
return $this->belongsTo(Client::class, 'id'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Sets the value for private key column. |
75
|
|
|
* |
76
|
|
|
* @param string $key |
77
|
|
|
* |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
public function setPrivateKeyAttribute(string $key): void |
81
|
|
|
{ |
82
|
|
|
$this->attributes['private_key'] = $this->encrypt($key); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Sets the value secret hash column. |
87
|
|
|
* |
88
|
|
|
* @param string|null $hash |
89
|
|
|
* |
90
|
|
|
* @throws \ParagonIE\CipherSweet\Exception\BlindIndexNotFoundException |
91
|
|
|
* @throws \ParagonIE\CipherSweet\Exception\CryptoOperationException |
92
|
|
|
* @throws \SodiumException |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
public function setSecretHashAttribute(?string $hash = null): void |
97
|
|
|
{ |
98
|
|
|
$this->attributes['secret_hash'] = $this->blindIndexValue(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get the value for private key column. |
103
|
|
|
* |
104
|
|
|
* @param string $encryptedKey |
105
|
|
|
* |
106
|
|
|
* @return mixed |
107
|
|
|
*/ |
108
|
|
|
public function getPrivateKeyAttribute(string $encryptedKey) |
109
|
|
|
{ |
110
|
|
|
return $this->decrypt($encryptedKey); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Add where clause for status equals active. |
115
|
|
|
* |
116
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
117
|
|
|
* |
118
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
119
|
|
|
*/ |
120
|
|
|
public function scopeActive(Builder $query): Builder |
121
|
|
|
{ |
122
|
|
|
return $query->where('status', self::STATUSES['ACTIVE']); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Add where clause for status equals active. |
127
|
|
|
* |
128
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
129
|
|
|
* |
130
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
131
|
|
|
*/ |
132
|
|
|
public function scopeSuspended(Builder $query): Builder |
133
|
|
|
{ |
134
|
|
|
return $query->where('status', self::STATUSES['SUSPENDED']); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Find client active api keys by private key. |
139
|
|
|
* |
140
|
|
|
* @param string $privateKey |
141
|
|
|
* |
142
|
|
|
* @return static|null |
143
|
|
|
*/ |
144
|
|
|
public static function findActiveByPrivateKey(string $privateKey): ?self |
145
|
|
|
{ |
146
|
|
|
return static::query() |
147
|
|
|
->with('client') |
148
|
|
|
->active() |
149
|
|
|
->whereBlindIndex($privateKey) |
150
|
|
|
->first(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Find client suspended api keys by private key. |
155
|
|
|
* |
156
|
|
|
* @param string $privateKey |
157
|
|
|
* |
158
|
|
|
* @return static|null |
159
|
|
|
*/ |
160
|
|
|
public static function findSuspendedByPrivateKey(string $privateKey): ?self |
161
|
|
|
{ |
162
|
|
|
return static::query() |
163
|
|
|
->with('client') |
164
|
|
|
->suspended() |
165
|
|
|
->whereBlindIndex($privateKey) |
166
|
|
|
->first(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Find client api keys by private key. |
171
|
|
|
* |
172
|
|
|
* @param string $privateKey |
173
|
|
|
* |
174
|
|
|
* @return static|null |
175
|
|
|
*/ |
176
|
|
|
public static function findByPrivateKey(string $privateKey): ?self |
177
|
|
|
{ |
178
|
|
|
return self::withTrashed() |
|
|
|
|
179
|
|
|
->with('client') |
180
|
|
|
->whereBlindIndex($privateKey) |
181
|
|
|
->first(); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Generates the public and secret key pairs for both live and test domains. |
186
|
|
|
* @throws \Exception |
187
|
|
|
* |
188
|
|
|
* @return array |
189
|
|
|
*/ |
190
|
|
|
public static function generateKeyPairArray(): array |
191
|
|
|
{ |
192
|
|
|
[$publicKey, $privateKey] = self::generatePublicPrivateKeys(); |
|
|
|
|
193
|
|
|
|
194
|
|
|
return [ |
195
|
|
|
'public_key' => self::PUBLIC_KEY_PREFIX.'_'.$publicKey, |
196
|
|
|
'private_key' => self::PRIVATE_KEY_PREFIX.'_'.$privateKey, |
197
|
|
|
'secret_hash' => null, |
198
|
|
|
'status' => self::STATUSES['ACTIVE'], |
199
|
|
|
]; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Generate unique public and secret key pairs. |
204
|
|
|
* @throws \Exception |
205
|
|
|
* |
206
|
|
|
* @return array |
207
|
|
|
*/ |
208
|
|
|
private static function generatePublicPrivateKeys(): array |
209
|
|
|
{ |
210
|
|
|
do { |
211
|
|
|
$privateKey = bin2hex(random_bytes(20)); |
212
|
|
|
$publicKey = bin2hex(random_bytes(20)); |
213
|
|
|
|
214
|
|
|
$secretHash = (new self())->getBlindIndexValueFor($privateKey); |
215
|
|
|
$hashExists = self::where('secret_hash', $secretHash)->withTrashed()->exists(); |
216
|
|
|
} while ($hashExists); |
217
|
|
|
|
218
|
|
|
return [$publicKey, $privateKey]; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: