You are here

function acquia_connector_test_validate_authenticator in Acquia Connector 7.3

Same name and namespace in other branches
  1. 7.2 acquia_agent/tests/acquia_connector_test.module \acquia_connector_test_validate_authenticator()

Needs comment.

3 calls to acquia_connector_test_validate_authenticator()
acquia_connector_test_nspi_update in acquia_agent/tests/acquia_connector_test.module
Test update.
acquia_connector_test_subscription in acquia_agent/tests/acquia_connector_test.module
Test subscription.
acquia_connector_test_validate in acquia_agent/tests/acquia_connector_test.module
Test validate.

File

acquia_agent/tests/acquia_connector_test.module, line 421
Test endpoint for Acquia Connector XML-RPC calls.

Code

function acquia_connector_test_validate_authenticator($data) {
  $fields = array(
    'time' => 'is_numeric',
    'identifier' => 'is_string',
    'nonce' => 'is_string',
    'hash' => 'is_string',
  );
  $result = acquia_connector_test_basic_authenticator($fields, $data);
  if (!empty($result->is_error)) {
    return $result;
  }
  if (strpos($data['authenticator']['identifier'], 'TEST_') !== 0) {
    $result->code = ACQUIA_CONNECTOR_TEST_SUBSCRIPTION_NOT_FOUND;
    $result->message = t('Subscription not found.');
    $result->is_error = TRUE;
    return $result;
  }
  switch ($data['authenticator']['identifier']) {
    case ACQUIA_CONNECTOR_TEST_ID:
      $key = ACQUIA_CONNECTOR_TEST_KEY;
      break;
    case ACQUIA_CONNECTOR_TEST_EXPIRED_ID:
      $key = ACQUIA_CONNECTOR_TEST_EXPIRED_KEY;
      break;
    case ACQUIA_CONNECTOR_TEST_503_ID:
      $key = ACQUIA_CONNECTOR_TEST_503_KEY;
      break;
    case ACQUIA_CONNECTOR_TEST_REVOKED_ID:
      $key = ACQUIA_CONNECTOR_TEST_REVOKED_KEY;
      break;
    default:
      $key = ACQUIA_CONNECTOR_TEST_ERROR_KEY;
      break;
  }
  $hash = _acquia_agent_hmac($key, $data['authenticator']['time'], $data['authenticator']['nonce'], $data['body']);
  $hash_simple = _acquia_agent_hmac($key, $data['authenticator']['time'], $data['authenticator']['nonce'], array());
  if ($hash !== $data['authenticator']['hash'] && $hash_simple != $data['authenticator']['hash']) {
    $result->code = ACQUIA_CONNECTOR_TEST_SUBSCRIPTION_VALIDATION_ERROR;
    $result->message = t('HMAC validation error: @hash1 != @hash2', array(
      '@hash1' => $hash,
      '@hash2' => $data['authenticator']['hash'],
    ));
    $result->is_error = TRUE;
    return $result;
  }
  if ($key === ACQUIA_CONNECTOR_TEST_EXPIRED_KEY) {
    $result->code = ACQUIA_CONNECTOR_TEST_SUBSCRIPTION_EXPIRED;
    $result->message = t('Subscription expired.');
    $result->is_error = TRUE;
    return $result;
  }

  // Record connections.
  $connections = variable_get('acquia_connector_test_connections' . $data['authenticator']['identifier'], 0);
  $connections++;
  variable_set('acquia_connector_test_connections' . $data['authenticator']['identifier'], $connections);
  if ($connections == 3 && $data['authenticator']['identifier'] == ACQUIA_CONNECTOR_TEST_503_ID) {
    $result->code = 9999;
    $result->message = t('General error.');
    $result->is_error = TRUE;
    return $result;
  }
  $result->is_error = FALSE;
  $result->body['active'] = 1;
  $result->body['href'] = 'TEST';
  $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'] = ACQUIA_CONNECTOR_TEST_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->authenticator = $data['authenticator'];
  $result->authenticator['hash'] = '';
  $result->authenticator['time'] += 1;
  return $result;
}