Commit 36ce6366 authored by Mathieu De Meue's avatar Mathieu De Meue Committed by Tim Diels
Browse files

Issue #3273756: Update the auth method

parent 24c1fea4
Loading
Loading
Loading
Loading
+7 −17
Original line number Diff line number Diff line
@@ -31,26 +31,18 @@ class CloudflareStreamSettings extends ConfigFormBase {
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    // Getting already saved config from the database.
    $email = $this->config('cloudflare_stream.settings')->get('email');
    $api_key = $this->config('cloudflare_stream.settings')->get('api_key');
    $api_token = $this->config('cloudflare_stream.settings')->get('api_token');
    $account_id = $this->config('cloudflare_stream.settings')->get('account_id');
    $zone_id = $this->config('cloudflare_stream.settings')->get('zone_id');
    $replace_source_files = $this->config('cloudflare_stream.settings')->get('replace_source_files');
    $debug_messages = $this->config('cloudflare_stream.settings')->get('debug_messages');

    // Adding form fields to the form.
    $form['email'] = [
      '#type' => 'email',
      '#title' => $this->t('Email Address'),
      '#description' => $this->t('Your Cloudflare login email address'),
      '#default_value' => $email,
      '#required' => TRUE,
    ];
    $form['api_key'] = [
    $form['api_token'] = [
      '#type' => 'textfield',
      '#title' => $this->t('API Key'),
      '#description' => $this->t('Your Cloudflare API key. This key can be found on your Cloudflare My Profile page > API tokens tab. <br><a href=":link">More info on the Cloudflare documentation page</a>', [':link' => 'https://support.cloudflare.com/hc/en-us/articles/200167836-Managing-API-Tokens-and-Keys#12345682']),
      '#default_value' => $api_key,
      '#title' => $this->t('API Token'),
      '#description' => $this->t('To find your API Token go to the Cloudflare Dashboard, optionally select your account, and select the website. In the sidebar API section you can find a link to the API Token page where you can generate an API token.'),
      '#default_value' => $api_token,
      '#required' => TRUE,
    ];
    $form['website_specific_settings'] = [
@@ -94,8 +86,7 @@ class CloudflareStreamSettings extends ConfigFormBase {
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    // Trimming user entry for any whitespace.
    $email = preg_replace('/\s/', '', $form_state->getValue('email'));
    $api_key = preg_replace('/\s/', '', $form_state->getValue('api_key'));
    $api_token = preg_replace('/\s/', '', $form_state->getValue('api_token'));
    $account_id = preg_replace('/\s/', '', $form_state->getValue('account_id'));
    $zone_id = preg_replace('/\s/', '', $form_state->getValue('zone_id'));
    $replace_source_files = $form_state->getValue('replace_source_files');
@@ -103,8 +94,7 @@ class CloudflareStreamSettings extends ConfigFormBase {

    // Saving config in the database.
    $this->config('cloudflare_stream.settings')
      ->set('email', $email)
      ->set('api_key', $api_key)
      ->set('api_token', $api_token)
      ->set('account_id', $account_id)
      ->set('zone_id', $zone_id)
      ->set('replace_source_files', $replace_source_files)
+2 −9
Original line number Diff line number Diff line
@@ -38,15 +38,8 @@ class CloudflareStream implements CloudflareStreamInterface {
  /**
   * {@inheritDoc}
   */
  public function getEmail() {
    return $this->config->get('email');
  }

  /**
   * {@inheritDoc}
   */
  public function getApiKey() {
    return $this->config->get('api_key');
  public function getApiToken() {
    return $this->config->get('api_token');
  }

  /**
+1 −2
Original line number Diff line number Diff line
@@ -189,8 +189,7 @@ class CloudflareStreamApi implements CloudflareStreamApiInterface {
   */
  protected function getHeaders() {
    return [
      'X-Auth-Email' => $this->cloudflareStream->getEmail(),
      'X-Auth-Key' => $this->cloudflareStream->getApiKey(),
      'Authorization' => 'Bearer ' . $this->cloudflareStream->getApiToken(),
    ];
  }

+3 −12
Original line number Diff line number Diff line
@@ -18,20 +18,11 @@ interface CloudflareStreamInterface {
  public function debugEnabled();

  /**
   * Return the email from Cloudflare Stream configuration.
   *
   * @return string
   *   The email.
   */
  public function getEmail();

  /**
   * Returns the api_key from Cloudflare Stream configuration.
   *
   * Returns the api_token from Cloudflare Stream configuration.
   * @return string
   *   The API key.
   *   The API token.
   */
  public function getApiKey();
  public function getApiToken();

  /**
   * Returns the zone ID from Cloudflare Stream configuration.