You are here

class ApigeeAuthKeyInput in Apigee Edge 8

Apigee Edge authentication credentials input text fields.

Defines a key input that provides input text fields and value preprocessors for Apigee Edge authentication credentials.

Plugin annotation


@KeyInput(
  id = "apigee_auth_input",
  label = @Translation("Apigee Edge authentication credentials input fields."),
  description = @Translation("Provides input text fields for Apigee Edge authentication credentials.")
)

Hierarchy

Expanded class hierarchy of ApigeeAuthKeyInput

File

src/Plugin/KeyInput/ApigeeAuthKeyInput.php, line 41

Namespace

Drupal\apigee_edge\Plugin\KeyInput
View source
class ApigeeAuthKeyInput extends KeyInputBase {

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $values = $this
      ->getFormDefaultValues($form_state);
    if (!empty($values['auth_type']) && $values['auth_type'] == EdgeKeyTypeInterface::EDGE_AUTH_TYPE_BASIC) {
      $this
        ->messenger()
        ->addWarning($this
        ->t('HTTP basic authentication will be deprecated. Please choose another authentication method.'));
    }
    $state_for_public = [
      ':input[name="key_input_settings[instance_type]"]' => [
        'value' => EdgeKeyTypeInterface::INSTANCE_TYPE_PUBLIC,
      ],
    ];
    $state_for_private = [
      ':input[name="key_input_settings[instance_type]"]' => [
        'value' => EdgeKeyTypeInterface::INSTANCE_TYPE_PRIVATE,
      ],
    ];
    $state_for_hybrid = [
      ':input[name="key_input_settings[instance_type]"]' => [
        'value' => EdgeKeyTypeInterface::INSTANCE_TYPE_HYBRID,
      ],
    ];
    $form['instance_type'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Apigee instance type'),
      '#description' => $this
        ->t('Select the Apigee instance type you are connecting to. More information can be found in the <a href="@link" target="_blank">Apigee documentation</a>.', [
        '@link' => 'https://www.drupal.org/docs/8/modules/apigee-edge/configure-the-connection-to-apigee-edge',
      ]),
      '#required' => TRUE,
      '#options' => [
        EdgeKeyTypeInterface::INSTANCE_TYPE_PUBLIC => $this
          ->t('Apigee Edge (Endpoint: https://api.enterprise.apigee.com/v1)'),
        EdgeKeyTypeInterface::INSTANCE_TYPE_HYBRID => $this
          ->t('Apigee X (Endpoint: https://apigee.googleapis.com/v1)'),
        EdgeKeyTypeInterface::INSTANCE_TYPE_PRIVATE => $this
          ->t('Private cloud (Custom endpoint)'),
      ],
      '#default_value' => $values['instance_type'] ?? 'public',
    ];
    $form['auth_type'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Authentication type'),
      '#description' => $this
        ->t('Select the authentication method to use.'),
      '#required' => TRUE,
      '#options' => [
        EdgeKeyTypeInterface::EDGE_AUTH_TYPE_OAUTH => $this
          ->t('OAuth'),
        EdgeKeyTypeInterface::EDGE_AUTH_TYPE_BASIC => $this
          ->t('HTTP basic (deprecated)'),
      ],
      '#default_value' => $values['auth_type'] ?? EdgeKeyTypeInterface::EDGE_AUTH_TYPE_OAUTH,
      '#states' => [
        'visible' => [
          $state_for_public,
          $state_for_private,
        ],
        'required' => [
          $state_for_public,
          $state_for_private,
        ],
      ],
    ];
    $form['organization'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Organization'),
      '#description' => $this
        ->t('Name of the organization on Apigee Edge. Changing this value could make your site stop working.'),
      '#default_value' => $values['organization'] ?? '',
      '#required' => TRUE,
      '#attributes' => [
        'autocomplete' => 'off',
      ],
      '#prefix' => '<div id="edit-organization-field">',
      '#suffix' => '</div>',
    ];
    $form['username'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Username'),
      '#description' => $this
        ->t("Apigee user's email address or identity provider username that is used for authenticating with the endpoint."),
      '#default_value' => $values['username'] ?? '',
      '#attributes' => [
        'autocomplete' => 'off',
      ],
      '#states' => [
        'visible' => [
          $state_for_public,
          $state_for_private,
        ],
        'required' => [
          $state_for_public,
          $state_for_private,
        ],
      ],
    ];
    $form['password'] = [
      '#type' => 'password',
      '#title' => $this
        ->t('Password'),
      '#description' => $this
        ->t("Organization user's password that is used for authenticating with the endpoint.") . (empty($values['password']) ? '' : ' ' . $this
        ->t('Leave empty unless you want to change the stored password.')),
      '#attributes' => [
        'autocomplete' => 'off',
      ],
      '#states' => [
        'visible' => [
          $state_for_public,
          $state_for_private,
        ],
      ],
    ];

    // If password was never set make sure it is required.
    if (empty($values['organization'])) {
      $form['password']['#states']['required'] = [
        $state_for_public,
        $state_for_private,
      ];
    }
    $state_for_not_gcp_hosted = [];
    $gceServiceAccountAuth = new GceServiceAccountAuthentication(\Drupal::service('apigee_edge.authentication.oauth_token_storage'));
    if ($gceServiceAccountAuth
      ->isAvailable()) {
      $form['gcp_hosted'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Use the default service account if this portal is hosted on GCP'),
        '#description' => $this
          ->t("Please ensure you have added 'Apigee Developer Administrator' role to the default compute engine service account hosting this portal."),
        '#default_value' => $values['gcp_hosted'] ?? TRUE,
        '#states' => [
          'visible' => $state_for_hybrid,
        ],
      ];
      $state_for_not_gcp_hosted = [
        ':input[name="key_input_settings[gcp_hosted]"]' => [
          'checked' => FALSE,
        ],
      ];
    }
    $form['account_json_key'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('GCP service account key'),
      '#description' => $this
        ->t("Paste the contents of the GCP service account key JSON file."),
      '#default_value' => $values['account_json_key'] ?? '',
      '#rows' => '8',
      '#states' => [
        'visible' => $state_for_hybrid + $state_for_not_gcp_hosted,
        'required' => $state_for_hybrid + $state_for_not_gcp_hosted,
      ],
    ];
    $form['endpoint'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Apigee Edge endpoint'),
      '#description' => $this
        ->t('Apigee Edge endpoint where the API calls are being sent. For a Private Cloud installation it is in the form: %form_a or %form_b.', [
        '%form_a' => 'http://ms_IP_or_DNS:8080/v1',
        '%form_b' => 'https://ms_IP_or_DNS:TLSport/v1',
      ]),
      '#default_value' => $values['endpoint'] ?? '',
      '#attributes' => [
        'autocomplete' => 'off',
      ],
      '#states' => [
        'visible' => $state_for_private,
        'required' => $state_for_private,
      ],
    ];
    $form['authorization_server_type'] = [
      '#title' => $this
        ->t('Authorization server'),
      '#type' => 'radios',
      '#required' => TRUE,
      '#default_value' => $values['authorization_server_type'] ?? 'default',
      '#options' => [
        'default' => $this
          ->t('Default'),
        'custom' => $this
          ->t('Custom'),
      ],
      '#description' => $this
        ->t('The server issuing access tokens to the client. Use the default (%authorization_server), unless using a SAML enabled organization.', [
        '%authorization_server' => Oauth::DEFAULT_AUTHORIZATION_SERVER,
      ]),
      '#states' => [
        'visible' => [
          [
            $state_for_public,
            $state_for_private,
          ],
          ':input[name="key_input_settings[auth_type]"]' => [
            'value' => EdgeKeyTypeInterface::EDGE_AUTH_TYPE_OAUTH,
          ],
        ],
      ],
    ];
    $form['authorization_server'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Custom authorization server'),
      '#description' => $this
        ->t('The authorization server endpoint for a SAML enabled edge org is in the form: %form.', [
        '%form' => 'https://{zonename}.login.apigee.com/oauth/token',
      ]),
      '#default_value' => $values['authorization_server'] ?? '',
      '#attributes' => [
        'autocomplete' => 'off',
      ],
      '#states' => [
        'visible' => [
          [
            $state_for_public,
            $state_for_private,
          ],
          ':input[name="key_input_settings[auth_type]"]' => [
            'value' => EdgeKeyTypeInterface::EDGE_AUTH_TYPE_OAUTH,
          ],
          ':input[name="key_input_settings[authorization_server_type]"]' => [
            'value' => 'custom',
          ],
        ],
        'required' => [
          [
            $state_for_public,
            $state_for_private,
          ],
          ':input[name="key_input_settings[auth_type]"]' => [
            'value' => EdgeKeyTypeInterface::EDGE_AUTH_TYPE_OAUTH,
          ],
          ':input[name="key_input_settings[authorization_server_type]"]' => [
            'value' => 'custom',
          ],
        ],
      ],
    ];
    $form['client_id'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Client ID'),
      '#description' => $this
        ->t('The client identifier issued to the client during the registration process. Leave empty to use the default %client_id client ID.', [
        '%client_id' => Oauth::DEFAULT_CLIENT_ID,
      ]),
      '#default_value' => $values['client_id'] ?? '',
      '#attributes' => [
        'autocomplete' => 'off',
      ],
      '#states' => [
        'visible' => [
          [
            $state_for_public,
            $state_for_private,
          ],
          ':input[name="key_input_settings[auth_type]"]' => [
            'value' => EdgeKeyTypeInterface::EDGE_AUTH_TYPE_OAUTH,
          ],
        ],
      ],
    ];
    $form['client_secret'] = [
      '#type' => 'password',
      '#title' => $this
        ->t('Client secret'),
      '#description' => $this
        ->t('A secret known only to the client and the authorization server. Leave empty to use the default %client_secret client secret.', [
        '%client_secret' => Oauth::DEFAULT_CLIENT_SECRET,
      ]),
      '#default_value' => $values['client_secret'] ?? '',
      '#attributes' => [
        'autocomplete' => 'off',
      ],
      '#states' => [
        'visible' => [
          [
            $state_for_public,
            $state_for_private,
          ],
          ':input[name="key_input_settings[auth_type]"]' => [
            'value' => EdgeKeyTypeInterface::EDGE_AUTH_TYPE_OAUTH,
          ],
        ],
      ],
    ];
    $form['key_value'] = [
      '#type' => 'value',
      '#value' => $form_state
        ->get('key_value')['current'],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    $input_values = $form_state
      ->getUserInput()['key_input_settings'];
    if ($input_values['instance_type'] == EdgeKeyTypeInterface::INSTANCE_TYPE_HYBRID && empty($input_values['gcp_hosted'])) {
      $account_key = $input_values['account_json_key'] ?? '';
      $json = json_decode($account_key, TRUE);
      if (empty($json['private_key']) || empty($json['client_email'])) {
        $form_state
          ->setErrorByName('key_input_settings][account_json_key', $this
          ->t('GCP service account key JSON file is invalid.'));
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function processSubmittedKeyValue(FormStateInterface $form_state) {

    // Get input values.
    $input_values = $form_state
      ->getValues();
    if (!empty($input_values)) {
      $instance_type = $input_values['instance_type'] ?? NULL;

      // Make sure the endpoint defaults are not overridden by other values.
      if ($instance_type == EdgeKeyTypeInterface::INSTANCE_TYPE_PUBLIC) {
        $input_values['endpoint'] = '';
      }
      if (empty($input_values['authorization_server_type']) || $input_values['authorization_server_type'] == 'default') {
        $input_values['authorization_server'] = '';
      }

      // Remove unneeded values if on a Hybrid instance.
      if ($instance_type == EdgeKeyTypeInterface::INSTANCE_TYPE_HYBRID) {
        $input_values['auth_type'] = '';
        $input_values['username'] = '';
        $input_values['password'] = '';
        $input_values['endpoint'] = '';
        $input_values['authorization_server_type'] = '';
        $input_values['authorization_server'] = '';
        $input_values['client_id'] = '';
        $input_values['client_secret'] = '';
        if (!empty($input_values['gcp_hosted'])) {
          $input_values['account_json_key'] = '';
        }
      }
      else {

        // Remove unneeded values if on a Public or Private instance.
        $input_values['account_json_key'] = '';
        if (!empty($input_values['gcp_hosted'])) {
          unset($input_values['gcp_hosted']);
        }

        // If password field is empty we just skip it and preserve the initial
        // password if there is one already.
        if (empty($input_values['password']) && !empty($form_state
          ->get('key_value')['current'])) {
          $values = $this
            ->getFormDefaultValues($form_state);
          if (!empty($values['password'])) {
            $input_values['password'] = $values['password'];
          }
        }
      }

      // Remove `key_value` so it doesn't get double encoded.
      unset($input_values['key_value']);
    }

    // Reset values to just `key_value`.
    $form_state
      ->setValues([
      'key_value' => Json::encode(array_filter($input_values)),
    ]);
    return parent::processSubmittedKeyValue($form_state);
  }

  /**
   * Get authentication from values.
   */
  protected function getFormDefaultValues(FormStateInterface $form_state) {

    // When AJAX rebuilds the the form (f.e.: the "Send request" button) the
    // submitted data is only available in $form_state->getUserInput() and not
    // in $form_state->getValues(). Key is not prepared to handle this out of
    // the box this is the reason why we have to manually process the user
    // input and retrieve the submitted values here.
    $key_value = $form_state
      ->get('key_value')['current'];

    // Either null or an empty string.
    if (empty($key_value)) {

      // When "Test connection" reloads the page they are not yet processed.
      // @see \Drupal\key\Form\KeyFormBase::createPluginFormState()
      $key_input_plugin_form_state = clone $form_state;
      $key_input_plugin_form_state
        ->setValues($form_state
        ->getUserInput()['key_input_settings']);

      // @see \Drupal\key\Form\KeyFormBase::validateForm()
      $key_input_processed_values = $form_state
        ->getFormObject()
        ->getEntity()
        ->getKeyInput()
        ->processSubmittedKeyValue($key_input_plugin_form_state);
      $key_value = $key_input_processed_values['processed_submitted'];
    }

    // Could be an empty array.
    $values = Json::decode($key_value);
    $values['authorization_server_type'] = empty($values['authorization_server']) ? 'default' : 'custom';
    return $values;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ApigeeAuthKeyInput::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm
ApigeeAuthKeyInput::getFormDefaultValues protected function Get authentication from values.
ApigeeAuthKeyInput::processSubmittedKeyValue public function Process a submitted key value. Overrides KeyInputBase::processSubmittedKeyValue
ApigeeAuthKeyInput::validateConfigurationForm public function Form validation handler. Overrides KeyInputBase::validateConfigurationForm
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
KeyInputBase::processExistingKeyValue public function Process an existing key value. Overrides KeyInputInterface::processExistingKeyValue 1
KeyInputBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
KeyPluginBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
KeyPluginBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 1
KeyPluginBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableInterface::defaultConfiguration 8
KeyPluginBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
KeyPluginBase::getPluginType public function Returns the type of plugin. Overrides KeyPluginInterface::getPluginType
KeyPluginBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
KeyPluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.