mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-19 12:33:56 +00:00
58 lines
1.6 KiB
PHP
58 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
use App\Classes\Exceptions\AccessUnauthorisedException;
|
|
use App\Classes\ValueObjects\Constants\HttpStatus;
|
|
use App\Classes\ValueObjects\Response\ApiResponseObject;
|
|
use App\Models\ShippingCompanyConnection;
|
|
use Closure;
|
|
use Exception;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Tymon\JWTAuth\JWT;
|
|
|
|
class ValidateShippingToken
|
|
{
|
|
/** @var JWT */
|
|
private $manager;
|
|
|
|
/**
|
|
* ValidateShippingToken constructor.
|
|
* @param JWT $manager
|
|
*/
|
|
public function __construct(JWT $manager)
|
|
{
|
|
$this->manager = $manager;
|
|
}
|
|
|
|
|
|
/**
|
|
* 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);
|
|
$shippingUser = json_decode($decodedToken, true)['user'];
|
|
if ($shippingUser['type'] === 3) {
|
|
$shippingCompanyModuleId = $shippingUser['company_module_id'];
|
|
$connection = ShippingCompanyConnection::where("shipping_company_module_id", $shippingCompanyModuleId)->first();
|
|
|
|
if(!$connection){ 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);
|
|
}
|
|
}
|