You are here

public function ClientFactory::authenticate in Acquia Content Hub 8.2

Makes a call to get a client response based on the client name.

Note, this receives a Symfony request, but uses a PSR7 Request to Auth.

Parameters

\Symfony\Component\HttpFoundation\Request $request: Request.

Return value

\Acquia\Hmac\KeyInterface|bool Authentication Key, FALSE otherwise.

File

src/Client/ClientFactory.php, line 240

Class

ClientFactory
Instantiates an Acquia ContentHub Client object.

Namespace

Drupal\acquia_contenthub\Client

Code

public function authenticate(Request $request) {
  if (!$this
    ->getClient()) {
    return FALSE;
  }
  $keys = [
    $this->client
      ->getSettings()
      ->getApiKey() => $this->client
      ->getSettings()
      ->getSecretKey(),
    'Webhook' => $this->client
      ->getSettings()
      ->getSharedSecret(),
  ];
  $keyLoader = new KeyLoader($keys);
  $authenticator = new RequestAuthenticator($keyLoader);

  // Authentication requires a PSR7 compatible request.
  if (class_exists(DiactorosFactory::class)) {
    $httpMessageFactory = new DiactorosFactory();
  }
  else {
    $httpMessageFactory = new PsrHttpFactory(new ServerRequestFactory(), new StreamFactory(), new UploadedFileFactory(), new ResponseFactory());
  }
  $psr7_request = $httpMessageFactory
    ->createRequest($request);
  try {
    return $authenticator
      ->authenticate($psr7_request);
  } catch (KeyNotFoundException $exception) {
    $this->loggerFactory
      ->get('acquia_contenthub')
      ->debug('HMAC validation failed. [authorization_header = %authorization_header]', [
      '%authorization_header' => $request->headers
        ->get('authorization'),
    ]);
  }
  return FALSE;
}