Skip to content

Commit f7a973d

Browse files
authored
Merge pull request #26 from markusguenther/feature/isAuthenticatedMethod
Add isAuthenticated() method to the CantoClient
2 parents ed13210 + ba2f471 commit f7a973d

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Classes/Service/CantoClient.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,4 +406,28 @@ public function sendAuthenticatedRequest(string $uriPathAndQuery, string $method
406406

407407
return $this->httpClient->send($this->getAuthenticatedRequest($this->authorization, $uriPathAndQuery, $method, $bodyFields));
408408
}
409+
410+
/**
411+
* Checks if the current account fetched from the security context has a valid access-token.
412+
*
413+
* If the security context is not initialized, no account is found or no valid access-token exists, false is returned.
414+
*
415+
* @return bool
416+
*/
417+
public function isAuthenticated(): bool
418+
{
419+
$oAuthClient = new CantoOAuthClient($this->serviceName);
420+
421+
if ($this->securityContext->isInitialized()) {
422+
$account = $this->securityContext->getAccount();
423+
$accountAuthorization = $account ? $this->accountAuthorizationRepository->findOneByFlowAccountIdentifier($account->getAccountIdentifier()) : null;
424+
$authorization = $accountAuthorization instanceof AccountAuthorization ? $oAuthClient->getAuthorization($accountAuthorization->getAuthorizationId()) : null;
425+
426+
if ($authorization !== null && ($authorization->getAccessToken() && !$authorization->getAccessToken()->hasExpired())) {
427+
return true;
428+
}
429+
}
430+
431+
return false;
432+
}
409433
}

0 commit comments

Comments
 (0)