add autodeploy and add basic auth for file uploader
Some checks failed
Deploy back2 / deploy (push) Failing after 46s
Some checks failed
Deploy back2 / deploy (push) Failing after 46s
This commit is contained in:
42
app/Http/Middleware/EnsureFileUploadBasicAuth.php
Normal file
42
app/Http/Middleware/EnsureFileUploadBasicAuth.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class EnsureFileUploadBasicAuth
|
||||
{
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
$authorizedUsers = config('file_links.upload_basic_auth_users', []);
|
||||
$username = (string) $request->getUser();
|
||||
$password = (string) $request->getPassword();
|
||||
|
||||
foreach ($authorizedUsers as $authorizedUser) {
|
||||
$validUsername = isset($authorizedUser['username']) && hash_equals((string) $authorizedUser['username'], $username);
|
||||
$validPassword = isset($authorizedUser['password']) && hash_equals((string) $authorizedUser['password'], $password);
|
||||
|
||||
if ($validUsername && $validPassword) {
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->unauthorizedResponse($request);
|
||||
}
|
||||
|
||||
private function unauthorizedResponse(Request $request): Response
|
||||
{
|
||||
if ($request->expectsJson()) {
|
||||
return new JsonResponse(['message' => 'Unauthorized'], Response::HTTP_UNAUTHORIZED, [
|
||||
'WWW-Authenticate' => 'Basic realm="File Upload"',
|
||||
]);
|
||||
}
|
||||
|
||||
return response('Unauthorized', Response::HTTP_UNAUTHORIZED, [
|
||||
'WWW-Authenticate' => 'Basic realm="File Upload"',
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user