Skip to content

Commit

Permalink
Implementation of protected files serving
Browse files Browse the repository at this point in the history
  • Loading branch information
LuckyCyborg committed Jul 14, 2019
1 parent d144586 commit 0656405
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
11 changes: 11 additions & 0 deletions app/Config/Routing.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,15 @@
'twbs/bootstrap' => 'dist',
),
),

/*
* The Protected Files Serving configuration.
*/
'files' => array(
// The path to the protected files.
'path' => BASEPATH .'files',

// The access token validity - in minutes.
'validity' => 180,
),
);
2 changes: 1 addition & 1 deletion app/Platform/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// Define The Application Version
//--------------------------------------------------------------------------

define('VERSION', '4.2.0');
define('VERSION', '4.2.1');

//--------------------------------------------------------------------------
// Set PHP Error Reporting Options
Expand Down
22 changes: 22 additions & 0 deletions app/Routes/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

use Nova\Http\Request;

use Carbon\Carbon;


// Register the route for assets from main assets folder.
$dispatcher->route('assets/(:all)', function (Request $request, $path) use ($dispatcher)
Expand All @@ -26,3 +28,23 @@
{
return $dispatcher->serveVendorFile($path, $request);
});

// Register the route for files from protected folder.
$dispatcher->route('files/(:any)/(:any)/(:all)', function (Request $request, $hash, $timestamp, $path) use ($dispatcher)
{
$basePath = Config::get('routing.files.path', BASEPATH .'files');

$validity = Carbon::now()->subMinutes(
Config::get('routing.files.validity', 180) // In minutes.
);

$localHash = hash_hmac('sha256', $path .'|' .$timestamp .'|' .$request->ip(), Config::get('app.key'));

if (! File::isDirectory($basePath) || ! hash_equals($hash, $localHash) || ($validity->timestamp > hexdec($timestamp))) {
return Response::make('Forbidden', 403);
}

$path = $basePath .DS .str_replace('/', DS, $path);

return $dispatcher->serve($path, $request);
});

0 comments on commit 0656405

Please sign in to comment.