webex_client-1.0.5/src/AuthService.php
src/AuthService.php
<?php
declare(strict_types=1);
namespace Drupal\webex_client;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Logger\LoggerChannelTrait;
use Drupal\Core\Url;
use Drupal\webex_client\Entity\Webex;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;
/**
* AuthService is responsible for handling authentication with the Webex API.
*/
final class AuthService implements AuthServiceInterface {
use LoggerChannelTrait;
/**
* Constructs an AuthService object.
*
* @param \GuzzleHttp\ClientInterface $httpClient
* The HTTP client used for making requests.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager for managing entities.
*/
public function __construct(
private readonly ClientInterface $httpClient,
private readonly EntityTypeManagerInterface $entityTypeManager,
) {
}
/**
* {@inheritdoc}
*/
public function accessToken(string $code, string $wId): false|string {
$webex = Webex::load($wId);
if ($webex && $token = $this->getToken(self::WEBEX_AUTH_CODE, $webex, $code)) {
$webex->setCode($code)->setStatus(1)->save();
return $token;
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function refreshToken(): false|string {
if ($webex = WebexHelper::defaultConfig()) {
return $this->getToken(self::WEBEX_REFRESH_TOKEN, $webex);
}
return FALSE;
}
/**
* Retrieves an access token from the Webex API.
*
* This method handles the retrieval of access tokens using different
* grant types, including authorization code and refresh token.
*
* @param string $grant_type
* The type of grant being used (e.g., authorization code, refresh token).
* @param \Drupal\webex_client\WebexInterface $webex
* The Webex service interface for accessing client credentials.
* @param string $code
* Optional authorization code when using the authorization code grant type.
*
* @return false|mixed
* Returns the access token on success, or FALSE on failure.
*/
protected function getToken(string $grant_type, WebexInterface $webex, string $code = ''): false|string {
$redirect_uri = Url::fromRoute('webex_client.callback')
->setAbsolute()
->toString();
// Construct the parameters for the token request.
$form_params = [
'grant_type' => $grant_type,
'client_id' => $webex->getClientId(),
'client_secret' => $webex->getClientSecret(),
'redirect_uri' => $redirect_uri,
];
if (!empty($code) && $grant_type === self::WEBEX_AUTH_CODE) {
$form_params['code'] = $code;
}
if ($grant_type === self::WEBEX_REFRESH_TOKEN) {
$form_params['refresh_token'] = $webex->getRefreshToken();
}
// Send the request to the Webex API to retrieve the access token.
try {
$response = $this->httpClient->post('https://webexapis.com/v1/access_token', [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8',
],
'form_params' => $form_params,
]);
$data = Json::decode($response->getBody()->getContents());
// Store the access token and related data in the Webex service interface.
$webex->setAccessToken($data['access_token'])
->setExpiresIn($data['expires_in'])
->setRefreshToken($data['refresh_token'])
->setRefreshTokenExpiresIn($data['refresh_token_expires_in'])
->setTokenType($data['token_type'])
->save();
return $data['access_token'];
}
catch (GuzzleException | EntityStorageException $e) {
$this->getLogger('webex_client')->error('Error occurred: @error', [
'@error' => $e->getMessage(),
]);
}
return FALSE;
}
}
