<?php
namespace App\Services\Main;
use App\Object\Admin\User;
use App\Object\Api\CallResponse;
use App\Object\Api\Content;
use App\Services\Util\JsonService;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
class SecurityService
{
/**
* @var ApiRequestService
*/
private ApiRequestService $apiRequestService;
/**
* @var ContainerInterface
*/
private ContainerInterface $container;
/**
* @var JsonService
*/
private JsonService $jsonService;
/**
* @var Request
*/
private $request; //do not type hint
/**
* SecurityService constructor.
*
* @param ApiRequestService $apiRequestService
* @param ContainerInterface $container
* @param JsonService $jsonService
* @param RequestStack $request
*/
public function __construct(
ApiRequestService $apiRequestService,
ContainerInterface $container,
JsonService $jsonService,
RequestStack $request
) {
$this->apiRequestService = $apiRequestService;
$this->container = $container;
$this->jsonService = $jsonService;
$this->request = $request->getCurrentRequest();
}
/**
* @return mixed
*/
public function getToken()
{
return $this->request->getSession()->get('_token');
}
/**
* @param $token
*/
public function setToken($token): void
{
$this->request->getSession()->set('_token', $token);
}
/**
* @param $refreshToken
*/
public function setRefreshToken($refreshToken): void
{
$this->request->getSession()->set('_refreshToken', $refreshToken);
}
/**
* @return mixed
*/
public function getRefreshToken()
{
return $this->request->getSession()->get('_refreshToken');
}
/**
* @return bool
*/
public function refreshToken(): bool
{
$response = $this->apiRequestService->request('GET', 'token/refresh', [
'auth_bearer' => $this->getToken(),
'json' => [
'refresh_token' => $this->getRefreshToken(),
],
]);
//TODO: ITDB-502
//Carefull $apiservice->get() call refreshToken()
/*$apiService = $this->container->get(ApiService::class);
$response = $apiService->get('token/refresh', [
'auth_bearer' => $this->getToken(),
'json' => [
'refresh_token' => $this->getRefreshToken()
],
'defaultResult' => []
]);*/
if (!$response->getSuccess()) {
return false;
}
/*if ($response->getArrayResponse()['error']) {
return false;
}*/
$tokenResult = $response->getContent()->getContent();
$this->setToken($tokenResult['token']);
$this->setRefreshToken($tokenResult['refresh_token']);
return true;
}
/**
* @param array $settings
* @param string $customerId
*
* @return array|bool
*/
public function updateSSOParameters(array $settings, string $customerId)
{
$customerSettings = $this->getSSOParameters($customerId);
if (!$customerSettings) {
return false;
}
$settings['idp']['entityId'] = $customerSettings['entityId'];
$settings['idp']['x509cert'] = $customerSettings['x509cert'];
$settings['idp']['singleSignOnService']['url'] = $customerSettings['singleSignOnServiceUrl'];
$settings['idp']['singleLogoutService']['url'] = $customerSettings['singleLogoutServiceUrl'];
return $settings;
}
/**
* @param $user
* @param mixed $customerId
*
* @return array|bool
*/
public function getSSOParameters($customerId)
{
$apiService = $this->container->get('App\Services\Main\ApiService');
$response = $apiService->get("login/sso-options/{$customerId}", ['defaultResult' => []]);
if ($response->getArrayResponse()['error']) {
return false;
}
return $response->getContent();
}
/**
* @param $user
*
* @return bool|User
*/
public function refreshUser($user)
{
$apiService = $this->container->get('App\Services\Main\ApiService');
if (!$user instanceof User) {
return false;
}
$response = $apiService->get('user/me', [
'auth_bearer' => $this->getToken(),
'defaultResult' => [],
]);
$responseArray = $response->getArrayResponse();
if ($responseArray['error']) {
$resultRefreshToken = $this->refreshToken();
if (!$resultRefreshToken) {
return false;
}
return $user;
}
$userArray = $responseArray['content'];
$user->setId($userArray['id']);
$user->setUsername($userArray['login']);
$user->setLogin($userArray['login']);
$user->setCustomer($this->jsonService->denormalize(
$userArray['customer'],
'App\Object\Admin\Customer'
));
$user->setCustomerType($userArray['customer']['customer_type']);
$user->setEmail($userArray['email']);
$user->setFirstName($userArray['first_name']);
$user->setLastName($userArray['last_name']);
$user->setCustomizations($userArray['customizations']);
$user->setRole($this->jsonService->denormalize(
$userArray['role'],
'App\Object\Admin\Role'
));
$role = $userArray['role'];
$listPrivileges = array_column($role['privileges'], 'name');
$user->setPrivileges($listPrivileges);
$listModules = array_column($userArray['customer']['modules'], 'code');
$user->setModules($listModules);
$user->setUnreadNotificationsCount($userArray['unread_notifications_count']);
return $user;
}
/**
* @param $login
*
* @return array
*/
public function requestPasswordRecovery($login): array
{
$apiService = $this->container->get('App\Services\Main\ApiService');
return $apiService->post(
'login/password/recover',
[
'json' => [
'login' => $login,
],
]
)->getArrayResponse();
}
/**
* @param $privateToken
*
* @return array
*/
public function getPasswordRecoveryByKey($privateToken): array
{
$apiService = $this->container->get('App\Services\Main\ApiService');
return $apiService->get(
'login/password/recover',
[
'query' => [
'privateToken' => $privateToken,
],
]
)->getArrayResponse();
}
/**
* @param $email
* @param $login
*
* @return Content
*/
public function handleFirstLogin($email, $login): Content
{
$apiService = $this->container->get('App\Services\Main\ApiService');
return $apiService->post(
'login/first-login',
[
'json' => [
'email' => $email,
'login' => $login,
],
]
);
}
/**
* @param $login
* @param $password
*
* @throws DecodingExceptionInterface
*
* @return Content
*/
public function handleDefinePassword($login, $password): Content
{
$apiService = $this->container->get('App\Services\Main\ApiService');
return $apiService->post(
'login/define-password',
[
'json' => [
'login' => $login,
'password' => $password,
],
]
);
}
/**
* @param string $tokenSSO
*
* @return CallResponse
*/
public function logUserSSO(string $tokenSSO): CallResponse
{
$apiService = $this->container->get('App\Services\Main\ApiService');
return $apiService->post(
'login/sso',
[
'json' => [
'tokenSSO' => $tokenSSO,
],
]
);
}
/**
* @param string $type
* @param array $parameters
*
* @return array
*/
public function authWithOauth(string $type, array $parameters): array
{
$apiService = $this->container->get('App\Services\Main\ApiService');
$response = $apiService->post(
'login/oauth',
[
'json' => [
'authType' => $type,
'parameters' => $parameters,
],
'defaultResult' => [],
]
);
return $response->getContent();
}
}