From e9623052c3810aab7fcc2bd2e5adfc4c513a0c65 Mon Sep 17 00:00:00 2001 From: Joseph Muto Date: Tue, 16 Jan 2024 17:16:31 -0500 Subject: [PATCH] DS-10606 extent api requests to allow for basic auth --- src/Auth/AuthProxy.php | 27 ++++++++---- src/Client.php | 5 ++- src/Common/AbstractApiRequest.php | 43 ++++++++++++++++++++ src/Common/AbstractCollectionApiResponse.php | 2 +- 4 files changed, 68 insertions(+), 9 deletions(-) diff --git a/src/Auth/AuthProxy.php b/src/Auth/AuthProxy.php index 35ee64d..191b0ff 100644 --- a/src/Auth/AuthProxy.php +++ b/src/Auth/AuthProxy.php @@ -77,16 +77,19 @@ public function getUri(): UriInterface /** * @throws GuzzleException */ - public function request(RequestInterface $request): ResponseInterface - { - // Add Auth Header to request. + public function request( + RequestInterface $request, + string $basicAuthHeader = null, + ): ResponseInterface { + // Add Auth Header Type to request. // Submit Request - return $this->httpClient->send( - $this->addAuthHeaderToRequest($request) - ); + $header = empty($basicAuthHeader) + ? $this->addBearerAuthHeaderToRequest($request) + : $this->addBasicAuthHeaderToRequest($request, $basicAuthHeader); + return $this->httpClient->send($header); } - private function addAuthHeaderToRequest(RequestInterface $request): RequestInterface + private function addBearerAuthHeaderToRequest(RequestInterface $request): RequestInterface { return $request->withHeader( 'Authorization', @@ -94,6 +97,16 @@ private function addAuthHeaderToRequest(RequestInterface $request): RequestInter ); } + private function addBasicAuthHeaderToRequest( + RequestInterface $request, + string $basicAuthHeader, + ): RequestInterface { + return $request->withHeader( + 'Authorization', + $basicAuthHeader + ); + } + public function requestJwtToken() { $httpResponse = $this->httpClient->send( diff --git a/src/Client.php b/src/Client.php index a05f483..cc49356 100755 --- a/src/Client.php +++ b/src/Client.php @@ -36,7 +36,10 @@ public function request(AbstractApiRequest $apiRequest): Common\Entity\AbstractE $httpRequest = $this->generateHttpRequest($apiRequest); // Pass Request thru AuthProxy - $httpResponse = $this->authProxy->request($httpRequest); + $httpResponse = $this->authProxy->request( + $httpRequest, + $apiRequest->getBasicAuthHeaderIfSet() + ); // Generate Response $responseObject = $apiRequest->getResponseObject(); diff --git a/src/Common/AbstractApiRequest.php b/src/Common/AbstractApiRequest.php index 88adb75..e846a9d 100644 --- a/src/Common/AbstractApiRequest.php +++ b/src/Common/AbstractApiRequest.php @@ -12,6 +12,9 @@ abstract class AbstractApiRequest implements JsonSerializable protected $httpEndpoint = '/'; protected $httpMethod = 'GET'; protected $queryParams = ''; + protected $hasBasicAuth = false; + protected $basicAuthUser = ''; + protected $basicAuthPassword = ''; // Endpoint: /api/user public function getHttpEndpoint(): string @@ -31,6 +34,46 @@ public function getQueryParams(): string return $this->queryParams; } + public function getBasicAuthHeaderIfSet(): ?string + { + if (!$this->isBasicAuth()) { + return null; + } + return 'Basic ' . base64_encode( + "{$this->getBasicAuthUser()}:{$this->getBasicAuthPassword()}" + ); + } + + public function isBasicAuth(): bool + { + return $this->hasBasicAuth; + } + + public function setHasBasicAuth($hasBasicAuth) + { + $this->hasBasicAuth = $hasBasicAuth; + } + + public function getBasicAuthUser(): string + { + return $this->basicAuthUser; + } + + public function setBasicAuthUser($basicAuthUser) + { + $this->basicAuthUser = $basicAuthUser; + } + + public function getBasicAuthPassword(): string + { + return $this->basicAuthPassword; + } + + public function setBasicAuthPassword($basicAuthPassword) + { + $this->basicAuthPassword = $basicAuthPassword; + } + // Response Object: abstract public function getResponseObject(): AbstractEntity; } diff --git a/src/Common/AbstractCollectionApiResponse.php b/src/Common/AbstractCollectionApiResponse.php index 708a910..a3f35b9 100644 --- a/src/Common/AbstractCollectionApiResponse.php +++ b/src/Common/AbstractCollectionApiResponse.php @@ -102,7 +102,7 @@ public function offsetUnset($offset) * * {@link http://us2.php.net/manual/en/class.countable.php} */ - public function count() + public function count(): int { return count($this->data); }