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
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-10-22 13:27:03 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-10-22 13:27:03 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
|
2021-02-23 22:12:23 +01:00
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
use App\Libraries\OAuth\Providers\Google;
|
2019-10-22 13:27:03 +02:00
|
|
|
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;
|
2022-12-19 11:07:38 +01:00
|
|
|
use Laravel\Socialite\Facades\Socialite;
|
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
|
|
|
];
|
|
|
|
|
2023-06-08 12:37:01 +02:00
|
|
|
/** @var \App\Models\User auth()->user() */
|
2021-03-09 11:52:48 +01:00
|
|
|
$timeout = auth()->user()->company()->default_password_timeout;
|
2021-03-04 06:03:28 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($timeout == 0) {
|
2021-05-05 08:44:31 +02:00
|
|
|
$timeout = 30*60*1000*1000;
|
2023-02-16 02:36:09 +01:00
|
|
|
} else {
|
2021-05-05 08:44:31 +02:00
|
|
|
$timeout = $timeout/1000;
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2021-03-04 06:03:28 +01:00
|
|
|
|
2021-07-19 02:57:13 +02:00
|
|
|
//test if password if base64 encoded
|
|
|
|
$x_api_password = $request->header('X-API-PASSWORD');
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($request->header('X-API-PASSWORD-BASE64')) {
|
2021-07-19 06:17:58 +02:00
|
|
|
$x_api_password = base64_decode($request->header('X-API-PASSWORD-BASE64'));
|
|
|
|
}
|
2021-07-19 02:57:13 +02:00
|
|
|
|
2021-08-31 06:09:12 +02:00
|
|
|
// If no password supplied - then we just check if their authentication is in cache //
|
|
|
|
if (Cache::get(auth()->user()->hashed_id.'_'.auth()->user()->account_id.'_logged_in') && !$x_api_password) {
|
2021-05-13 14:41:32 +02:00
|
|
|
Cache::put(auth()->user()->hashed_id.'_'.auth()->user()->account_id.'_logged_in', Str::random(64), $timeout);
|
2021-03-04 06:03:28 +01:00
|
|
|
|
|
|
|
return $next($request);
|
2023-02-16 02:36:09 +01:00
|
|
|
} elseif ($request->header('X-API-OAUTH-PASSWORD') && strlen($request->header('X-API-OAUTH-PASSWORD')) >=1) {
|
2021-02-23 22:12:23 +01:00
|
|
|
//user is attempting to reauth with OAuth - check the token value
|
|
|
|
//todo expand this to include all OAuth providers
|
2023-02-16 02:36:09 +01:00
|
|
|
if (auth()->user()->oauth_provider_id == 'google') {
|
2022-06-24 23:47:15 +02:00
|
|
|
$user = false;
|
|
|
|
$google = new Google();
|
|
|
|
$user = $google->getTokenResponse(request()->header('X-API-OAUTH-PASSWORD'));
|
2022-06-25 00:30:06 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
if ($existing_user = MultiDB::hasUser($query) && auth()->user()->company()->oauth_password_required && auth()->user()->has_password && Hash::check(auth()->user()->password, $x_api_password)) {
|
|
|
|
nlog("existing user with password");
|
|
|
|
|
|
|
|
Cache::put(auth()->user()->hashed_id.'_'.auth()->user()->account_id.'_logged_in', Str::random(64), $timeout);
|
|
|
|
|
|
|
|
return $next($request);
|
2023-02-16 02:36:09 +01:00
|
|
|
} elseif ($existing_user = MultiDB::hasUser($query) && !auth()->user()->company()->oauth_password_required) {
|
2022-06-25 00:30:06 +02:00
|
|
|
nlog("existing user without password");
|
|
|
|
|
|
|
|
Cache::put(auth()->user()->hashed_id.'_'.auth()->user()->account_id.'_logged_in', Str::random(64), $timeout);
|
2023-02-16 02:36:09 +01:00
|
|
|
return $next($request);
|
2022-06-25 00:30:06 +02:00
|
|
|
}
|
|
|
|
}
|
2023-02-16 02:36:09 +01:00
|
|
|
} elseif (auth()->user()->oauth_provider_id == 'microsoft') {
|
|
|
|
try {
|
|
|
|
$payload = json_decode(base64_decode(str_replace('_', '/', str_replace('-', '+', explode('.', request()->header('X-API-OAUTH-PASSWORD'))[1]))));
|
|
|
|
} catch(\Exception $e) {
|
2022-06-25 00:39:45 +02:00
|
|
|
nlog("could not decode microsoft response");
|
|
|
|
return response()->json(['message' => 'Could not decode the response from Microsoft'], 412);
|
|
|
|
}
|
2021-03-04 06:03:28 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($payload->preferred_username == auth()->user()->email) {
|
2021-05-13 14:41:32 +02:00
|
|
|
Cache::put(auth()->user()->hashed_id.'_'.auth()->user()->account_id.'_logged_in', Str::random(64), $timeout);
|
2021-02-23 22:12:23 +01:00
|
|
|
return $next($request);
|
|
|
|
}
|
2023-02-16 02:36:09 +01:00
|
|
|
} elseif (auth()->user()->oauth_provider_id == 'apple') {
|
2022-12-19 11:07:38 +01:00
|
|
|
$user = Socialite::driver('apple')->userFromToken($request->header('X-API-OAUTH-PASSWORD'));
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($user && ($user->email == auth()->user()->email)) {
|
2022-12-19 11:07:38 +01:00
|
|
|
Cache::put(auth()->user()->hashed_id.'_'.auth()->user()->account_id.'_logged_in', Str::random(64), $timeout);
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
}
|
2021-06-09 08:22:25 +02:00
|
|
|
|
2021-02-23 22:12:23 +01:00
|
|
|
|
|
|
|
return response()->json($error, 412);
|
2023-02-16 02:36:09 +01:00
|
|
|
} elseif ($x_api_password && Hash::check($x_api_password, auth()->user()->password)) {
|
2021-05-13 14:41:32 +02:00
|
|
|
Cache::put(auth()->user()->hashed_id.'_'.auth()->user()->account_id.'_logged_in', Str::random(64), $timeout);
|
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 {
|
|
|
|
return response()->json($error, 412);
|
2019-10-22 13:27:03 +02:00
|
|
|
}
|
|
|
|
}
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|