You are here

public function NspiController::getCredentials 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::getCredentials()
  2. 3.x tests/modules/src/Controller/NspiController.php \Drupal\acquia_connector_test\Controller\NspiController::getCredentials()

Test returns subscriptions for an email.

Parameters

\GuzzleHttp\Psr7\Request $request: Request.

Return value

\GuzzleHttp\Psr7\Response JsonResponse.

File

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

Class

NspiController
Class NspiController.

Namespace

Drupal\acquia_connector_test\Controller

Code

public function getCredentials(Request $request) {
  $data = json_decode($request
    ->getBody(), TRUE);
  $fields = [
    'time' => 'is_numeric',
    'nonce' => 'is_string',
    'hash' => 'is_string',
  ];
  $result = $this
    ->basicAuthenticator($fields, $data);
  if (!empty($result['error'])) {
    return new Response(self::ACQTEST_SUBSCRIPTION_SERVICE_UNAVAILABLE, [], $result);
  }
  if (!empty($data['body']['email'])) {
    $account = user_load_by_mail($data['body']['email']);
    $this
      ->getLogger('getCredentials password')
      ->debug($account
      ->getPassword());
    if (empty($account) || $account
      ->isAnonymous()) {
      return new Response(self::ACQTEST_SUBSCRIPTION_SERVICE_UNAVAILABLE, [], json_encode($this
        ->errorResponse(self::ACQTEST_SUBSCRIPTION_VALIDATION_ERROR, $this
        ->t('Account not found'))));
    }
  }
  else {
    return new Response(self::ACQTEST_SUBSCRIPTION_SERVICE_UNAVAILABLE, [], json_encode($this
      ->errorResponse(self::ACQTEST_SUBSCRIPTION_VALIDATION_ERROR, $this
      ->t('Invalid arguments'))));
  }
  $hash = CryptConnector::acquiaHash($account
    ->getPassword(), $data['authenticator']['time'] . ':' . $data['authenticator']['nonce']);
  if ($hash === $data['authenticator']['hash']) {
    $result = [];
    $result['is_error'] = FALSE;
    $result['body']['subscription'][] = [
      'identifier' => self::ACQTEST_ID,
      'key' => self::ACQTEST_KEY,
      'name' => self::ACQTEST_ID,
    ];
    return new Response(200, [], json_encode($result));
  }
  else {
    return new Response(self::ACQTEST_SUBSCRIPTION_SERVICE_UNAVAILABLE, [], json_encode($this
      ->errorResponse(self::ACQTEST_SUBSCRIPTION_VALIDATION_ERROR, $this
      ->t('Incorrect password.'))));
  }
}