diff --git a/modules/entity_share_client/config/schema/remote.schema.yml b/modules/entity_share_client/config/schema/remote.schema.yml index 5537f6e..9afa160 100644 --- a/modules/entity_share_client/config/schema/remote.schema.yml +++ b/modules/entity_share_client/config/schema/remote.schema.yml @@ -36,3 +36,6 @@ key.type.entity_share_basic_auth: key.type.entity_share_oauth: type: sequence + +key.type.entity_share_header: + type: sequence diff --git a/modules/entity_share_client/src/Plugin/ClientAuthorization/Anonymous.php b/modules/entity_share_client/src/Plugin/ClientAuthorization/Anonymous.php index f463542..a152ea0 100644 --- a/modules/entity_share_client/src/Plugin/ClientAuthorization/Anonymous.php +++ b/modules/entity_share_client/src/Plugin/ClientAuthorization/Anonymous.php @@ -28,41 +28,76 @@ class Anonymous extends ClientAuthorizationPluginBase { * {@inheritdoc} */ public function getClient($url) { - return $this->httpClientFactory->fromOptions([ + $credentials = $this->keyService->getCredentials($this); + $client = $this->httpClientFactory->fromOptions([ 'base_uri' => $url . '/', 'cookies' => TRUE, 'allow_redirects' => TRUE, ]); + if (!empty($credentials['login']) && !empty($credentials['password'])) { + $client['auth'] = [ + $credentials['login'], + $credentials['password'], + ]; + } + return $client; } /** * {@inheritdoc} */ public function getJsonApiClient($url) { - return $this->httpClientFactory->fromOptions([ + $credentials = $this->keyService->getCredentials($this); + $client = $this->httpClientFactory->fromOptions([ 'base_uri' => $url . '/', 'headers' => [ 'Content-type' => 'application/vnd.api+json', ], ]); + if (!empty($credentials['login']) && !empty($credentials['password'])) { + $client['auth'] = [ + $credentials['login'], + $credentials['password'], + ]; + } + return $client; } /** * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { - return $form + [ - 'credential_provider' => [], - 'entity_share' => [], + $form = parent::buildConfigurationForm($form, $form_state); + $credentials = $this->keyService->getCredentials($this); + + $form['entity_share']['#description'] = $this->t('Leave empty if Server website is not protected via HTTP Password.'); + $form['entity_share']['login'] = [ + '#type' => 'textfield', + '#required' => FALSE, + '#title' => $this->t('Login'), + '#default_value' => $credentials['login'] ?? '', + ]; + + $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']['#description'] = $this->t('Select the key you have configured to hold the HTTP Password credentials.'); + } + return $form; } /** * {@inheritdoc} */ public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { - // To prevent validation from parent::validateConfigurationForm() as we - // don't have credentials here. + // To prevent validation from parent::validateConfigurationForm() as + // credentials is not required. } } diff --git a/modules/entity_share_client/src/Plugin/ClientAuthorization/Header.php b/modules/entity_share_client/src/Plugin/ClientAuthorization/Header.php new file mode 100644 index 0000000..1dc4218 --- /dev/null +++ b/modules/entity_share_client/src/Plugin/ClientAuthorization/Header.php @@ -0,0 +1,87 @@ +keyService->getCredentials($this); + return $this->httpClientFactory->fromOptions([ + 'base_uri' => $url . '/', + 'cookies' => TRUE, + 'allow_redirects' => TRUE, + 'headers' => [ + 'Content-type' => 'application/vnd.api+json', + $credentials['header_name'] => $credentials['header_value'], + ], + ]); + } + + /** + * {@inheritdoc} + */ + public function getJsonApiClient($url) { + $credentials = $this->keyService->getCredentials($this); + return $this->httpClientFactory->fromOptions([ + 'base_uri' => $url . '/', + 'headers' => [ + 'Content-type' => 'application/vnd.api+json', + $credentials['header_name'] => $credentials['header_value'], + ], + ]); + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $form = parent::buildConfigurationForm($form, $form_state); + $credentials = $this->keyService->getCredentials($this); + + $form['entity_share']['header_name'] = [ + '#type' => 'textfield', + '#required' => TRUE, + '#title' => $this->t('Name'), + '#default_value' => $credentials['header_name'] ?? '', + ]; + + $form['entity_share']['header_value'] = [ + '#type' => 'textfield', + '#required' => TRUE, + '#title' => $this->t('Value'), + '#default_value' => $credentials['header_value'] ?? '', + ]; + if ($this->keyService->additionalProviders()) { + $this->expandedProviderOptions($form); + $form['key']['id']['#key_filters'] = ['type' => 'entity_share_header']; + $form['key']['id']['#description'] = $this->t('Select the key you have configured to hold the Header data.'); + } + + return $form; + } + +} diff --git a/modules/entity_share_client/src/Plugin/KeyType/EntityShareHeader.php b/modules/entity_share_client/src/Plugin/KeyType/EntityShareHeader.php new file mode 100644 index 0000000..a6c3cb5 --- /dev/null +++ b/modules/entity_share_client/src/Plugin/KeyType/EntityShareHeader.php @@ -0,0 +1,31 @@ +
{
"header_name": "header name",
"header_value": "header value"
}
"), + * group = "authentication", + * key_value = { + * "plugin" = "textarea_field" + * }, + * multivalue = { + * "enabled" = true, + * "fields" = { + * "header_name" = @Translation("Header name"), + * "header_value" = @Translation("Header value") + * } + * } + * ) + */ +class EntityShareHeader extends AuthenticationMultivalueKeyType { + +}