-
Notifications
You must be signed in to change notification settings - Fork 97
Add AWS IAM authorization support #64
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,15 @@ | ||
<?php | ||
|
||
namespace GraphQL\Auth; | ||
|
||
use GuzzleHttp\Psr7\Request; | ||
|
||
interface AuthInterface | ||
{ | ||
/** | ||
* @param Request $request | ||
* @param array $options | ||
* @return Request | ||
*/ | ||
public function run(Request $request, array $options = []): Request; | ||
} |
This file contains hidden or 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,62 @@ | ||
<?php | ||
|
||
namespace GraphQL\Auth; | ||
|
||
use Aws\Credentials\Credentials; | ||
use Aws\Credentials\CredentialProvider; | ||
use Aws\Signature\SignatureV4; | ||
use GraphQL\Exception\AwsRegionNotSetException; | ||
use GraphQL\Exception\MissingAwsSdkPackageException; | ||
use GuzzleHttp\Psr7\Request; | ||
|
||
class AwsIamAuth implements AuthInterface | ||
{ | ||
protected const SERVICE_NAME = 'appsync'; | ||
|
||
/** | ||
* @codeCoverageIgnore | ||
* | ||
* AwsIamAuth constructor. | ||
*/ | ||
public function __construct() | ||
{ | ||
if (!class_exists('\Aws\Signature\SignatureV4')) { | ||
throw new MissingAwsSdkPackageException(); | ||
} | ||
} | ||
|
||
/** | ||
* @param Request $request | ||
* @param array $options | ||
* @return Request | ||
*/ | ||
public function run(Request $request, array $options = []): Request | ||
{ | ||
$region = $options['aws_region'] ?? null; | ||
if ($region === null) { | ||
throw new AwsRegionNotSetException(); | ||
} | ||
return $this->getSignature($region)->signRequest( | ||
$request, $this->getCredentials(), | ||
self::SERVICE_NAME | ||
); | ||
} | ||
|
||
/** | ||
* @param string $region | ||
* @return SignatureV4 | ||
*/ | ||
protected function getSignature(string $region): SignatureV4 | ||
{ | ||
return new SignatureV4(self::SERVICE_NAME, $region); | ||
} | ||
|
||
/** | ||
* @return Credentials | ||
*/ | ||
protected function getCredentials(): Credentials | ||
{ | ||
$provider = CredentialProvider::defaultProvider(); | ||
return $provider()->wait(); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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,18 @@ | ||
<?php | ||
|
||
namespace GraphQL\Exception; | ||
|
||
use RunTimeException; | ||
|
||
/** | ||
* Class AwsRegionNotSetException | ||
* | ||
* @package GraphQL\Exception | ||
*/ | ||
class AwsRegionNotSetException extends RunTimeException | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct("AWS region not set."); | ||
} | ||
} |
This file contains hidden or 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,26 @@ | ||
<?php | ||
|
||
namespace GraphQL\Exception; | ||
|
||
use RunTimeException; | ||
|
||
/** | ||
* Class MissingAwsSdkPackageException | ||
* | ||
* @package GraphQL\Exception | ||
*/ | ||
class MissingAwsSdkPackageException extends RunTimeException | ||
{ | ||
/** | ||
* @codeCoverageIgnore | ||
* | ||
* MissingAwsSdkPackageException constructor. | ||
*/ | ||
public function __construct() | ||
{ | ||
parent::__construct( | ||
'To be able to use AWS IAM authorization you should | ||
install "aws/aws-sdk-php" as a project dependency.' | ||
); | ||
} | ||
} |
This file contains hidden or 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,49 @@ | ||
<?php | ||
|
||
namespace GraphQL\Tests\Auth; | ||
|
||
use GraphQL\Auth\AwsIamAuth; | ||
use GraphQL\Exception\AwsRegionNotSetException; | ||
use GuzzleHttp\Psr7\Request; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class AwsIamAuthTest extends TestCase | ||
{ | ||
/** | ||
* @var AwsIamAuth | ||
*/ | ||
protected $auth; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->auth = new AwsIamAuth(); | ||
} | ||
|
||
/** | ||
* @covers \GraphQL\Auth\AwsIamAuth::run | ||
* @covers \GraphQL\Exception\AwsRegionNotSetException::__construct | ||
*/ | ||
public function testRunMissingRegion() | ||
{ | ||
$this->expectException(AwsRegionNotSetException::class); | ||
$request = new Request('POST', ''); | ||
$this->auth->run($request, []); | ||
} | ||
|
||
/** | ||
* @covers \GraphQL\Auth\AwsIamAuth::run | ||
* @covers \GraphQL\Auth\AwsIamAuth::getSignature | ||
* @covers \GraphQL\Auth\AwsIamAuth::getCredentials | ||
*/ | ||
public function testRunSuccess() | ||
{ | ||
$request = $this->auth->run( | ||
new Request('POST', ''), | ||
['aws_region' => 'us-east-1'] | ||
); | ||
$headers = $request->getHeaders(); | ||
$this->assertArrayHasKey('X-Amz-Date', $headers); | ||
$this->assertArrayHasKey('X-Amz-Security-Token', $headers); | ||
$this->assertArrayHasKey('Authorization', $headers); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.