You are here

OauthInteractions.php in FormAssembly 8

File

src/Controller/OauthInteractions.php
View source
<?php

namespace Drupal\formassembly\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Fathershawn\OAuth2\Client\Provider\FormAssembly as OauthProvider;

/**
 * Utility class for interacting with FormAssembly for authorization.
 *
 * @author Shawn P. Duncan <code@sd.shawnduncan.org>
 *
 * Copyright 2018 by Shawn P. Duncan.  This code is
 * released under the GNU General Public License.
 * Which means that it is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 * http://www.gnu.org/licenses/gpl.html
 * @package Drupal\formassembly
 */
class OauthInteractions extends ControllerBase {

  /**
   * Injected service.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $currentRequest;

  /**
   * Injected service.
   *
   * @var \Drupal\formassembly\ApiAuthorize
   */
  protected $apiAuthorize;

  /**
   * Injected service.
   *
   * @var \Drupal\Core\Messenger\Messenger
   */
  protected $messenger;

  /**
   * Service to obtain oauth credentials.
   *
   * @var \Drupal\formassembly\FormAssemblyKeyService
   */
  protected $keyService;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    $instance = parent::create($container);
    $requestStack = $container
      ->get('request_stack');
    $instance->currentRequest = $requestStack
      ->getCurrentRequest();
    $instance->apiAuthorize = $container
      ->get('formassembly.authorize');
    $instance->messenger = $container
      ->get('messenger');
    $instance->keyService = $container
      ->get('formassembly.key');
    return $instance;
  }

  /**
   * Assemble the authorization url and redirect to it.
   */
  public function authorize() {

    /** @var \Drupal\Core\Config\ImmutableConfig $formassembly_config */
    $credentials = $this->keyService
      ->getOauthKeys();
    $provider = new OauthProvider([
      'clientId' => $credentials['cid'],
      'clientSecret' => $credentials['secret'],
      'redirectUri' => Url::fromRoute('fa_form.authorize.store', [], [
        'absolute' => TRUE,
      ])
        ->toString(TRUE)
        ->getGeneratedUrl(),
      'baseUrl' => $this->apiAuthorize
        ->getUrl('base')
        ->toString(TRUE)
        ->getGeneratedUrl(),
    ]);
    $url = $provider
      ->getAuthorizationUrl();
    $response = new TrustedRedirectResponse($url);
    $response
      ->addCacheableDependency($url);
    return $response;
  }

  /**
   * Capture the authorization code and trigger token request.
   *
   * @throws \Exception
   *   Re-throws any caught exception after sending a message to the user.
   *
   * @return \Symfony\Component\HttpFoundation\RedirectResponse
   *   Redirect to the settings form.
   */
  public function code() {
    try {
      $code = $this->currentRequest->query
        ->get('code');
      if (empty($code)) {
        throw new \UnexpectedValueException("The authorization_code query parameter is missing.");
      }
      $this->apiAuthorize
        ->authorize($code);
      $this->messenger
        ->addMessage($this
        ->t('FormAssembly successfully authorized.'));
      $url = Url::fromRoute('fa_form.settings');
      return new RedirectResponse($url
        ->toString());
    } catch (\Exception $exception) {
      $this->messenger
        ->addMessage($this
        ->t('FormAssembly module failed to authorize. Reason: @message', [
        '@message' => $exception
          ->getMessage(),
      ]), 'error');
      throw $exception;
    }
  }

}

Classes

Namesort descending Description
OauthInteractions Utility class for interacting with FormAssembly for authorization.