1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 08:51:34 +02:00
invoiceninja/app/Http/Middleware/PasswordProtection.php

97 lines
2.9 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Http\Middleware;
use App\Libraries\MultiDB;
use App\Libraries\OAuth\Providers\Google;
use Closure;
2020-10-28 11:10:49 +01:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
2020-10-28 11:10:49 +01:00
use stdClass;
class PasswordProtection
{
/**
* Handle an incoming request.
*
2020-10-28 11:10:49 +01:00
* @param Request $request
* @param Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$error = [
'message' => 'Invalid Password',
2020-10-28 11:10:49 +01:00
'errors' => new stdClass,
];
2021-03-09 11:52:48 +01:00
$timeout = auth()->user()->company()->default_password_timeout;
2021-03-09 11:52:48 +01:00
if($timeout == 0)
2021-05-05 08:44:31 +02:00
$timeout = 30*60*1000*1000;
2021-03-09 11:52:48 +01:00
else
2021-05-05 08:44:31 +02:00
$timeout = $timeout/1000;
if (Cache::get(auth()->user()->hashed_id.'_logged_in')) {
Cache::put(auth()->user()->hashed_id.'_logged_in', Str::random(64), $timeout);
return $next($request);
}elseif( $request->header('X-API-OAUTH-PASSWORD') && strlen($request->header('X-API-OAUTH-PASSWORD')) >=1){
//user is attempting to reauth with OAuth - check the token value
//todo expand this to include all OAuth providers
$user = false;
$google = new Google();
$user = $google->getTokenResponse(request()->header('X-API-OAUTH-PASSWORD'));
if (is_array($user)) {
$query = [
'oauth_user_id' => $google->harvestSubField($user),
'oauth_provider_id'=> 'google'
];
//If OAuth and user also has a password set - check both
2021-03-19 13:37:57 +01:00
if ($existing_user = MultiDB::hasUser($query) && auth()->user()->has_password && Hash::check(auth()->user()->password, $request->header('X-API-PASSWORD'))) {
2021-05-05 08:11:40 +02:00
Cache::put(auth()->user()->hashed_id.'_logged_in', Str::random(64), $timeout);
return $next($request);
}
2021-03-07 21:56:44 +01:00
elseif($existing_user = MultiDB::hasUser($query) && !auth()->user()->has_password){
2021-05-05 08:11:40 +02:00
Cache::put(auth()->user()->hashed_id.'_logged_in', Str::random(64), $timeout);
return $next($request);
}
}
return response()->json($error, 412);
}elseif ($request->header('X-API-PASSWORD') && Hash::check($request->header('X-API-PASSWORD'), auth()->user()->password)) {
2021-05-05 08:11:40 +02:00
Cache::put(auth()->user()->hashed_id.'_logged_in', Str::random(64), $timeout);
return $next($request);
} else {
return response()->json($error, 412);
}
}
2021-03-09 11:30:34 +01:00
}