diff --git a/apiConfig.readonly.json b/apiConfig.readonly.json index 4b516b9..66c9adb 100644 --- a/apiConfig.readonly.json +++ b/apiConfig.readonly.json @@ -1,5 +1,5 @@ { - "version": "2.0.10", + "version": "2.0.11", "tokenTimeout": 60, "apiRequestRatePerMinute": 256 } \ No newline at end of file diff --git a/composer.json b/composer.json index 71b3e03..afe5dbe 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "type": "library", "description": "LDC CRM Client V2", "license": "MIT", - "version": "2.0.10", + "version": "2.0.11", "authors": [ { "name": "Jérémy Munsch", diff --git a/src/Enums/ApiError.php b/src/Enums/ApiError.php index 3e6769d..b4d7a17 100644 --- a/src/Enums/ApiError.php +++ b/src/Enums/ApiError.php @@ -47,6 +47,9 @@ use Kwaadpepper\Enum\BaseEnum; * a replay attack scenario. Timestamps are provided in headers * and in the http body * + * @method static self missingParameter() + * The request is missing some parameters + * * @method static self throttleRequestsException() * Too many requests have been attempted. The is a throttle security. * Normally you should not trigger this. @@ -78,6 +81,7 @@ class ApiError extends BaseEnum 'singleOptinCustomerExists' => 120, 'methodNotAllowed' => 150, 'wrongTimeStamp' => 151, + 'missingParameter' => 400, 'authorizationException' => 403, 'throttleRequestsException' => 429 ]; @@ -96,6 +100,7 @@ class ApiError extends BaseEnum 'needAuth' => 'Vous devez être authentifié', 'methodNotAllowed' => 'This method is not allowed for this request', 'wrongTimeStamp' => 'Your request is malformed, please check timestamp header and parameter', + 'missingParameter' => 'The request is missing some parameters', 'authorizationException' => 'You are not autorized to access this', 'throttleRequestsException' => 'Throttle: too many request have been attempted, please retry later' ]; 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); } }