add ability to generate a token to retrieve the config for a specific node

This commit is contained in:
Jakob Schrettenbrunner 2017-01-07 18:10:11 +01:00
parent 24bab6de17
commit e1e159b7de
7 changed files with 147 additions and 0 deletions

View file

@ -28,6 +28,7 @@ use DB;
use Log;
use Alert;
use Validator;
use Carbon\Carbon;
use Pterodactyl\Models;
use Illuminate\Http\Request;
use Pterodactyl\Exceptions\DisplayException;
@ -276,4 +277,24 @@ class NodesController extends Controller
'tab' => 'tab_delete',
]);
}
public function getConfigurationToken(Request $request, $id) {
// Check if Node exists. Will lead to 404 if not.
Models\Node::findOrFail($id);
// Create a token
$token = new Models\NodeConfigurationToken();
$token->node = $id;
$token->token = str_random(32);
$token->expires_at = Carbon::now()->addMinutes(5); // Expire in 5 Minutes
$token->save();
$token_response = array(
'token' => $token->token,
'expires_at' => $token->expires_at->toDateTimeString()
);
return response(json_encode($token_response), 200)
->header('Content-Type', 'application/json');
}
}

View file

@ -24,10 +24,12 @@
namespace Pterodactyl\Http\Controllers\Remote;
use Carbon\Carbon;
use Pterodactyl\Models;
use Illuminate\Http\Request;
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Services\NotificationService;
use Pterodactyl\Models\NodeConfigurationToken;
class RemoteController extends Controller
{
@ -107,4 +109,28 @@ class RemoteController extends Controller
return response('', 201);
}
public function getConfiguration(Request $request, $tokenString) {
// Try to query the token and the node from the database
try {
$token = Models\NodeConfigurationToken::where('token', $tokenString)->firstOrFail();
$node = Models\Node::findOrFail($token->node);
} catch(\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
return response(json_encode(array('error' => 'token_invalid')), 403)
->header('Content-Type', 'application/json');
}
// Check if token is expired
if ($token->expires_at->lt(Carbon::now())) {
$token->delete();
return response(json_encode(array('error' => 'token_expired')), 403)
->header('Content-Type', 'application/json');
}
// Delete the token, it's one-time use
$token->delete();
return response($node->getConfigurationAsJson(), 200)
->header('Content-Type', 'application/json');
}
}