44
55use Pterodactyl \Models \APIKey ;
66use Illuminate \Database \ConnectionInterface ;
7+ use Illuminate \Contracts \Encryption \Encrypter ;
78use Pterodactyl \Contracts \Repository \ApiKeyRepositoryInterface ;
89
910class KeyCreationService
@@ -14,9 +15,9 @@ class KeyCreationService
1415 private $ connection ;
1516
1617 /**
17- * @var \Pterodactyl\Services\Api\PermissionService
18+ * @var \Illuminate\Contracts\Encryption\Encrypter
1819 */
19- private $ permissionService ;
20+ private $ encrypter ;
2021
2122 /**
2223 * @var \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface
@@ -28,67 +29,36 @@ class KeyCreationService
2829 *
2930 * @param \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface $repository
3031 * @param \Illuminate\Database\ConnectionInterface $connection
31- * @param \Pterodactyl\Services\Api\PermissionService $permissionService
32+ * @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
3233 */
3334 public function __construct (
3435 ApiKeyRepositoryInterface $ repository ,
3536 ConnectionInterface $ connection ,
36- PermissionService $ permissionService
37+ Encrypter $ encrypter
3738 ) {
3839 $ this ->repository = $ repository ;
3940 $ this ->connection = $ connection ;
40- $ this ->permissionService = $ permissionService ;
41+ $ this ->encrypter = $ encrypter ;
4142 }
4243
4344 /**
44- * Create a new API Key on the system with the given permissions.
45+ * Create a new API key for the Panel using the permissions passed in the data request.
46+ * This will automatically generate an identifer and an encrypted token that are
47+ * stored in the database.
4548 *
4649 * @param array $data
47- * @param array $permissions
48- * @param array $administrative
4950 * @return \Pterodactyl\Models\APIKey
5051 *
51- * @throws \Exception
5252 * @throws \Pterodactyl\Exceptions\Model\DataValidationException
5353 */
54- public function handle (array $ data, array $ permissions , array $ administrative = [] ): APIKey
54+ public function handle (array $ data ): APIKey
5555 {
56- $ token = str_random (APIKey::KEY_LENGTH );
57- $ data = array_merge ($ data , ['token ' => $ token ]);
56+ $ data = array_merge ($ data , [
57+ 'identifer ' => str_random (APIKey::IDENTIFIER_LENGTH ),
58+ 'token ' => $ this ->encrypter ->encrypt (str_random (APIKey::KEY_LENGTH )),
59+ ]);
5860
59- $ this ->connection ->beginTransaction ();
6061 $ instance = $ this ->repository ->create ($ data , true , true );
61- $ nodes = $ this ->permissionService ->getPermissions ();
62-
63- foreach ($ permissions as $ permission ) {
64- @list ($ block , $ search ) = explode ('- ' , $ permission , 2 );
65-
66- if (
67- (empty ($ block ) || empty ($ search )) ||
68- ! array_key_exists ($ block , $ nodes ['_user ' ]) ||
69- ! in_array ($ search , $ nodes ['_user ' ][$ block ])
70- ) {
71- continue ;
72- }
73-
74- $ this ->permissionService ->create ($ instance ->id , sprintf ('user.%s ' , $ permission ));
75- }
76-
77- foreach ($ administrative as $ permission ) {
78- @list ($ block , $ search ) = explode ('- ' , $ permission , 2 );
79-
80- if (
81- (empty ($ block ) || empty ($ search )) ||
82- ! array_key_exists ($ block , $ nodes ) ||
83- ! in_array ($ search , $ nodes [$ block ])
84- ) {
85- continue ;
86- }
87-
88- $ this ->permissionService ->create ($ instance ->id , $ permission );
89- }
90-
91- $ this ->connection ->commit ();
9262
9363 return $ instance ;
9464 }
0 commit comments