webex_client-1.0.5/src/Controller/OAuthCallbackController.php

src/Controller/OAuthCallbackController.php
<?php

declare(strict_types=1);

namespace Drupal\webex_client\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Url;
use Drupal\webex_client\AuthServiceInterface;
use Drupal\webex_client\Entity\Webex;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;

/**
 * Returns responses for webex routes.
 *
 * This controller handles the OAuth callback process for Webex, including
 * error handling and successful login responses. It is responsible for
 * redirecting users based on the outcome of the OAuth authentication process.
 */
final class OAuthCallbackController extends ControllerBase {

  /**
   * Constructs a new OAuthCallbackController object.
   *
   * @param \Drupal\webex_client\AuthServiceInterface $webexAuth
   *   The authentication service for Webex.
   */
  public function __construct(
    private readonly AuthServiceInterface $webexAuth,
  ) {}

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container): self {
    return new self(
      $container->get('webex_client.auth'),
    );
  }

  /**
   * Builds the response for the OAuth callback.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The HTTP request object containing the query parameters.
   *
   * @return \Symfony\Component\HttpFoundation\RedirectResponse
   *   A redirect response to the appropriate URL.
   *
   * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
   */
  public function __invoke(Request $request): RedirectResponse {
    $state = $request->query->get('state');
    // Handle OAuth errors.
    if ($request->query->has('error')) {
      $error = $request->query->get('error');
      $this->getLogger('webex_client')->error('OAuth error: @error', [
        '@error' => $request->query->get('error_description', $error),
      ]);

      $this->messenger()->addError(
        $this->t('Failed to authenticate with Webex. Please review your settings and try again.')
      );

      $params = [];
      $routeName = 'entity.webex.add_form';
      if ($state && (($webex = Webex::load($state)) && !$webex->isNew())) {
        $routeName = 'entity.webex.edit_form';
        $params['webex'] = $webex->id();
      }

      $callback_url = Url::fromRoute($routeName, $params, [
        'query' => ['error' => $error],
      ])->toString();
      return new RedirectResponse($callback_url);
    }

    // Validate required parameter.
    $code = $request->query->get('code');

    // Process token exchange.
    if (!$this->webexAuth->accessToken($code, $state)) {
      $this->getLogger('webex_client')->error('Token exchange failed');
      $this->messenger()->addError(
        $this->t('Failed to authenticate with Webex. Please try again.')
      );
    }
    else {
      $this->getLogger('webex_client')->info('Authenticated with Webex.');
      $this->messenger()->addMessage(
        $this->t('Successfully authenticated with Webex.')
      );
    }

    return new RedirectResponse(
      Url::fromRoute('entity.webex.collection')->setAbsolute()->toString()
    );
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc