Commit d8ae7a20 authored by Akshay Dalvi's avatar Akshay Dalvi Committed by Stephan Zeidler
Browse files

Issue #3295808 by akshaydalvi212, dipesh_goswami, szeidler: PHPCS Drupal...

Issue #3295808 by akshaydalvi212, dipesh_goswami, szeidler: PHPCS Drupal coding standard issue for media Fotoweb
parent ca3b9c3d
Loading
Loading
Loading
Loading
+7 −5
Original line number Diff line number Diff line
@@ -44,9 +44,10 @@ CONFIGURATION
* Go to the Fotoweb module configuration (`/admin/config/media/media-fotoweb`)
* Fill in your `Fotoweb server` and OAuth 2.0 credentials

You will need to create a "Non-interactive/script" application and another one for the "Web Application / API -> Selection Widget". The first one
will be used for backend actions like asset imports and the selection widget will make it possible for users to
authenticate with the Fotoweb Selection Widget.
You will need to create a "Non-interactive/script" application and another one
for the "Web Application / API -> Selection Widget". The first one will be used
for backend actions like asset imports and the selection widget will make it
possible for users to authenticate with the Fotoweb Selection Widget.


## File storage types
@@ -61,8 +62,9 @@ your threshold.
Legacy
-------------

The module supports the legacy "Full API Key" authentication method for on-premise installations. This authentication
method is deprecated, but can be configured on the configuration page.
The module supports the legacy "Full API Key" authentication method for
on-premise installations. This authentication method is deprecated, but
can be configured on the configuration page.

## Fotoweb Single Sign-on (Legacy)

+0 −2
Original line number Diff line number Diff line
@@ -27,8 +27,6 @@ function media_fotoweb_update_8001(&$sandbox) {
  }
}



/**
 * Enable the legacy "Full API Key" authentication method for existing pages.
 */
+66 −10
Original line number Diff line number Diff line
@@ -2,16 +2,69 @@

namespace Drupal\media_fotoweb\Controller;

use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Drupal\Core\Url;
use Drupal\language\Entity\ConfigurableLanguage;
use kamermans\OAuth2\Exception\OAuth2Exception;
use Drupal\media_fotoweb\FotowebClient;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;

/**
 * AuthenticationController for authentication of user in fotoweb.
 */
class AuthenticationController extends ControllerBase {

  /**
   * The tempstore service.
   *
   * @var \Drupal\Core\TempStore\PrivateTempStoreFactory
   */
  protected $tempStore;

  /**
   * The client service.
   *
   * @var Drupal\media_fotoweb\FotowebClient
   */
  protected $client;

  /**
   * User token service.
   *
   * @var Drupal\media_fotoweb\OAuth2\Persistence\UserTokenPersistence
   */
  protected $userTokenPersistence;

  /**
   * Constructs a service instance.
   *
   * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store
   *   The tempstore service.
   * @param \Drupal\media_fotoweb\FotowebClient $client
   *   The client service.
   * @param \Drupal\media_fotoweb\OAuth2\Persistence\UserTokenPersistence $user_token_persistence
   *   The user token persistence service.
   */
  public function __construct(PrivateTempStoreFactory $temp_store, FotowebClient $client, UserTokenPersistence $user_token_persistence) {
    $this->tempStore = $temp_store;
    $this->client = $client;
    $this->userTokenPersistence = $user_token_persistence;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('tempstore.private'),
      $container->get('media_fotoweb.client'),
      $container->get('media_fotoweb.user_token_persistence')
    );
  }

  /**
   * Handles the oAuth2 user authentication.
   *
@@ -24,7 +77,7 @@ class AuthenticationController extends ControllerBase {
  public function authenticateUser(Request $request) {
    if ($code = $request->get('code')) {
      // Load in security verifiers.
      $tempstore = \Drupal::service('tempstore.private')->get('media_fotoweb');
      $tempstore = $this->tempStore->get('media_fotoweb');
      $state = $tempstore->get('state');
      $codeVerifier = $tempstore->get('code_verifier');

@@ -37,7 +90,7 @@ class AuthenticationController extends ControllerBase {

      // Make a token request using the code.
      /** @var \Drupal\media_fotoweb\FotowebClient $client */
      $client = \Drupal::service('media_fotoweb.client');
      $client = $this->client;
      $clientConfiguration = $client->getConfiguration();
      $clientConfiguration['grantType'] = 'authorization_code';
      $clientConfiguration['authorizationCode'] = $code;
@@ -72,9 +125,7 @@ class AuthenticationController extends ControllerBase {
   *   The response.
   */
  public function revokeAuthenticateUser(Request $request) {
    /** @var \Drupal\media_fotoweb\OAuth2\Persistence\UserTokenPersistence $tokenPersistence */
    $tokenPersistence = \Drupal::service('media_fotoweb.user_token_persistence');
    $tokenPersistence->deleteToken();
    $this->userTokenPersistence->deleteToken();
    $this->messenger()->addStatus($this->t('Your account got disconnected from Fotoware. Please reload the widget.'));
    return new RedirectResponse('/');
  }
@@ -89,7 +140,10 @@ class AuthenticationController extends ControllerBase {
    // Create a fake language for generating a redirect url without language
    // prefixes.
    $language = ConfigurableLanguage::createFromLangcode('und');
    $url = Url::fromRoute('media_fotoweb.oauth2_callback', [], ['absolute' => TRUE, 'language' => $language]);
    $url = Url::fromRoute('media_fotoweb.oauth2_callback', [], [
      'absolute' => TRUE,
      'language' => $language,
    ]);
    return $url->toString(TRUE)->getGeneratedUrl();
  }

@@ -98,6 +152,7 @@ class AuthenticationController extends ControllerBase {
   *
   * @return string
   *   The authorization URL.
   *
   * @throws \Exception
   */
  protected function getAuthorizationUrl() {
@@ -111,7 +166,7 @@ class AuthenticationController extends ControllerBase {
    $codeChallenge = $this->generatePkceCodeChallenge($codeVerifier);

    // Store verifiers in the user temp sore to be able to verify them.
    $tempstore = \Drupal::service('tempstore.private')->get('media_fotoweb');
    $tempstore = $this->tempStore->get('media_fotoweb');
    $tempstore->set('state', $state);
    $tempstore->set('code_verifier', $codeVerifier);

@@ -132,6 +187,7 @@ class AuthenticationController extends ControllerBase {
   *
   * @return string
   *   The verifier.
   *
   * @throws \Exception
   */
  protected function generatePkceCodeVerifier() {
@@ -150,7 +206,7 @@ class AuthenticationController extends ControllerBase {
   *   The code challenge.
   */
  protected function generatePkceCodeChallenge($codeVerifier) {
    $challengeBytes = hash('sha256', $codeVerifier, true);
    $challengeBytes = hash('sha256', $codeVerifier, TRUE);
    $codeChallenge = rtrim(strtr(base64_encode($challengeBytes), '+/', '-_'), '=');
    return $codeChallenge;
  }
+1 −4
Original line number Diff line number Diff line
@@ -5,8 +5,6 @@ namespace Drupal\media_fotoweb\Form;
use Drupal\Core\Ajax\CloseDialogCommand;
use Drupal\Core\Ajax\InvokeCommand;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\ContentEntityFormInterface;
use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormStateInterface;
@@ -150,13 +148,12 @@ class FotowebBrowserForm extends AddFormBase {
        ],
      ],
      '#attributes' => [
        'class' => ['visually-hidden']
        'class' => ['visually-hidden'],
      ],
    ];
    return $actions;
  }


  /**
   * {@inheritdoc}
   */
+28 −10
Original line number Diff line number Diff line
@@ -6,12 +6,13 @@ use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\media_fotoweb\FotowebClient;
use Drupal\media_fotoweb\ImageFetcherManager;
use Drupal\media_fotoweb\OAuth2\Persistence\ApiTokenPersistence;
use GuzzleHttp\Command\Exception\CommandException;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
@@ -19,6 +20,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
 */
class FotowebSettingsForm extends ConfigFormBase {

  use StringTranslationTrait;

  /**
   * The entity field manager.
   *
@@ -40,6 +43,13 @@ class FotowebSettingsForm extends ConfigFormBase {
   */
  protected $imageFetcherManager;

  /**
   * The Api Token Persistence.
   *
   * @var \Drupal\media_fotoweb\OAuth2\Persistence\ApiTokenPersistence
   */
  protected $apiTokenPersistence;

  /**
   * Constructs a FotowebSettingsForm object.
   *
@@ -51,12 +61,15 @@ class FotowebSettingsForm extends ConfigFormBase {
   *   The Fotoweb client.
   * @param \Drupal\media_fotoweb\ImageFetcherManager $image_fetcher_manager
   *   The image fetcher manager.
   * @param \Drupal\media_fotoweb\OAuth2\Persistence\ApiTokenPersistence $api_token_persistence
   *   The api token persistence.
   */
  public function __construct(ConfigFactoryInterface $config_factory, EntityFieldManagerInterface $entity_field_manager, FotowebClient $fotoweb_client, ImageFetcherManager $image_fetcher_manager) {
  public function __construct(ConfigFactoryInterface $config_factory, EntityFieldManagerInterface $entity_field_manager, FotowebClient $fotoweb_client, ImageFetcherManager $image_fetcher_manager, ApiTokenPersistence $api_token_persistence) {
    parent::__construct($config_factory);
    $this->entityFieldManager = $entity_field_manager;
    $this->fotowebClient = $fotoweb_client;
    $this->imageFetcherManager = $image_fetcher_manager;
    $this->apiTokenPersistence = $api_token_persistence;
  }

  /**
@@ -67,7 +80,8 @@ class FotowebSettingsForm extends ConfigFormBase {
      $container->get('config.factory'),
      $container->get('entity_field.manager'),
      $container->get('media_fotoweb.client'),
      $container->get('plugin.manager.media_fotoweb.image_fetcher')
      $container->get('plugin.manager.media_fotoweb.image_fetcher'),
      $container->get('media_fotoweb.api_token_persistence')
    );
  }

@@ -141,7 +155,15 @@ class FotowebSettingsForm extends ConfigFormBase {
    $form['selection_widget_client_id'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Client ID (Selection Widget)'),
      '#description' => $this->t('The Client ID for a Fotoweb <a href=":url" target="_blank">Web API Selection Widget application</a>. You will need to specify a callback URL: <em>:callback_url</em>', [':url' => 'https://learn.fotoware.com/Integrations_and_APIs/Authorizing_applications_using_OAuth/02_Application_registration_using_OAuth_2.0', ':callback_url' => 'https://yoursite.com/media-fotoweb/oauth2/selection/callback']),
      '#description' => $this->t(
        'The Client ID for a Fotoweb 
        <a href=":url" target="_blank">Web API Selection Widget application</a>. 
        You will need to specify a callback URL: <em>:callback_url</em>',
        [
          ':url' =>
          'https://learn.fotoware.com/Integrations_and_APIs/Authorizing_applications_using_OAuth/02_Application_registration_using_OAuth_2.0',
          ':callback_url' => 'https://yoursite.com/media-fotoweb/oauth2/selection/callback',
        ]),
      '#default_value' => $config->get('selection_widget_client_id'),
      '#states' => [
        'visible' => [
@@ -333,7 +355,7 @@ class FotowebSettingsForm extends ConfigFormBase {
    catch (CommandException $e) {
      $response = $e->getResponse();
      if ($response && $response->getStatusCode() == 401) {
        $errorMessage = t('The request has not been authorized. Please authenticate the API below.');
        $errorMessage = $this->t('The request has not been authorized. Please authenticate the API below.');
      }
      else {
        $errorMessage = $e->getMessage();
@@ -359,13 +381,9 @@ class FotowebSettingsForm extends ConfigFormBase {
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   *
   * @return void
   */
  public function submitRevokeApiAuthorize(array &$form, FormStateInterface $form_state) {
    /** @var \Drupal\media_fotoweb\OAuth2\Persistence\ApiTokenPersistence $tokenPersistence */
    $tokenPersistence = \Drupal::service('media_fotoweb.api_token_persistence');
    $tokenPersistence->deleteToken();
    $this->apiTokenPersistence->deleteToken();
    $this->messenger()->addStatus($this->t('Your site got disconnected from Fotoware.'));
  }

Loading