forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiKeyController.php
More file actions
76 lines (65 loc) · 2.26 KB
/
ApiKeyController.php
File metadata and controls
76 lines (65 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
namespace Pterodactyl\Http\Controllers\Api\Client;
use Pterodactyl\Models\ApiKey;
use Illuminate\Http\JsonResponse;
use Pterodactyl\Facades\Activity;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
use Pterodactyl\Transformers\Api\Client\ApiKeyTransformer;
use Pterodactyl\Http\Requests\Api\Client\Account\StoreApiKeyRequest;
class ApiKeyController extends ClientApiController
{
/**
* Returns all of the API keys that exist for the given client.
*
* @return array
*/
public function index(ClientApiRequest $request)
{
return $this->fractal->collection($request->user()->apiKeys)
->transformWith($this->getTransformer(ApiKeyTransformer::class))
->toArray();
}
/**
* Store a new API key for a user's account.
*
* @return array
*
* @throws \Pterodactyl\Exceptions\DisplayException
*/
public function store(StoreApiKeyRequest $request)
{
if ($request->user()->apiKeys->count() >= 5) {
throw new DisplayException('You have reached the account limit for number of API keys.');
}
$token = $request->user()->createToken(
$request->input('description'),
$request->input('allowed_ips')
);
Activity::event('user:api-key.create')
->subject($token->accessToken)
->property('identifier', $token->accessToken->identifier)
->log();
return $this->fractal->item($token->accessToken)
->transformWith($this->getTransformer(ApiKeyTransformer::class))
->addMeta(['secret_token' => $token->plainTextToken])
->toArray();
}
/**
* Deletes a given API key.
*
* @return \Illuminate\Http\JsonResponse
*/
public function delete(ClientApiRequest $request, string $identifier)
{
$key = $request->user()->apiKeys()
->where('key_type', ApiKey::TYPE_ACCOUNT)
->where('identifier', $identifier)
->first();
Activity::event('user:api-key.delete')
->property('identifer', $key->identifer)
->log();
$key->delete();
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
}
}