Skip to content

Commit 317698a

Browse files
committed
encrypt API keys
1 parent 3e595ca commit 317698a

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

app/Http/Middleware/APISecretToken.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Pterodactyl\Http\Middleware;
44

5+
use Crypt;
6+
57
use Pterodactyl\Models\APIKey;
68
use Pterodactyl\Models\APIPermission;
79

@@ -12,6 +14,7 @@
1214
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; // 400
1315
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException; // 401
1416
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; // 403
17+
use Symfony\Component\HttpKernel\Exception\HttpException; //500
1518

1619
class APISecretToken extends Authorization
1720
{
@@ -63,7 +66,13 @@ public function authenticate(Request $request, Route $route)
6366
}
6467
}
6568

66-
if($this->_generateHMAC($request->fullUrl(), $request->getContent(), $key->secret) !== base64_decode($hashed)) {
69+
try {
70+
$decrypted = Crypt::decrypt($key->secret);
71+
} catch (\Illuminate\Contracts\Encryption\DecryptException $ex) {
72+
throw new HttpException('There was an error while attempting to check your secret key.');
73+
}
74+
75+
if($this->_generateHMAC($request->fullUrl(), $request->getContent(), $decrypted) !== base64_decode($hashed)) {
6776
throw new BadRequestHttpException('The hashed body was not valid. Potential modification of contents in route.');
6877
}
6978

app/Repositories/APIRepository.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Pterodactyl\Repositories;
44

55
use DB;
6+
use Crypt;
67
use Validator;
78
use IPTools\Network;
89

@@ -100,10 +101,11 @@ public function new(array $data)
100101

101102
DB::beginTransaction();
102103

104+
$secretKey = str_random(16) . '.' . str_random(15);
103105
$key = new Models\APIKey;
104106
$key->fill([
105107
'public' => str_random(16),
106-
'secret' => str_random(16) . '.' . str_random(15),
108+
'secret' => Crypt::encrypt($secretKey),
107109
'allowed_ips' => empty($this->allowed) ? null : json_encode($this->allowed)
108110
]);
109111
$key->save();
@@ -121,7 +123,7 @@ public function new(array $data)
121123

122124
try {
123125
DB::commit();
124-
return $key->secret;
126+
return $secretKey;
125127
} catch (\Exception $ex) {
126128
throw $ex;
127129
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class ModifyApiKeys extends Migration
7+
{
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up()
14+
{
15+
Schema::table('api_keys', function (Blueprint $table) {
16+
DB::statement('ALTER TABLE `api_keys` MODIFY `secret` TINYTEXT NOT NULL');
17+
});
18+
}
19+
20+
/**
21+
* Reverse the migrations.
22+
*
23+
* @return void
24+
*/
25+
public function down()
26+
{
27+
Schema::table('api_keys', function (Blueprint $table) {
28+
DB::statement('ALTER TABLE `api_keys` MODIFY `secret` TINYTEXT NOT NULL');
29+
});
30+
}
31+
}

0 commit comments

Comments
 (0)