2019-10-22 13:27:03 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-10-22 13:27:03 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2020-01-07 01:13:47 +01:00
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-10-22 13:27:03 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
|
|
|
|
use Closure;
|
2020-10-28 11:10:49 +01:00
|
|
|
use Illuminate\Http\Request;
|
2019-10-23 03:01:25 +02:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
2019-10-24 06:46:24 +02:00
|
|
|
use Illuminate\Support\Str;
|
2020-10-28 11:10:49 +01:00
|
|
|
use stdClass;
|
2019-10-22 13:27:03 +02:00
|
|
|
|
|
|
|
class PasswordProtection
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param Request $request
|
|
|
|
* @param Closure $next
|
2019-10-22 13:27:03 +02:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle($request, Closure $next)
|
|
|
|
{
|
|
|
|
$error = [
|
|
|
|
'message' => 'Invalid Password',
|
2020-10-28 11:10:49 +01:00
|
|
|
'errors' => new stdClass,
|
2019-10-22 13:27:03 +02:00
|
|
|
];
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($request->header('X-API-PASSWORD')) {
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! Hash::check($request->header('X-API-PASSWORD'), auth()->user()->password)) {
|
2019-10-22 13:27:03 +02:00
|
|
|
return response()->json($error, 403);
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
} elseif (Cache::get(auth()->user()->email.'_logged_in')) {
|
|
|
|
Cache::pull(auth()->user()->email.'_logged_in');
|
|
|
|
Cache::add(auth()->user()->email.'_logged_in', Str::random(64), now()->addMinutes(30));
|
2020-02-17 10:37:44 +01:00
|
|
|
|
2019-10-22 13:27:03 +02:00
|
|
|
return $next($request);
|
2019-12-30 22:59:12 +01:00
|
|
|
} else {
|
2019-10-22 13:27:03 +02:00
|
|
|
$error = [
|
|
|
|
'message' => 'Access denied',
|
2020-10-28 11:10:49 +01:00
|
|
|
'errors' => new stdClass,
|
2019-10-22 13:27:03 +02:00
|
|
|
];
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
return response()->json($error, 412);
|
2019-10-22 13:27:03 +02:00
|
|
|
}
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
Cache::add(auth()->user()->email.'_logged_in', Str::random(64), now()->addMinutes(30));
|
2019-10-22 13:27:03 +02:00
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
}
|