/**
  * @throws AbortException
  */
 public function actionAuthorize()
 {
     if (!$this->getHttpRequest()->isMethod(IRequest::GET)) {
         $body = $this->createStream();
         $body->write('Method not allowed');
         $this->sendResponse($this->createResponse()->withStatus(IResponse::S405_METHOD_NOT_ALLOWED)->withBody($body));
     }
     $response = $this->createResponse();
     try {
         $this->getSession(self::SESSION_NAMESPACE)->authorizationRequest = $this->authorizationRequestSerializer->serialize($this->authorizationServer->validateAuthorizationRequest($this->createServerRequest()));
         if (!$this->getUser()->isLoggedIn()) {
             $this->redirect(...$this->redirectConfig->getLoginDestination());
         }
         $this->redirect(...$this->redirectConfig->getApproveDestination());
     } catch (AbortException $e) {
         throw $e;
     } catch (OAuthServerException $e) {
         $this->sendResponse($e->generateHttpResponse($response));
     } catch (\Exception $e) {
         if ($this->logger) {
             $this->logger->error($e->getMessage(), ['exception' => $e]);
         }
         $body = $this->createStream();
         $body->write('Unknown error');
         $this->sendResponse($response->withStatus(IResponse::S500_INTERNAL_SERVER_ERROR)->withBody($body));
     }
 }
 /**
  * @param User $user
  * @throws InvalidStateException
  * @throws AbortException
  */
 public function onLoggedIn(User $user)
 {
     if (!$this->presenter) {
         throw new InvalidStateException('Presenter not set');
     }
     if ($this->presenter instanceof Presenter && $this->presenter->getSession(OAuth2Presenter::SESSION_NAMESPACE)->authorizationRequest) {
         $this->presenter->redirect(...$this->redirectConfig->getApproveDestination());
     }
 }
 /**
  * @return ApproveControl
  * @throws AbortException
  * @throws BadRequestException
  */
 protected function createComponentApprove() : ApproveControl
 {
     if (!$this->getUser()->isLoggedIn()) {
         $this->redirect(...$this->redirectConfig->getLoginDestination());
     }
     /** @var string $data */
     $data = $this->getSession(OAuth2Presenter::SESSION_NAMESPACE)->authorizationRequest;
     $authorizationRequest = $data ? $this->authorizationRequestSerializer->unserialize($data) : null;
     if ($authorizationRequest) {
         if (!$authorizationRequest->getUser()) {
             $authorizationRequest->setUser(new UserEntity($this->getUser()->getId()));
         }
         $control = $this->approveControlFactory->create($authorizationRequest);
         $control->onResponse[] = function (ApplicationPsr7ResponseInterface $response) {
             $this->sendResponse($response);
         };
         return $control;
     }
     $this->error(null, HttpResponse::S400_BAD_REQUEST);
 }