You are here

protected function NspiController::validateAuthenticator in Acquia Connector 8.2

Same name and namespace in other branches
  1. 8 tests/modules/src/Controller/NspiController.php \Drupal\acquia_connector_test\Controller\NspiController::validateAuthenticator()
  2. 3.x tests/modules/src/Controller/NspiController.php \Drupal\acquia_connector_test\Controller\NspiController::validateAuthenticator()

Test validates an Acquia authenticator.

Parameters

array $data: Data to validate.

Return value

array Result array.

2 calls to NspiController::validateAuthenticator()
NspiController::getSubscription in tests/modules/src/Controller/NspiController.php
Test validates an Acquia subscription.
NspiController::nspiUpdate in tests/modules/src/Controller/NspiController.php
SPI API site update.

File

tests/modules/src/Controller/NspiController.php, line 477

Class

NspiController
Class NspiController.

Namespace

Drupal\acquia_connector_test\Controller

Code

protected function validateAuthenticator(array $data) {
  $fields = [
    'time' => 'is_numeric',
    'identifier' => 'is_string',
    'nonce' => 'is_string',
    'hash' => 'is_string',
  ];
  $result = $this
    ->basicAuthenticator($fields, $data);
  if (!empty($result['error'])) {
    return $result;
  }
  if (strpos($data['authenticator']['identifier'], 'TEST_') !== 0) {
    return $this
      ->errorResponse(self::ACQTEST_SUBSCRIPTION_NOT_FOUND, $this
      ->t('Subscription not found'));
  }
  switch ($data['authenticator']['identifier']) {
    case self::ACQTEST_ID:
      $key = self::ACQTEST_KEY;
      break;
    case self::ACQTEST_EXPIRED_ID:
      $key = self::ACQTEST_EXPIRED_KEY;
      break;
    case self::ACQTEST_503_ID:
      $key = self::ACQTEST_503_KEY;
      break;
    default:
      $key = self::ACQTEST_ERROR_KEY;
      break;
  }
  $hash = CryptConnector::acquiaHash($key, $data['authenticator']['time'] . ':' . $data['authenticator']['nonce']);
  $hash_simple = CryptConnector::acquiaHash($key, $data['authenticator']['time'] . ':' . $data['authenticator']['nonce']);
  if ($hash !== $data['authenticator']['hash'] && $hash_simple != $data['authenticator']['hash']) {
    return $this
      ->errorResponse(self::ACQTEST_SUBSCRIPTION_VALIDATION_ERROR, $this
      ->t('HMAC validation error: @expected != @actual', [
      '@expected' => $hash,
      '@actual' => $data['authenticator']['hash'],
    ]));
  }
  if ($key === self::ACQTEST_EXPIRED_KEY) {
    return $this
      ->errorResponse(self::ACQTEST_SUBSCRIPTION_EXPIRED, $this
      ->t('Subscription expired.'));
  }

  // Record connections.
  $connections = \Drupal::state()
    ->get('test_connections' . $data['authenticator']['identifier']);
  $connections++;
  \Drupal::state()
    ->set('test_connections' . $data['authenticator']['identifier'], $connections);
  if ($connections == 3 && $data['authenticator']['identifier'] == self::ACQTEST_503_ID) {

    // Trigger a 503 response on 3rd call to this (1st is
    // acquia.agent.subscription and 2nd is acquia.agent.validate)
    return $this
      ->errorResponse(self::ACQTEST_SUBSCRIPTION_SERVICE_UNAVAILABLE, 'Subscription service unavailable.');
  }
  $result['error'] = FALSE;
  $result['body']['subscription_name'] = 'TEST_AcquiaConnectorTestID';
  $result['body']['active'] = 1;
  $result['body']['href'] = 'http://acquia.com/network';
  $result['body']['expiration_date']['value'] = '2023-10-08T06:30:00';
  $result['body']['product'] = '91990';
  $result['body']['derived_key_salt'] = $data['authenticator']['identifier'] . '_KEY_SALT';
  $result['body']['update_service'] = 1;
  $result['body']['search_service_enabled'] = 1;
  $result['body']['uuid'] = self::ACQTEST_UUID;
  if (isset($data['body']['rpc_version'])) {
    $result['body']['rpc_version'] = $data['body']['rpc_version'];
  }
  $result['secret']['data'] = $data;
  $result['secret']['nid'] = '91990';
  $result['secret']['node'] = $data['authenticator']['identifier'] . '_NODE';
  $result['secret']['key'] = $key;

  // $result['secret']['nonce'] = '';.
  $result['authenticator'] = $data['authenticator'];
  $result['authenticator']['hash'] = '';
  $result['authenticator']['time'] += 1;
  $result['authenticator']['nonce'] = $data['authenticator']['nonce'];
  return $result;
}