From 8952fb70f41a815b5fd5758d19054cc7159ea8ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Mon, 16 Aug 2021 19:21:53 +0200 Subject: [PATCH] Handle HTTP Basic Auth with Guzzle --- src/Transport/ApiInterface.php | 37 ++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/src/Transport/ApiInterface.php b/src/Transport/ApiInterface.php index 645831c..db4f597 100644 --- a/src/Transport/ApiInterface.php +++ b/src/Transport/ApiInterface.php @@ -58,6 +58,8 @@ class ApiInterface */ protected static $apiToken; + protected static $authentification = null; + public function __construct(string $endpoint = null, string $bearerToken = null, string $bearerPwd = null) { static::$endpoint = static::$endpoint ?: $endpoint; @@ -76,23 +78,33 @@ class ApiInterface if (!static::$endpoint) { throw new CrmException('Either config or .env file need to have the endpoint var set'); } + + // handle http Auth + if (\strpos(static::$endpoint, '@')) { + $vars = \parse_url(static::$endpoint); + static::$authentification = \base64_encode($vars['user'] . ':' . $vars['pass']); + } } /** * Make a crm http call to get a reponse * * @param Request $request + * @param bool $addBearer * @return \LDC\LDCCRM\Transport\Response * @throws \LDC\LDCCRM\Exceptions\CrmException if an error occurs on server side or on getting response * @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs */ - public function call(Request $request): Response + public function call(Request $request, bool $addBearer = false): Response { $headers = []; $data = []; $this->setHeaders($headers); $this->setData($data); $data = array_merge($request->data, $data); + + $this->addCredentials($request, $addBearer); + $response = $this->processRequest($request, $headers, $data); try { if ($response->status->equals(ApiStatus::KO())) { @@ -188,8 +200,7 @@ class ApiInterface protected function authenticate(): bool { $request = Utils::getResquest('apiV2.authenticate'); - $this->addBearerCredentials($request); - $response = $this->call($request); + $response = $this->call($request, true); if ($response->status->equals(ApiStatus::KO())) { throw new LoginException($response->apiError->label, $response->apiError->value); } @@ -200,13 +211,23 @@ class ApiInterface * Inject bearer credentials into the request * * @param Request $request + * @param bool $addBearer * @return void */ - protected function addBearerCredentials(Request $request) + protected function addCredentials(Request $request, bool $addBearer = false) { - $request->setHidden([ - 'headers' => ['Authorization' => sprintf('Bearer %s', static::$bearerToken)], - 'data' => ['bearer_pwd' => static::$bearerPwd] - ]); + $params = []; + $authentification = []; + if (static::$authentification) { + $authentification[] = sprintf('Basic %s', static::$authentification); + } + if ($addBearer) { + $authentification[] = sprintf('Bearer %s', static::$bearerToken); + $params['data'] = ['bearer_pwd' => static::$bearerPwd]; + } + if ($authentification) { + $params['headers'] = ['Authorization' => implode(',', $authentification)]; + } + $request->setHidden($params); } }