Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DS-10606 extent api requests to allow for basic auth #85

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions src/Auth/AuthProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,36 @@ 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',
'Bearer ' . $this->getJwtToken()->getToken()
);
}

private function addBasicAuthHeaderToRequest(
RequestInterface $request,
string $basicAuthHeader,
): RequestInterface {
return $request->withHeader(
'Authorization',
$basicAuthHeader
);
}

public function requestJwtToken()
{
$httpResponse = $this->httpClient->send(
Expand Down
5 changes: 4 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
43 changes: 43 additions & 0 deletions src/Common/AbstractApiRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe throw some :void return types on these setters, assuming this won't be digested by any service lower than PHP 7.1

{
$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;
}
2 changes: 1 addition & 1 deletion src/Common/AbstractCollectionApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Loading