From a08e1e8b87235885c7df365ffa99ed5737d450f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20G=C3=BCnther?= Date: Fri, 11 Feb 2022 21:35:56 +0100 Subject: [PATCH 1/2] FEATURE: Adds isAuthenticated method to the client The client always redirects to the authentication notice page when the client is not authenticated. When you are in a different context than a backend module, it is not always the best case to get the redirect. So, this method adds to check if the client is authenticated without the redirect. --- Classes/Service/CantoClient.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Classes/Service/CantoClient.php b/Classes/Service/CantoClient.php index 334534c..9d4b76c 100644 --- a/Classes/Service/CantoClient.php +++ b/Classes/Service/CantoClient.php @@ -406,4 +406,26 @@ public function sendAuthenticatedRequest(string $uriPathAndQuery, string $method return $this->httpClient->send($this->getAuthenticatedRequest($this->authorization, $uriPathAndQuery, $method, $bodyFields)); } + + /** + * Checks if the current user is authenticated and has a valid access-token. + * + * @return bool + */ + public function isAuthenticated(): bool + { + $oAuthClient = new CantoOAuthClient($this->serviceName); + + if ($this->securityContext->isInitialized()) { + $account = $this->securityContext->getAccount(); + $accountAuthorization = $account ? $this->accountAuthorizationRepository->findOneByFlowAccountIdentifier($account->getAccountIdentifier()) : null; + $authorization = $accountAuthorization instanceof AccountAuthorization ? $oAuthClient->getAuthorization($accountAuthorization->getAuthorizationId()) : null; + + if ($authorization !== null && ($authorization->getAccessToken() && !$authorization->getAccessToken()->hasExpired())) { + return true; + } + } + + return false; + } } From ba2f471ecabc7aac1142162de59e834252fa8c77 Mon Sep 17 00:00:00 2001 From: Karsten Dambekalns Date: Wed, 16 Feb 2022 22:19:12 +0100 Subject: [PATCH 2/2] Tweak method description --- Classes/Service/CantoClient.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Classes/Service/CantoClient.php b/Classes/Service/CantoClient.php index 9d4b76c..3202534 100644 --- a/Classes/Service/CantoClient.php +++ b/Classes/Service/CantoClient.php @@ -408,7 +408,9 @@ public function sendAuthenticatedRequest(string $uriPathAndQuery, string $method } /** - * Checks if the current user is authenticated and has a valid access-token. + * Checks if the current account fetched from the security context has a valid access-token. + * + * If the security context is not initialized, no account is found or no valid access-token exists, false is returned. * * @return bool */