Skip to content

Commit

Permalink
Merge pull request #26 from markusguenther/feature/isAuthenticatedMethod
Browse files Browse the repository at this point in the history
Add isAuthenticated() method to the CantoClient
  • Loading branch information
kdambekalns authored Sep 7, 2022
2 parents ed13210 + ba2f471 commit f7a973d
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Classes/Service/CantoClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,4 +406,28 @@ public function sendAuthenticatedRequest(string $uriPathAndQuery, string $method

return $this->httpClient->send($this->getAuthenticatedRequest($this->authorization, $uriPathAndQuery, $method, $bodyFields));
}

/**
* 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
*/
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;
}
}

0 comments on commit f7a973d

Please sign in to comment.