Skip to content

Commit 0656405

Browse files
committed
Implementation of protected files serving
1 parent d144586 commit 0656405

3 files changed

Lines changed: 34 additions & 1 deletion

File tree

app/Config/Routing.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,15 @@
4040
'twbs/bootstrap' => 'dist',
4141
),
4242
),
43+
44+
/*
45+
* The Protected Files Serving configuration.
46+
*/
47+
'files' => array(
48+
// The path to the protected files.
49+
'path' => BASEPATH .'files',
50+
51+
// The access token validity - in minutes.
52+
'validity' => 180,
53+
),
4354
);

app/Platform/Bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// Define The Application Version
1515
//--------------------------------------------------------------------------
1616

17-
define('VERSION', '4.2.0');
17+
define('VERSION', '4.2.1');
1818

1919
//--------------------------------------------------------------------------
2020
// Set PHP Error Reporting Options

app/Routes/Assets.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
use Nova\Http\Request;
44

5+
use Carbon\Carbon;
6+
57

68
// Register the route for assets from main assets folder.
79
$dispatcher->route('assets/(:all)', function (Request $request, $path) use ($dispatcher)
@@ -26,3 +28,23 @@
2628
{
2729
return $dispatcher->serveVendorFile($path, $request);
2830
});
31+
32+
// Register the route for files from protected folder.
33+
$dispatcher->route('files/(:any)/(:any)/(:all)', function (Request $request, $hash, $timestamp, $path) use ($dispatcher)
34+
{
35+
$basePath = Config::get('routing.files.path', BASEPATH .'files');
36+
37+
$validity = Carbon::now()->subMinutes(
38+
Config::get('routing.files.validity', 180) // In minutes.
39+
);
40+
41+
$localHash = hash_hmac('sha256', $path .'|' .$timestamp .'|' .$request->ip(), Config::get('app.key'));
42+
43+
if (! File::isDirectory($basePath) || ! hash_equals($hash, $localHash) || ($validity->timestamp > hexdec($timestamp))) {
44+
return Response::make('Forbidden', 403);
45+
}
46+
47+
$path = $basePath .DS .str_replace('/', DS, $path);
48+
49+
return $dispatcher->serve($path, $request);
50+
});

0 commit comments

Comments
 (0)