0

Using the Remote POST handler in Webform 5, is there a way to add basic auth / API key to the request leveraging tokens or referencing a const? I'd like to find a way not to hardcode actual values.

1
  • 1
    Sounds like you are going to want to define a custom webform handler. You can add settings to the settings form on the handler for your api keys. Try making a new one that extends the remote post handler to give you a head start. This way your keys end up in config Dec 29, 2020 at 21:52

1 Answer 1

1

Just adding an answer in addition to @Ryan Hartman's comment. Extend the RemotePostWebformHandler class and its ::buildConfigurationForm() method:

/**
 * Custom Webform submission remote post handler.
 *
 * @WebformHandler(
 *   id = "custom_remote_post",
 *   ...
 * )  
 */
class CustomRemotePostWebformHandler extends RemotePostWebformHandler {

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);

    $form['api_key'] = [
      '#type' => 'textfield',
      '#title' => $this->t('API Key'),
      '#description' => $this->t('The API key for basic auth.'),
      '#default_value' => $this->configuration['api_key'] ?? NULL,
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    parent::submitConfigurationForm($form, $form_state);

    $this->configuration['api_key'] = $form_state->getValue('api_key') ?: NULL;
  }

  ...

Alternatively implement the form element provided by the Key module so you can securely store sensitive secret keys.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.