Handle HTTP Basic Auth with Guzzle
This commit is contained in:
parent
f7e4ddbf7e
commit
8952fb70f4
1 changed files with 29 additions and 8 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue