Skip to content

Commit

Permalink
feat(api tester): enable specific user authentication for given endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzov committed Nov 4, 2024
1 parent 189326d commit c088017
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/Authenticator/Authenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function authenticate(Auth $config, Api $api, Requester $requester): Toke
$body['access_token'],
explode(' ', $config->getBody()['scope'] ?? ''),
$body['refresh_token'] ?? null,
$config->getFilters(),
$body['token_type'] ?? null,
$body['expires_in'] ?? null,
);
Expand Down
13 changes: 13 additions & 0 deletions src/Config/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,22 @@ final class Auth
*/
private array $body = [];

private Filters $filters;

public function __construct(
private readonly string $name
) {
$this->filters = new Filters();
}

public function getFilters(): Filters
{
return $this->filters;
}

public function setFilters(Filters $filters): void
{
$this->filters = $filters;
}

public function getName(): string
Expand Down
25 changes: 16 additions & 9 deletions src/Definition/Example/OperationExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public function setParameter(
foreach ($value as $attribute => $attributeValue) {
$this->{$paramProp}[$name][$attribute] = (string) $attributeValue;
}

return $this;
}

Expand Down Expand Up @@ -239,8 +240,8 @@ public function getQueryParameters(): array
->count()
;
if ($this->forceRandom || ($this->autoComplete && \count(
$this->queryParameters
) < $definitionParamsCount)) {
$this->queryParameters
) < $definitionParamsCount)) {
$randomQueryParams = $this->parent
->getQueryParameters()
->getRandomExamples()
Expand Down Expand Up @@ -278,8 +279,8 @@ public function getHeaders(): array

if ($this->parent !== null) {
$definitionHeadersCount = $this->parent
->getHeaders()
->count() + 1 // content-type
->getHeaders()
->count() + 1 // content-type
;

if ($this->parent->getSecurities()->count() > 0) {
Expand Down Expand Up @@ -383,16 +384,22 @@ public function setAuthenticationHeaders(Tokens $tokens, bool $ignoreScope = fal
->toArray()
;

$filteredTokens = $tokens->filter(static fn (Token $token) => $token->supportsOperation($operation))
->toArray()
;

if ($ignoreScope) {
/** @var Token|null $token */
$token = $tokens->first();
} else {
/** @var Token|null $token */
$token = $tokens->where(
'scopes',
'includes',
$scopes
)->first();
$token = !empty($filteredTokens)
? reset($filteredTokens)
: $tokens->where(
'scopes',
'includes',
$scopes
)->first();
}

if ($token !== null) {
Expand Down
22 changes: 22 additions & 0 deletions src/Definition/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace APITester\Definition;

use APITester\Config\Filters;
use APITester\Util\Filterable;

final class Token
{
private readonly string $type;
Expand All @@ -19,13 +22,32 @@ public function __construct(
private readonly string $accessToken,
private readonly array $scopes = [],
private readonly ?string $refreshToken = null,
private readonly ?Filters $filters = new Filters(),
?string $type = null,
?int $expiresIn = null
) {
$this->type = $type ?? 'Bearer';
$this->expiresIn = $expiresIn ?? 3600;
}

public function getFilters(): ?Filters
{
return $this->filters;
}

public function supportsOperation(Filterable $operation): bool
{
foreach ($this->filters->getInclude() as $item) {

Check failure on line 40 in src/Definition/Token.php

View workflow job for this annotation

GitHub Actions / PHPStan

Cannot call method getInclude() on APITester\Config\Filters|null.
foreach ($item as $value) {
if ($operation->has('id', $value)) {
return true;
}
}
}

return false;
}

public function getAccessToken(): string
{
return $this->accessToken;
Expand Down

0 comments on commit c088017

Please sign in to comment.