src/EventSubscriber/AuthenticationToken/AuthenticationTokenSubscriber.php line 260

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\AuthenticationToken;
  3. use App\Entity\Referral;
  4. use App\Service\AuthenticationTokenService;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Symfony\Component\HttpKernel\Event\RequestEvent;
  7. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  8. use Doctrine\Persistence\ManagerRegistry;
  9. use Psr\Log\LoggerInterface;
  10. use Symfony\Component\HttpFoundation\Exception\BadRequestException;
  11. class AuthenticationTokenSubscriber
  12. {
  13. protected $doctrine;
  14. protected $router;
  15. /** @var AuthenticationTokenService $tokenService **/
  16. protected $tokenService;
  17. protected $authenticationKey;
  18. protected $authenticationSecret;
  19. private $logger;
  20. public function __construct(ManagerRegistry $doctrine, UrlGeneratorInterface $router, AuthenticationTokenService $tokenService, $authenticationKey, $authenticationSecret, LoggerInterface $logger)
  21. {
  22. $this->doctrine = $doctrine;
  23. $this->router = $router;
  24. $this->tokenService = $tokenService;
  25. $this->authenticationKey = $authenticationKey;
  26. $this->authenticationSecret = $authenticationSecret;
  27. $this->logger = $logger;
  28. }
  29. protected function checkTokenReview(RequestEvent $event)
  30. {
  31. $request = $event->getRequest();
  32. $checkPath = '/api/authentication_tokens/review';
  33. if ($request->getRequestUri() !== $checkPath || $request->getMethod() !== 'POST') {
  34. return false;
  35. }
  36. $headers = $request->headers;
  37. $authenticationTokenUid = $headers->get('authorization', null);
  38. if (! $authenticationTokenUid) {
  39. return false;
  40. }
  41. $token = $this->tokenService->getTokenByUid($authenticationTokenUid);
  42. if (! $token) {
  43. return false;
  44. }
  45. if ($token->getAccessTimestamp()) {
  46. return false;
  47. }
  48. $uid = $token->getUid();
  49. $url = $this->router->generate('views_external_referral_review', [
  50. 'token' => $uid
  51. ], UrlGeneratorInterface::ABSOLUTE_URL);
  52. $response = new JsonResponse([
  53. 'url' => $url
  54. ]);
  55. $event->setResponse($response);
  56. return true;
  57. }
  58. protected function checkTokenRequest(RequestEvent $event)
  59. {
  60. $request = $event->getRequest();
  61. $checkPath = '/api/authentication_tokens/request';
  62. if ($request->getRequestUri() !== $checkPath || $request->getMethod() !== 'POST') {
  63. return false;
  64. }
  65. $content = json_decode($request->getContent(), true);
  66. try {
  67. $key = $content['key'];
  68. $secret = $content['secret'];
  69. $referralId = $content['referral'];
  70. $userData = $content['user'];
  71. } catch (\Exception $ex) {
  72. throw new BadRequestException("Invalid request data");
  73. }
  74. $systemKey = $this->authenticationKey;
  75. $systemSecret = $this->authenticationSecret;
  76. if (! $key) {
  77. throw new BadRequestException("Authentication key must be submitted");
  78. }
  79. if (! $secret) {
  80. throw new BadRequestException("Authentication secret must be submitted");
  81. }
  82. if (! $referralId) {
  83. throw new BadRequestException("Authentication referral must be submitted");
  84. }
  85. if (! $userData) {
  86. throw new BadRequestException("Authentication user must be submitted");
  87. }
  88. if ($key !== $systemKey || $secret !== $systemSecret) {
  89. throw new BadRequestException("Could not authenticate");
  90. }
  91. $referralRepository = $this->doctrine->getRepository(Referral::class);
  92. $referral = null;
  93. try {
  94. $referral = $referralRepository->findOneBy([
  95. 'submissionId' => $referralId
  96. ]);
  97. } catch (\Exception $ex) {}
  98. if (! $referral) {
  99. throw new BadRequestException("Invalid referral submitted");
  100. }
  101. $clientIps = $request->headers->get('x-forwarded-for') ?: json_encode($request->getClientIps());
  102. $requestorInformation = $clientIps;
  103. $token = $this->tokenService->generateToken($referral, $requestorInformation, $userData);
  104. $response = new JsonResponse([
  105. 'uid' => $token->getUid()
  106. ]);
  107. $event->setResponse($response);
  108. return true;
  109. }
  110. protected function checkTokenStatusUpdate(RequestEvent $event)
  111. {
  112. $request = $event->getRequest();
  113. $checkPaths = [
  114. '/api/authentication_tokens/received',
  115. '/api/authentication_tokens/processed',
  116. '/api/authentication_tokens/eligible',
  117. '/api/authentication_tokens/not_eligible',
  118. '/api/authentication_tokens/rejected',
  119. '/api/authentication_tokens/unable_to_contact'
  120. ];
  121. if (! in_array($request->getRequestUri(), $checkPaths) || $request->getMethod() !== 'POST') {
  122. return false;
  123. }
  124. $content = json_decode($request->getContent(), true);
  125. $comments = null;
  126. $serviceCoordinator = null;
  127. $serviceCoordinatorEs = null;
  128. $eligibilityDetermination = null;
  129. $eligibilityDeterminationEs = null;
  130. $statusDetails = null;
  131. $statusDetailsEs = null;
  132. $status = null;
  133. switch ($request->getRequestUri()) {
  134. case '/api/authentication_tokens/received':
  135. $status = Referral::STATUS_RECEIVED;
  136. break;
  137. case '/api/authentication_tokens/processed':
  138. $status = Referral::STATUS_PROCESSED;
  139. break;
  140. case '/api/authentication_tokens/eligible':
  141. $status = Referral::STATUS_ELIGIBLE;
  142. $serviceCoordinator = $content['service_coordinator'] ?? null;
  143. $serviceCoordinatorEs = $content['service_coordinator_es'] ?? null;
  144. $eligibilityDetermination = $content['eligibility_determination'] ?? null;
  145. $eligibilityDeterminationEs = $content['eligibility_determination_es'] ?? null;
  146. $statusDetails = $content['status_details'] ?? null;
  147. $statusDetailsEs = $content['status_details_es'] ?? null;
  148. break;
  149. case '/api/authentication_tokens/not_eligible':
  150. $status = Referral::STATUS_NOT_ELIGIBLE;
  151. $serviceCoordinator = $content['service_coordinator'] ?? null;
  152. $serviceCoordinatorEs = $content['service_coordinator_es'] ?? null;
  153. break;
  154. case '/api/authentication_tokens/rejected':
  155. $status = Referral::STATUS_REJECTED;
  156. $comments = $content['comments'] ?? null;
  157. break;
  158. case '/api/authentication_tokens/unable_to_contact':
  159. $status = Referral::STATUS_UNABLE_TO_CONTACT;
  160. $serviceCoordinator = $content['service_coordinator'] ?? null;
  161. $serviceCoordinatorEs = $content['service_coordinator_es'] ?? null;
  162. $statusDetails = $content['status_details'] ?? null;
  163. $statusDetailsEs = $content['status_details_es'] ?? null;
  164. break;
  165. }
  166. $headers = $request->headers;
  167. $authenticationTokenUid = $headers->get('authorization', null);
  168. if (! $authenticationTokenUid) {
  169. return false;
  170. }
  171. $token = $this->tokenService->getTokenByUid($authenticationTokenUid);
  172. if (! $token) {
  173. return false;
  174. }
  175. if ($token->getExpirationTimestamp()) {
  176. return false;
  177. }
  178. $clientIps = $request->headers->get('x-forwarded-for') ?: json_encode($request->getClientIps());
  179. $accessorInformation = $clientIps;
  180. $accessTimestamp = new \DateTime("now");
  181. $referral = $token->getReferral();
  182. $token->setAccessorInformation($accessorInformation);
  183. $token->setAccessTimestamp($accessTimestamp);
  184. $token->setExpirationTimestamp($accessTimestamp);
  185. $referral->setStatus($status);
  186. $referral->setComments($comments);
  187. $referral->setServiceCoordinator($serviceCoordinator);
  188. $referral->setServiceCoordinatorEs($serviceCoordinatorEs);
  189. $referral->setEligibilityDetermination($eligibilityDetermination);
  190. $referral->setEligibilityDeterminationEs($eligibilityDeterminationEs);
  191. $referral->setStatusDetails($statusDetails);
  192. $referral->setStatusDetailsEs($statusDetailsEs);
  193. $this->doctrine->getManager()->persist($token);
  194. $this->doctrine->getManager()->persist($referral);
  195. $this->doctrine->getManager()->flush();
  196. $response = new JsonResponse([
  197. 'status' => $referral->getStatus()
  198. ]);
  199. $event->setResponse($response);
  200. return true;
  201. }
  202. public function onKernelRequest(RequestEvent $event)
  203. {
  204. if ($this->checkTokenRequest($event)) {
  205. return;
  206. }
  207. if ($this->checkTokenReview($event)) {
  208. return;
  209. }
  210. if ($this->checkTokenStatusUpdate($event)) {
  211. return;
  212. }
  213. return;
  214. }
  215. }