|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pterodactyl\Repositories; |
| 4 | + |
| 5 | +use DB; |
| 6 | +use Validator; |
| 7 | +use IPTools\Network; |
| 8 | + |
| 9 | +use Pterodactyl\Models; |
| 10 | +use Pterodactyl\Exceptions\DisplayException; |
| 11 | +use Pterodactyl\Exceptions\DisplayValidationException; |
| 12 | + |
| 13 | +class APIRepository |
| 14 | +{ |
| 15 | + |
| 16 | + /** |
| 17 | + * Valid API permissions. |
| 18 | + * @var array |
| 19 | + */ |
| 20 | + protected $permissions = [ |
| 21 | + '*', |
| 22 | + |
| 23 | + // User Management Routes |
| 24 | + 'api.users', |
| 25 | + 'api.users.view', |
| 26 | + 'api.users.post', |
| 27 | + 'api.users.delete', |
| 28 | + 'api.users.patch', |
| 29 | + |
| 30 | + // Server Manaement Routes |
| 31 | + 'api.servers', |
| 32 | + 'api.servers.view', |
| 33 | + 'api.servers.post', |
| 34 | + 'api.servers.suspend', |
| 35 | + 'api.servers.unsuspend', |
| 36 | + 'api.servers.delete', |
| 37 | + |
| 38 | + // Node Management Routes |
| 39 | + 'api.nodes', |
| 40 | + 'api.nodes.view', |
| 41 | + 'api.nodes.post', |
| 42 | + 'api.nodes.view_allocations', |
| 43 | + 'api.nodes.delete', |
| 44 | + |
| 45 | + // Assorted Routes |
| 46 | + 'api.services', |
| 47 | + 'api.services.view', |
| 48 | + 'api.locations', |
| 49 | + ]; |
| 50 | + |
| 51 | + /** |
| 52 | + * Holder for listing of allowed IPs when creating a new key. |
| 53 | + * @var array |
| 54 | + */ |
| 55 | + protected $allowed = []; |
| 56 | + |
| 57 | + /** |
| 58 | + * Constructor |
| 59 | + */ |
| 60 | + public function __construct() |
| 61 | + { |
| 62 | + // |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Create a New API Keypair on the system. |
| 67 | + * |
| 68 | + * @param array $data An array with a permissions and allowed_ips key. |
| 69 | + * |
| 70 | + * @throws Pterodactyl\Exceptions\DisplayException if there was an error that can be safely displayed to end-users. |
| 71 | + * @throws Pterodactyl\Exceptions\DisplayValidationException if there was a validation error. |
| 72 | + * |
| 73 | + * @return string Returns the generated secret token. |
| 74 | + */ |
| 75 | + public function new(array $data) |
| 76 | + { |
| 77 | + $validator = Validator::make($data, [ |
| 78 | + 'permissions' => 'required|array' |
| 79 | + ]); |
| 80 | + |
| 81 | + $validator->after(function($validator) use ($data) { |
| 82 | + if (array_key_exists('allowed_ips', $data) && !empty($data['allowed_ips'])) { |
| 83 | + foreach(explode("\n", $data['allowed_ips']) as $ip) { |
| 84 | + $ip = trim($ip); |
| 85 | + try { |
| 86 | + Network::parse($ip); |
| 87 | + array_push($this->allowed, $ip); |
| 88 | + } catch (\Exception $ex) { |
| 89 | + $validator->errors()->add('allowed_ips', 'Could not parse IP <' . $ip . '> because it is in an invalid format.'); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + // Run validator, throw catchable and displayable exception if it fails. |
| 96 | + // Exception includes a JSON result of failed validation rules. |
| 97 | + if ($validator->fails()) { |
| 98 | + throw new DisplayValidationException($validator->errors()); |
| 99 | + } |
| 100 | + |
| 101 | + DB::beginTransaction(); |
| 102 | + |
| 103 | + $key = new Models\APIKey; |
| 104 | + $key->fill([ |
| 105 | + 'public' => str_random(16), |
| 106 | + 'secret' => str_random(16) . '.' . str_random(15), |
| 107 | + 'allowed_ips' => empty($this->allowed) ? null : json_encode($this->allowed) |
| 108 | + ]); |
| 109 | + $key->save(); |
| 110 | + |
| 111 | + foreach($data['permissions'] as $permission) { |
| 112 | + if (in_array($permission, $this->permissions)) { |
| 113 | + $model = new Models\APIPermission; |
| 114 | + $model->fill([ |
| 115 | + 'key_id' => $key->id, |
| 116 | + 'permission' => $permission |
| 117 | + ]); |
| 118 | + $model->save(); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + try { |
| 123 | + DB::commit(); |
| 124 | + return $key->secret; |
| 125 | + } catch (\Exception $ex) { |
| 126 | + throw $ex; |
| 127 | + } |
| 128 | + |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Revokes an API key and associated permissions. |
| 133 | + * |
| 134 | + * @param string $key The public key. |
| 135 | + * |
| 136 | + * @throws Illuminate\Database\Eloquent\ModelNotFoundException |
| 137 | + * |
| 138 | + * @return void |
| 139 | + */ |
| 140 | + public function revoke(string $key) |
| 141 | + { |
| 142 | + DB::beginTransaction(); |
| 143 | + |
| 144 | + $model = Models\APIKey::where('public', $key)->firstOrFail(); |
| 145 | + $permissions = Models\APIPermission::where('key_id', $model->id)->delete(); |
| 146 | + $model->delete(); |
| 147 | + |
| 148 | + DB::commit(); |
| 149 | + } |
| 150 | + |
| 151 | +} |
0 commit comments