-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from DoclerLabs/add-bearer-authentication
Add bearer authentication
- Loading branch information
Showing
10 changed files
with
253 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DoclerLabs\ApiClientGenerator\Generator\Security; | ||
|
||
use cebe\openapi\spec\SecurityRequirement; | ||
use cebe\openapi\spec\SecurityScheme; | ||
use DoclerLabs\ApiClientGenerator\Ast\Builder\CodeBuilder; | ||
use DoclerLabs\ApiClientGenerator\Entity\Operation; | ||
use DoclerLabs\ApiClientGenerator\Generator\SecurityStrategyInterface; | ||
use DoclerLabs\ApiClientGenerator\Input\Specification; | ||
|
||
class BearerAuthentication implements SecurityStrategyInterface | ||
{ | ||
private const PROPERTY_NAME = 'bearerToken'; | ||
private const SCHEME = 'bearer'; | ||
private const TYPE = 'http'; | ||
|
||
private CodeBuilder $builder; | ||
|
||
public function __construct(CodeBuilder $builder) | ||
{ | ||
$this->builder = $builder; | ||
} | ||
|
||
public function getProperties(Operation $operation, Specification $specification): array | ||
{ | ||
$statements = []; | ||
|
||
if ($this->requiresBearerToken($operation, $specification)) { | ||
$statements[] = $this->builder->localProperty('bearerToken', 'string', 'string'); | ||
} | ||
|
||
return $statements; | ||
} | ||
|
||
public function getConstructorParams(Operation $operation, Specification $specification): array | ||
{ | ||
$params = []; | ||
|
||
if ($this->requiresBearerToken($operation, $specification)) { | ||
$params[] = $this->builder | ||
->param(self::PROPERTY_NAME) | ||
->setType('string') | ||
->getNode(); | ||
} | ||
|
||
return $params; | ||
} | ||
|
||
public function getConstructorParamInits(Operation $operation, Specification $specification): array | ||
{ | ||
$paramInits = []; | ||
|
||
if ($this->requiresBearerToken($operation, $specification)) { | ||
$paramInits[] = $this->builder->assign( | ||
$this->builder->localPropertyFetch(self::PROPERTY_NAME), | ||
$this->builder->var(self::PROPERTY_NAME) | ||
); | ||
} | ||
|
||
return $paramInits; | ||
} | ||
|
||
public function getSecurityHeaders(Operation $operation, Specification $specification): array | ||
{ | ||
$headers = []; | ||
|
||
foreach ($this->loopSecuritySchemes($operation, $specification) as $securityScheme) { | ||
/** @var SecurityScheme $securityScheme */ | ||
if ( | ||
$securityScheme->scheme === self::SCHEME | ||
&& $securityScheme->type === self::TYPE | ||
) { | ||
$headers['Authorization'] = $this->builder->funcCall( | ||
'sprintf', | ||
['Bearer %s', $this->builder->localPropertyFetch(self::PROPERTY_NAME)] | ||
); | ||
} | ||
} | ||
|
||
return $headers; | ||
} | ||
|
||
private function requiresBearerToken(Operation $operation, Specification $specification): bool | ||
{ | ||
foreach ($this->loopSecuritySchemes($operation, $specification) as $securityScheme) { | ||
/** @var SecurityScheme $securityScheme */ | ||
if ( | ||
$securityScheme->scheme === self::SCHEME | ||
&& $securityScheme->type === self::TYPE | ||
) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private function loopSecuritySchemes(Operation $operation, Specification $specification): iterable | ||
{ | ||
if ( | ||
!empty($specification->getSecuritySchemes()) | ||
&& !empty($operation->getSecurity()) | ||
) { | ||
/** @var SecurityScheme $securityScheme */ | ||
foreach ($specification->getSecuritySchemes() as $name => $securityScheme) { | ||
/** @var SecurityRequirement $securityRequirement */ | ||
foreach ($operation->getSecurity() as $securityRequirement) { | ||
if (isset($securityRequirement->$name)) { | ||
yield $securityScheme; | ||
} | ||
} | ||
} | ||
} | ||
|
||
yield from []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DoclerLabs\ApiClientGenerator\Generator; | ||
|
||
use DoclerLabs\ApiClientGenerator\Entity\Operation; | ||
use DoclerLabs\ApiClientGenerator\Input\Specification; | ||
|
||
interface SecurityStrategyInterface | ||
{ | ||
public function getProperties(Operation $operation, Specification $specification): array; | ||
|
||
public function getConstructorParams(Operation $operation, Specification $specification): array; | ||
|
||
public function getConstructorParamInits(Operation $operation, Specification $specification): array; | ||
|
||
public function getSecurityHeaders(Operation $operation, Specification $specification): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.