You are here

class BasicAuth in Entity Share 8.3

Provides Basic Auth based client authorization.

Plugin annotation


@ClientAuthorization(
  id = "basic_auth",
  label = @Translation("Basic Auth"),
)

Hierarchy

Expanded class hierarchy of BasicAuth

File

modules/entity_share_client/src/Plugin/ClientAuthorization/BasicAuth.php, line 18

Namespace

Drupal\entity_share_client\Plugin\ClientAuthorization
View source
class BasicAuth extends ClientAuthorizationPluginBase {

  /**
   * {@inheritdoc}
   */
  public function checkIfAvailable() {

    // Basic Auth is a core module which any server can enable.
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function getClient($url) {
    $credentials = $this->keyService
      ->getCredentials($this);
    $http_client = $this->httpClientFactory
      ->fromOptions([
      'base_uri' => $url . '/',
      'cookies' => TRUE,
      'allow_redirects' => TRUE,
    ]);
    $http_client
      ->post('/user/login', [
      'form_params' => [
        'name' => $credentials['username'],
        'pass' => $credentials['password'],
        'form_id' => 'user_login_form',
      ],
    ]);
    return $http_client;
  }

  /**
   * {@inheritdoc}
   */
  public function getJsonApiClient($url) {
    $credentials = $this->keyService
      ->getCredentials($this);
    return $this->httpClientFactory
      ->fromOptions([
      'base_uri' => $url . '/',
      'auth' => [
        $credentials['username'],
        $credentials['password'],
      ],
      'headers' => [
        'Content-type' => 'application/vnd.api+json',
      ],
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);
    $credentials = $this->keyService
      ->getCredentials($this);
    $form['entity_share']['username'] = [
      '#type' => 'textfield',
      '#required' => FALSE,
      '#title' => $this
        ->t('Username'),
      '#default_value' => $credentials['username'] ?? '',
    ];
    $form['entity_share']['password'] = [
      '#type' => 'password',
      '#required' => FALSE,
      '#title' => $this
        ->t('Password'),
      '#default_value' => $credentials['password'] ?? '',
    ];
    if ($this->keyService
      ->additionalProviders()) {
      $this
        ->expandedProviderOptions($form);
      $form['key']['id']['#key_filters'] = [
        'type' => 'entity_share_basic_auth',
      ];
      $form['key']['id']['#description'] = $this
        ->t('Select the key you have configured to hold the Basic Auth credentials.');
    }
    $form['warning_message'] = [
      '#theme' => 'status_messages',
      '#message_list' => [
        'warning' => [
          $this
            ->t('With the Basic Auth authorization method you need to ensure that the %module_name module is enabled on the server website.', [
            '%module_name' => $this
              ->t('HTTP Basic Authentication'),
          ]),
        ],
      ],
    ];
    return $form;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BasicAuth::buildConfigurationForm public function Form constructor. Overrides ClientAuthorizationPluginBase::buildConfigurationForm
BasicAuth::checkIfAvailable public function Returns true if the plugin method is supported. Overrides ClientAuthorizationInterface::checkIfAvailable
BasicAuth::getClient public function Prepares a guzzle client for http operations with the supported auth. Overrides ClientAuthorizationInterface::getClient
BasicAuth::getJsonApiClient public function Prepares a guzzle client for JSON operations with the supported auth. Overrides ClientAuthorizationInterface::getJsonApiClient
ClientAuthorizationInterface::LOCAL_STORAGE_KEY_VALUE_COLLECTION constant The collection ID of for authorization config local storage.
ClientAuthorizationPluginBase::$httpClientFactory protected property Injected HTTP client factory.
ClientAuthorizationPluginBase::$keyService protected property Injected key service.
ClientAuthorizationPluginBase::$keyValueStore protected property The key value store to use.
ClientAuthorizationPluginBase::$uuid protected property Injected UUID service.
ClientAuthorizationPluginBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 1
ClientAuthorizationPluginBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableInterface::defaultConfiguration
ClientAuthorizationPluginBase::expandedProviderOptions protected function Helper method to build the credential provider elements of the form.
ClientAuthorizationPluginBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
ClientAuthorizationPluginBase::getCredentialProvider public function Returns the plugin data if it is set, otherwise returns NULL. Overrides ClientAuthorizationInterface::getCredentialProvider
ClientAuthorizationPluginBase::getLabel public function Gets the plugin label. Overrides ClientAuthorizationInterface::getLabel
ClientAuthorizationPluginBase::getStorageKey public function Returns the plugin data if it is set, otherwise returns NULL. Overrides ClientAuthorizationInterface::getStorageKey
ClientAuthorizationPluginBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
ClientAuthorizationPluginBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm 1
ClientAuthorizationPluginBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm 1
ClientAuthorizationPluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
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
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.