mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/shipping-portal.git
synced 2026-08-19 04:24:12 +00:00
73 lines
2.3 KiB
PHP
73 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
use App\Classes\Exceptions\AccessUnauthorisedException;
|
|
use App\Classes\Modules\Accounts\Services\GeneratesAuthenticationToken;
|
|
use App\Classes\ValueObjects\Constants\HttpStatus;
|
|
use App\Classes\ValueObjects\Response\ApiResponseObject;
|
|
use App\Models\ExchangeCompanyConnection;
|
|
use App\Models\User;
|
|
use Closure;
|
|
use Exception;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Tymon\JWTAuth\JWT;
|
|
|
|
class ValidateExchangeToken
|
|
{
|
|
/** @var JWT */
|
|
private $manager;
|
|
|
|
/** @var GeneratesAuthenticationToken */
|
|
private $generatesAuthenticationToken;
|
|
|
|
|
|
/**
|
|
* ValidateExchangeToken constructor.
|
|
* @param JWT $manager
|
|
* @param GeneratesAuthenticationToken $generatesAuthenticationToken
|
|
*/
|
|
public function __construct(JWT $manager, GeneratesAuthenticationToken $generatesAuthenticationToken)
|
|
{
|
|
$this->manager = $manager;
|
|
$this->generatesAuthenticationToken = $generatesAuthenticationToken;
|
|
}
|
|
|
|
|
|
/**
|
|
* Checks if jwt token is valid.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param \Closure $next
|
|
* @return mixed
|
|
*/
|
|
public function handle($request, Closure $next)
|
|
{
|
|
try {
|
|
|
|
$token = explode('.', $this->manager->getToken())[1];
|
|
$decodedToken = base64_decode($token);
|
|
$exchangeUser = json_decode($decodedToken, true)['user'];
|
|
if ($exchangeUser['type'] === 3) {
|
|
$exchangeCompanyId = $exchangeUser['company_id'];
|
|
$connection = ExchangeCompanyConnection::where("exchange_company_id", $exchangeCompanyId)->first();
|
|
|
|
Log::info($request->url());
|
|
Log::info(route('api.exchange.company.connect' , $request->route('exchange_company_id')));
|
|
|
|
if(!$connection && $request->url() !== route('api.exchange.company.connect' , $request->route('exchange_company_id'))){
|
|
throw new AccessUnauthorisedException();
|
|
}
|
|
}
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
return (new ApiResponseObject('Authentication', 'To keep your account secure we need to re-validate your account', HttpStatus::ACCESS_UNAUTHORISED))->handler();
|
|
|
|
}
|
|
|
|
return $next($request);
|
|
}
|
|
}
|