View source
<?php
namespace Drupal\simple_oauth\Controller;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\simple_oauth\KnownClientsRepositoryInterface;
use Drupal\simple_oauth\Plugin\Oauth2GrantManagerInterface;
use League\OAuth2\Server\Exception\OAuthServerException;
use Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface;
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Oauth2AuthorizeForm extends FormBase {
protected $entityTypeManager;
protected $messageFactory;
protected $foundationFactory;
protected $server;
protected $grantManager;
protected $configFactory;
protected $knownClientRepository;
public function __construct(EntityTypeManagerInterface $entity_type_manager, HttpMessageFactoryInterface $message_factory, HttpFoundationFactoryInterface $foundation_factory, Oauth2GrantManagerInterface $grant_manager, ConfigFactoryInterface $config_factory, KnownClientsRepositoryInterface $known_clients_repository) {
$this->entityTypeManager = $entity_type_manager;
$this->messageFactory = $message_factory;
$this->foundationFactory = $foundation_factory;
$this->grantManager = $grant_manager;
$this->configFactory = $config_factory;
$this->knownClientRepository = $known_clients_repository;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('psr7.http_message_factory'), $container
->get('psr7.http_foundation_factory'), $container
->get('plugin.manager.oauth2_grant.processor'), $container
->get('config.factory'), $container
->get('simple_oauth.known_clients'));
}
public function getFormId() {
return 'simple_oauth_authorize_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$request = $this
->getRequest();
if ($request
->get('response_type') == 'code') {
$grant_type = 'code';
}
elseif ($request
->get('response_type') == 'token') {
$grant_type = 'implicit';
}
else {
$grant_type = NULL;
}
$client_uuid = $request
->get('client_id');
$consumer_storage = $this->entityTypeManager
->getStorage('consumer');
$client_drupal_entities = $consumer_storage
->loadByProperties([
'uuid' => $client_uuid,
]);
if (empty($client_drupal_entities)) {
throw OAuthServerException::invalidClient();
}
$client_drupal_entity = reset($client_drupal_entities);
$this->server = $this->grantManager
->getAuthorizationServer($grant_type, $client_drupal_entity);
$psr7_request = $this->messageFactory
->createRequest($request);
$auth_request = $this->server
->validateAuthorizationRequest($psr7_request);
$form_state
->set('auth_request', $auth_request);
$manager = $this->entityTypeManager;
$form = [
'#type' => 'container',
];
$cacheablity_metadata = new CacheableMetadata();
$form['client'] = $manager
->getViewBuilder('consumer')
->view($client_drupal_entity);
$form['scopes'] = [
'#title' => $this
->t('Permissions'),
'#theme' => 'item_list',
'#items' => [],
];
$client_roles = [];
foreach ($client_drupal_entity
->get('roles') as $role_item) {
$client_roles[$role_item->target_id] = $role_item->entity;
}
foreach ($auth_request
->getScopes() as $scope) {
$cacheablity_metadata
->addCacheableDependency($scope);
$form['scopes']['#items'][] = $scope
->getName();
unset($client_roles[$scope
->getIdentifier()]);
}
foreach ($client_roles as $client_role) {
$cacheablity_metadata
->addCacheableDependency($client_role);
$form['scopes']['#items'][] = $client_role
->label();
}
$cacheablity_metadata
->applyTo($form['scopes']);
$form['redirect_uri'] = [
'#type' => 'hidden',
'#value' => $request
->get('redirect_uri') ? $request
->get('redirect_uri') : $client_drupal_entity
->get('redirect')->value,
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Grant'),
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($auth_request = $form_state
->get('auth_request')) {
$can_grant_codes = $this
->currentUser()
->hasPermission('grant simple_oauth codes');
$redirect_response = Oauth2AuthorizeController::redirectToCallback($auth_request, $this->server, $this
->currentUser(), (bool) $form_state
->getValue('submit') && $can_grant_codes, (bool) $this->configFactory
->get('simple_oauth.settings')
->get('remember_clients'), $this->knownClientRepository);
$form_state
->setResponse($redirect_response);
}
}
}