Skip to content

Commit

Permalink
fix(security): better scopes loading
Browse files Browse the repository at this point in the history
sidux committed Feb 28, 2024
1 parent a849bac commit 610d5aa
Showing 3 changed files with 26 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/Definition/Loader/OpenApiDefinitionLoader.php
Original file line number Diff line number Diff line change
@@ -298,6 +298,7 @@ private function getSecurities(array $securitySchemes, array $requirements = [])
if ($scheme->type === 'http') {
$collection[] = new HttpSecurity($name, $scheme->scheme, $scheme->bearerFormat);
}
$supportedScopes = [];
if ($scheme->type === 'oauth2' && $scheme->flows !== null) {
$flows = (array) $scheme->flows->getSerializableData();
$notFoundRequirements = [];
@@ -311,45 +312,48 @@ private function getSecurities(array $securitySchemes, array $requirements = [])
if (is_object($flowScopes)) {
$flowScopes = array_keys((array) $flowScopes);
}
$diff = array_diff($scopes, $flowScopes);
if (\count($diff) > 0) {
$notFoundRequirements[$type] = $diff;
$flowSupportedScopes = array_values(array_intersect($scopes, $flowScopes));
$supportedScopes[] = $flowSupportedScopes;
if (count($flowSupportedScopes) === 0) {
continue;
}
$notFoundRequirements = [];
$scopes = Scopes::fromNames($scopes);
$flowSupportedScopes = Scopes::fromNames($flowSupportedScopes);
$securityName = $name . '_' . $type;
if ($type === 'implicit') {
$collection[] = new OAuth2ImplicitSecurity(
$securityName,
$flow->authorizationUrl,
$scopes
$flowSupportedScopes
);
}
if ($type === 'password') {
$collection[] = new OAuth2PasswordSecurity(
$securityName,
$flow->tokenUrl,
$scopes
$flowSupportedScopes
);
}
if ($type === 'clientCredentials') {
$collection[] = new OAuth2ClientCredentialsSecurity(
$securityName,
$flow->tokenUrl,
$scopes
$flowSupportedScopes
);
}
if ($type === 'authorizationCode') {
$collection[] = new OAuth2AuthorizationCodeSecurity(
$securityName,
$flow->authorizationUrl,
$flow->tokenUrl,
$scopes,
$flowSupportedScopes,
);
}
}
if (count($notFoundRequirements) >= count($flows)) {
$notFoundRequirements = array_diff(
$requirements[$name],
array_unique(array_merge(...$supportedScopes))
);
if ($notFoundRequirements !== []) {
$notFoundRequirements = Json::encode($notFoundRequirements);
throw new DefinitionLoadingException(
"Scopes '{$notFoundRequirements}' not configured in securitySchemes"
@@ -400,7 +404,7 @@ private function getExamples(\cebe\openapi\spec\Operation $operation, array $par
if ($parameter->schema instanceof Schema && $parameter->schema->example !== null) {
$operationExample = $this->getExample('default', $examples, $successStatusCode);
try {
$example = $this->extractDeepExamples($parameter->schema);
$example = $this->extractDeepExamples($parameter->schema, path: 'parameter.' . $parameter->name);
} catch (InvalidExampleException $e) {
$this->logger->warning($e->getMessage());
continue;
10 changes: 10 additions & 0 deletions src/Preparator/TestCasesPreparator.php
Original file line number Diff line number Diff line change
@@ -15,6 +15,8 @@
use APITester\Test\TestCase;
use APITester\Util\Json;
use APITester\Util\Object_;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\Yaml\Tag\TaggedValue;
use Vural\OpenAPIFaker\Options;
@@ -26,10 +28,13 @@ abstract class TestCasesPreparator

protected PreparatorConfig $config;

protected LoggerInterface $logger;

public function __construct()
{
$this->tokens = new Tokens();
$this->config = $this->newConfigInstance(static::getConfigFQCN());
$this->logger = new NullLogger();
}

/**
@@ -118,6 +123,11 @@ final public function getConfig(): PreparatorConfig
return $this->config;
}

final public function setLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
}

/**
* @return class-string<PreparatorConfig>
*/
1 change: 1 addition & 0 deletions src/Test/Suite.php
Original file line number Diff line number Diff line change
@@ -186,6 +186,7 @@ private function prepareTestCases(): void
/** @var Collection<int, TestCase> $allTests */
$allTests = collect();
foreach ($this->preparators as $preparator) {
$preparator->setLogger($this->logger);
$operations = $this->api->getOperations()
->map(
static fn (Operation $op) => $op->setPreparator($preparator::getName())

0 comments on commit 610d5aa

Please sign in to comment.