Skip to content

Commit 47e14cc

Browse files
committed
API key UI changes and backend storage of the keys
1 parent 69e67b5 commit 47e14cc

File tree

11 files changed

+136
-160
lines changed

11 files changed

+136
-160
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ This file is a running track of new features and fixes to each version of the pa
33

44
This project follows [Semantic Versioning](http://semver.org) guidelines.
55

6+
## v0.7.0-beta.3 (Derelict Dermodactylus)
7+
### Changed
8+
* API keys have been changed to only use a single public key passed in a bearer token. All existing keys can continue being used, however only the first 32 characters should be sent.
9+
610
## v0.7.0-beta.2 (Derelict Dermodactylus)
711
### Fixed
812
* `[beta.1]` — Fixes a CORS header issue due to a wrong API endpoint being provided in the administrative node listing.

app/Http/Controllers/Base/APIController.php

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,4 @@
11
<?php
2-
/**
3-
* Pterodactyl - Panel
4-
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>
5-
* Some Modifications (c) 2015 Dylan Seidt <dylan.seidt@gmail.com>.
6-
*
7-
* Permission is hereby granted, free of charge, to any person obtaining a copy
8-
* of this software and associated documentation files (the "Software"), to deal
9-
* in the Software without restriction, including without limitation the rights
10-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11-
* copies of the Software, and to permit persons to whom the Software is
12-
* furnished to do so, subject to the following conditions:
13-
*
14-
* The above copyright notice and this permission notice shall be included in all
15-
* copies or substantial portions of the Software.
16-
*
17-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23-
* SOFTWARE.
24-
*/
252

263
namespace Pterodactyl\Http\Controllers\Base;
274

@@ -120,7 +97,7 @@ public function store(ApiKeyFormRequest $request)
12097
'memo' => $request->input('memo'),
12198
], $request->input('permissions', []), $adminPermissions);
12299

123-
$this->alert->success(trans('base.api.index.keypair_created', ['token' => $secret]))->flash();
100+
$this->alert->success(trans('base.api.index.keypair_created'))->flash();
124101

125102
return redirect()->route('account.api');
126103
}
@@ -136,7 +113,7 @@ public function revoke(Request $request, $key)
136113
{
137114
$this->repository->deleteWhere([
138115
['user_id', '=', $request->user()->id],
139-
['public', '=', $key],
116+
['token', '=', $key],
140117
]);
141118

142119
return response('', 204);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Middleware\API;
4+
5+
use Closure;
6+
use Illuminate\Http\Request;
7+
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
8+
9+
class AuthenticateKey
10+
{
11+
/**
12+
* @var \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface
13+
*/
14+
private $repository;
15+
16+
/**
17+
* AuthenticateKey constructor.
18+
*
19+
* @param \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface $repository
20+
*/
21+
public function __construct(ApiKeyRepositoryInterface $repository)
22+
{
23+
$this->repository = $repository;
24+
}
25+
26+
/**
27+
* Handle an API request by verifying that the provided API key
28+
* is in a valid format, and the route being accessed is allowed
29+
* for the given key.
30+
*
31+
* @param \Illuminate\Http\Request $request
32+
* @param \Closure $next
33+
*/
34+
public function handle(Request $request, Closure $next)
35+
{
36+
$this->repository->findFirstWhere([
37+
'',
38+
]);
39+
}
40+
}

app/Models/APIKey.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,15 @@ class APIKey extends Model implements CleansAttributes, ValidableContract
1919
{
2020
use Eloquence, Validable;
2121

22+
const KEY_LENGTH = 32;
23+
2224
/**
2325
* The table associated with the model.
2426
*
2527
* @var string
2628
*/
2729
protected $table = 'api_keys';
2830

29-
/**
30-
* The attributes excluded from the model's JSON form.
31-
*
32-
* @var array
33-
*/
34-
protected $hidden = ['secret'];
35-
3631
/**
3732
* Cast values to correct type.
3833
*
@@ -57,8 +52,7 @@ class APIKey extends Model implements CleansAttributes, ValidableContract
5752
protected static $applicationRules = [
5853
'memo' => 'required',
5954
'user_id' => 'required',
60-
'secret' => 'required',
61-
'public' => 'required',
55+
'token' => 'required',
6256
];
6357

6458
/**
@@ -68,8 +62,7 @@ class APIKey extends Model implements CleansAttributes, ValidableContract
6862
*/
6963
protected static $dataIntegrityRules = [
7064
'user_id' => 'exists:users,id',
71-
'public' => 'string|size:16',
72-
'secret' => 'string',
65+
'token' => 'string|size:32',
7366
'memo' => 'nullable|string|max:500',
7467
'allowed_ips' => 'nullable|json',
7568
'expires_at' => 'nullable|datetime',
Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,42 @@
11
<?php
2-
/**
3-
* Pterodactyl - Panel
4-
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5-
*
6-
* This software is licensed under the terms of the MIT license.
7-
* https://opensource.org/licenses/MIT
8-
*/
92

103
namespace Pterodactyl\Services\Api;
114

5+
use Pterodactyl\Models\APIKey;
126
use Illuminate\Database\ConnectionInterface;
13-
use Illuminate\Contracts\Encryption\Encrypter;
147
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
158

169
class KeyCreationService
1710
{
18-
const PUB_CRYPTO_LENGTH = 16;
19-
const PRIV_CRYPTO_LENGTH = 64;
20-
2111
/**
2212
* @var \Illuminate\Database\ConnectionInterface
2313
*/
24-
protected $connection;
25-
26-
/**
27-
* @var \Illuminate\Contracts\Encryption\Encrypter
28-
*/
29-
protected $encrypter;
14+
private $connection;
3015

3116
/**
3217
* @var \Pterodactyl\Services\Api\PermissionService
3318
*/
34-
protected $permissionService;
19+
private $permissionService;
3520

3621
/**
3722
* @var \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface
3823
*/
39-
protected $repository;
24+
private $repository;
4025

4126
/**
4227
* ApiKeyService constructor.
4328
*
4429
* @param \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface $repository
4530
* @param \Illuminate\Database\ConnectionInterface $connection
46-
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
4731
* @param \Pterodactyl\Services\Api\PermissionService $permissionService
4832
*/
4933
public function __construct(
5034
ApiKeyRepositoryInterface $repository,
5135
ConnectionInterface $connection,
52-
Encrypter $encrypter,
5336
PermissionService $permissionService
5437
) {
5538
$this->repository = $repository;
5639
$this->connection = $connection;
57-
$this->encrypter = $encrypter;
5840
$this->permissionService = $permissionService;
5941
}
6042

@@ -64,24 +46,17 @@ public function __construct(
6446
* @param array $data
6547
* @param array $permissions
6648
* @param array $administrative
67-
* @return string
49+
* @return \Pterodactyl\Models\APIKey
6850
*
6951
* @throws \Exception
7052
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
7153
*/
72-
public function handle(array $data, array $permissions, array $administrative = [])
54+
public function handle(array $data, array $permissions, array $administrative = []): APIKey
7355
{
74-
$publicKey = str_random(self::PUB_CRYPTO_LENGTH);
75-
$secretKey = str_random(self::PRIV_CRYPTO_LENGTH);
56+
$token = str_random(APIKey::KEY_LENGTH);
57+
$data = array_merge($data, ['token' => $token]);
7658

77-
// Start a Transaction
7859
$this->connection->beginTransaction();
79-
80-
$data = array_merge($data, [
81-
'public' => $publicKey,
82-
'secret' => $this->encrypter->encrypt($secretKey),
83-
]);
84-
8560
$instance = $this->repository->create($data, true, true);
8661
$nodes = $this->permissionService->getPermissions();
8762

@@ -115,6 +90,6 @@ public function handle(array $data, array $permissions, array $administrative =
11590

11691
$this->connection->commit();
11792

118-
return $secretKey;
93+
return $instance;
11994
}
12095
}

database/factories/ModelFactory.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,14 @@
221221
'expires_at' => \Carbon\Carbon::now()->addMinutes(10)->toDateTimeString(),
222222
];
223223
});
224+
225+
$factory->define(Pterodactyl\Models\APIKey::class, function (Faker\Generator $faker) {
226+
return [
227+
'id' => $faker->unique()->randomNumber(),
228+
'user_id' => $faker->randomNumber(),
229+
'token' => str_random(Pterodactyl\Models\APIKey::KEY_LENGTH),
230+
'memo' => 'Test Function Key',
231+
'created_at' => \Carbon\Carbon::now()->toDateTimeString(),
232+
'updated_at' => \Carbon\Carbon::now()->toDateTimeString(),
233+
];
234+
});

resources/lang/en/base.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
'header_sub' => 'Manage your API access keys.',
3434
'list' => 'API Keys',
3535
'create_new' => 'Create New API key',
36-
'keypair_created' => 'An API Key-Pair has been generated. Your API secret token is <code>:token</code>. Please take note of this key as it will not be displayed again.',
36+
'keypair_created' => 'An API Key-Pair has been generated and is listed below.',
3737
],
3838
'new' => [
3939
'header' => 'New API Key',

resources/lang/en/strings.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
'memo' => 'Memo',
3333
'created' => 'Created',
3434
'expires' => 'Expires',
35-
'public_key' => 'Public key',
35+
'public_key' => 'Token',
3636
'api_access' => 'Api Access',
3737
'never' => 'never',
3838
'sign_out' => 'Sign out',

resources/themes/pterodactyl/base/api/index.blade.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,7 @@
2020
@section('content')
2121
<div class="row">
2222
<div class="col-xs-12">
23-
<div class="alert alert-danger">
24-
API functionality is disabled in this beta release.
25-
</div>
26-
2723
<div class="box">
28-
<div class="overlay"></div>
2924
<div class="box-header">
3025
<h3 class="box-title">@lang('base.api.index.list')</h3>
3126
<div class="box-tools">
@@ -44,7 +39,7 @@
4439
</tr>
4540
@foreach ($keys as $key)
4641
<tr>
47-
<td><code>{{ $key->public }}</code></td>
42+
<td><code>{{ $key->token }}</code></td>
4843
<td>{{ $key->memo }}</td>
4944
<td class="text-center hidden-sm hidden-xs">
5045
{{ (new Carbon($key->created_at))->toDayDateTimeString() }}
@@ -57,7 +52,7 @@
5752
@endif
5853
</td>
5954
<td class="text-center">
60-
<a href="#delete" class="text-danger" data-action="delete" data-attr="{{ $key->public}}"><i class="fa fa-trash"></i></a>
55+
<a href="#delete" class="text-danger" data-action="delete" data-attr="{{ $key->token }}"><i class="fa fa-trash"></i></a>
6156
</td>
6257
</tr>
6358
@endforeach

0 commit comments

Comments
 (0)