You are here

simple_oauth_extras.module in Simple OAuth (OAuth2) & OpenID Connect 8.3

Same filename and directory in other branches
  1. 8.2 simple_oauth_extras/simple_oauth_extras.module

Module implementation file.

File

simple_oauth_extras/simple_oauth_extras.module
View source
<?php

/**
 * @file
 * Module implementation file.
 */
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Link;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;

/**
 * Implements hook_form_FORM_ID_alter().
 */
function simple_oauth_extras_form_oauth2_token_settings_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form['use_implicit'] = [
    '#type' => 'checkbox',
    '#title' => t('Enable the implicit grant?'),
    '#description' => t('The implicit grant has the potential to be used in an insecure way. Only enable this if you understand the risks. See https://tools.ietf.org/html/rfc6819#section-4.4.2 for more information.'),
    '#default_value' => \Drupal::config('simple_oauth_extras.settings')
      ->get('use_implicit'),
  ];
  $form['#submit'][] = 'simple_oauth_extras_form_oauth2_token_settings_submit';
}

/**
 * Form submission handler.
 *
 * @param array $form
 *   An associative array containing the structure of the form.
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 *   The current state of the form.
 */
function simple_oauth_extras_form_oauth2_token_settings_submit(array &$form, FormStateInterface $form_state) {
  $settings = \Drupal::configFactory()
    ->getEditable('simple_oauth_extras.settings');
  $settings
    ->set('use_implicit', $form_state
    ->getValue('use_implicit'));
  $settings
    ->save();
}

/**
 * Implements hook_entity_base_field_info().
 */
function simple_oauth_extras_entity_base_field_info(EntityTypeInterface $entity_type) {
  $fields = [];
  if ($entity_type
    ->id() == 'consumer') {
    $fields['redirect'] = BaseFieldDefinition::create('uri')
      ->setLabel(new TranslatableMarkup('Redirect URI'))
      ->setDescription(new TranslatableMarkup('The URI this client will redirect to when needed.'))
      ->setDisplayOptions('view', [
      'label' => 'inline',
      'weight' => 4,
    ])
      ->setDisplayOptions('form', [
      'weight' => 4,
    ])
      ->setDisplayConfigurable('view', TRUE)
      ->setTranslatable(TRUE)
      ->setSetting('max_length', 255);
    $fields['user_id'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(new TranslatableMarkup('User'))
      ->setDescription(new TranslatableMarkup('When no specific user is authenticated Drupal will use this user as the author of all the actions made.'))
      ->setRevisionable(TRUE)
      ->setSetting('target_type', 'user')
      ->setSetting('handler', 'default')
      ->setTranslatable(FALSE)
      ->setDisplayOptions('view', [
      'label' => 'inline',
      'type' => 'entity_reference_label',
      'weight' => 1,
    ])
      ->setCardinality(1)
      ->setDisplayOptions('form', [
      'type' => 'entity_reference_autocomplete',
      'weight' => 0,
      'settings' => [
        'match_operator' => 'CONTAINS',
        'size' => '60',
        'autocomplete_type' => 'tags',
        'placeholder' => '',
      ],
    ]);
  }
  return $fields;
}

/**
 * Implements hook_consumers_list_alter().
 */
function simple_oauth_extras_consumers_list_alter(&$data, $context) {
  if ($context['type'] === 'header') {
    $data['redirect'] = t('Redirect');
  }
  elseif ($context['type'] === 'row') {
    $data['redirect'] = NULL;
    if ($redirect_url = $context['entity']
      ->get('redirect')->value) {
      $data['redirect'] = Link::fromTextAndUrl($redirect_url, Url::fromUri($redirect_url));
    }
  }
}