-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
omar.fawzy
committed
Jul 27, 2022
1 parent
b594b4d
commit 7082b03
Showing
6 changed files
with
130 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace App\Modules\OpenApi\Factories; | ||
|
||
use App\Modules\Api\Middlewares\ApiMiddleware; | ||
use App\Modules\OpenApi\Middlewares\Middleware; | ||
use InvalidArgumentException; | ||
|
||
class AuthenticationFactory | ||
{ | ||
private const BEARER_AUTH = 'bearerAuth'; | ||
|
||
public function make(string $securityMethod, array $context = []): Middleware | ||
{ | ||
return match ($securityMethod) { | ||
self::BEARER_AUTH => new ApiMiddleware(), | ||
default => throw new InvalidArgumentException("Security method: $securityMethod is not supported for this operation."), | ||
}; | ||
} | ||
} |
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,10 @@ | ||
<?php | ||
|
||
namespace App\Modules\OpenApi\Middlewares; | ||
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
interface Middleware | ||
{ | ||
public function handle(ServerRequestInterface $serverRequest): bool; | ||
} |
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,61 @@ | ||
<?php | ||
|
||
namespace App\Modules\OpenApi\Services; | ||
|
||
use App\Modules\OpenApi\Factories\AuthenticationFactory; | ||
use cebe\openapi\spec\OpenApi; | ||
use Exception; | ||
use Illuminate\Validation\UnauthorizedException; | ||
use League\OpenAPIValidation\PSR7\PathFinder; | ||
use League\OpenAPIValidation\PSR7\SpecFinder; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
class AuthenticationManager | ||
{ | ||
public function __construct(private AuthenticationFactory $authenticationFactory) | ||
{ | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
public function authenticate(ServerRequestInterface $serverRequest, OpenApi $openApi): void | ||
{ | ||
$pathFinder = new PathFinder($openApi, $serverRequest->getUri(), $serverRequest->getMethod()); | ||
|
||
$operationAddresses = $pathFinder->search(); | ||
|
||
if (empty($operationAddresses)) { | ||
throw new Exception("Operation with uri: {$serverRequest->getUri()} doesn't exist in the open api specs."); | ||
} | ||
|
||
if (count($operationAddresses) > 1) { | ||
throw new Exception( | ||
"Duplicate operations for uri: {$serverRequest->getUri()} exist in the open api specs." | ||
); | ||
} | ||
|
||
$specFinder = new SpecFinder($openApi); | ||
|
||
$securityRequirements = $specFinder->findSecuritySpecs($operationAddresses[0]); | ||
|
||
if (empty($securityRequirements)) { | ||
return; | ||
} | ||
|
||
$successfulAuth = false; | ||
|
||
foreach ($securityRequirements as $securityRequirement) { | ||
foreach ((array)$securityRequirement->getSerializableData() as $securityMethod => $context) { | ||
$successfulAuth = $this->authenticationFactory->make($securityMethod, $context)->handle($serverRequest); | ||
if (true === $successfulAuth) { | ||
return; | ||
} | ||
} | ||
} | ||
|
||
if (false === $successfulAuth) { | ||
throw new UnauthorizedException("Unauthorized access to a protected uri: {$serverRequest->getUri()}"); | ||
} | ||
} | ||
} |
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