mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/shipping-portal.git
synced 2026-08-19 04:24:12 +00:00
46 lines
1.2 KiB
PHP
46 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Classes\Modules\Unity\Services;
|
|
|
|
use Config;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
class GetAccessToken
|
|
{
|
|
private $getAccessTokenEndpoint;
|
|
|
|
private $username;
|
|
|
|
private $password;
|
|
|
|
private $apiKey;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->getAccessTokenEndpoint = Config::get('unity.url').Config::get('unity.endpoint.login');
|
|
$this->username = Config::get('unity.auth.login.username');
|
|
$this->password = Config::get('unity.auth.login.password');
|
|
$this->apiKey = Config::get('unity.auth.api_key.key');
|
|
}
|
|
|
|
public function execute(){
|
|
|
|
//Note: Cache access token base on expiry from login not ideal as token may gets overwritten if user login Unity dashboard
|
|
|
|
$accessToken='';
|
|
if(empty($this->apiKey)){
|
|
$response = Http::post($this->getAccessTokenEndpoint, [
|
|
'email' => $this->username,
|
|
'password' => $this->password ,
|
|
])->object();
|
|
$accessToken = $response->access_token;
|
|
}else{
|
|
$accessToken = $this->apiKey;
|
|
}
|
|
|
|
//TODO: Throw error if access token not available
|
|
|
|
return $accessToken;
|
|
}
|
|
}
|