You are here

public function Base::authorizeBySecret in DRD Agent 8.3

Same name and namespace in other branches
  1. 4.0.x src/Agent/Action/Base.php \Drupal\drd_agent\Agent\Action\Base::authorizeBySecret()

Callback to authorize a DRD instance with a given secret.

Parameters

bool $debugMode: Whether we operate in debug mode.

Return value

string Encrypted and base64 encoded result from the executed action.

File

src/Agent/Action/Base.php, line 330

Class

Base
Base class for Remote DRD Action Code.

Namespace

Drupal\drd_agent\Agent\Action

Code

public function authorizeBySecret($debugMode = FALSE) : string {
  try {
    $input = $this
      ->readInput($debugMode, 'Authorize DRD by secret');
    if (empty($input['remoteSetupToken']) || empty($input['method']) || empty($input['secrets'])) {
      throw new RuntimeException('Input is incomplete');
    }
    switch ($input['method']) {
      case self::SEC_AUTH_ACQUIA:
        $required = array(
          'username',
          'password',
        );
        $local = $this
          ->getDbInfo();
        break;
      case self::SEC_AUTH_PANTHEON:
        $required = array(
          'PANTHEON_SITE',
        );
        $local = $_ENV;
        break;
      case self::SEC_AUTH_PLATFORMSH:
        $required = array(
          'PLATFORM_PROJECT',
        );
        $local = $_ENV;
        break;
      default:
        throw new RuntimeException('Unknown method.');
    }
    foreach ($required as $item) {
      if (!isset($local[$item])) {
        throw new RuntimeException('Unsupported method.');
      }
      if ($local[$item] !== $input['secrets'][$item]) {
        throw new RuntimeException('Invalid secret.');
      }
    }
    $this
      ->authorize($input['remoteSetupToken']);
  } catch (Exception $ex) {
    $this
      ->watchdog($ex
      ->getMessage(), array(), 3);

    // Let's slow down to prevent brute force.
    sleep(10);
    header('HTTP/1.1 502 Error');
    print 'error';
    exit;
  }
  return 'ok';
}