forked from aws/aws-sdk-php
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a CognitoIdentity credential provider
- Loading branch information
Showing
4 changed files
with
124 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
namespace Aws\CognitoIdentity; | ||
|
||
use Aws\Credentials\Credentials; | ||
use GuzzleHttp\Promise; | ||
|
||
class CognitoIdentityProvider | ||
{ | ||
/** @var CognitoIdentityClient */ | ||
private $client; | ||
/** @var string */ | ||
private $identityPoolId; | ||
/** @var string|null */ | ||
private $accountId; | ||
/** @var array */ | ||
private $logins; | ||
|
||
public function __construct( | ||
$poolId, | ||
array $clientOptions, | ||
array $logins = [], | ||
$accountId = null | ||
) { | ||
$this->identityPoolId = $poolId; | ||
$this->logins = $logins; | ||
$this->accountId = $accountId; | ||
$this->client = new CognitoIdentityClient($clientOptions + [ | ||
'credentials' => false, | ||
]); | ||
} | ||
|
||
public function __invoke() | ||
{ | ||
return Promise\coroutine(function () { | ||
$params = $this->logins ? ['Logins' => $this->logins] : []; | ||
$getIdParams = $params + ['IdentityPoolId' => $this->identityPoolId]; | ||
if ($this->accountId) { | ||
$getIdParams['AccountId'] = $this->accountId; | ||
} | ||
|
||
$id = (yield $this->client->getId($getIdParams)); | ||
$result = (yield $this->client->getCredentialsForIdentity([ | ||
'IdentityId' => $id['IdentityId'], | ||
] + $params)); | ||
|
||
yield new Credentials( | ||
$result['Credentials']['AccessKeyId'], | ||
$result['Credentials']['SecretKey'], | ||
$result['Credentials']['SessionToken'], | ||
(int) $result['Credentials']['Expiration']->format('U') | ||
); | ||
}); | ||
} | ||
|
||
public function updateLogin($key, $value) | ||
{ | ||
$this->logins[$key] = $value; | ||
|
||
return $this; | ||
} | ||
} |
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,56 @@ | ||
<?php | ||
namespace Aws\Test\CognitoIdentity; | ||
|
||
use Aws\Api\DateTimeResult; | ||
use Aws\CognitoIdentity\CognitoIdentityProvider; | ||
use Aws\MockHandler; | ||
use Aws\Result; | ||
use Aws\Test\UsesServiceTrait; | ||
|
||
class CognitoIdentityProviderTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testCreatesFromCognitoIdentity() | ||
{ | ||
$options = [ | ||
'region' => 'not-a-region', | ||
'version' => 'latest', | ||
'handler' => new MockHandler([ | ||
new Result(['IdentityId' => 'foo:bar:baz']), | ||
new Result([ | ||
'Credentials' => [ | ||
'AccessKeyId' => 'foo', | ||
'SecretKey' => 'bar', | ||
'SessionToken' => 'baz', | ||
'Expiration' => DateTimeResult::fromEpoch(time() + 10), | ||
] | ||
]), | ||
]), | ||
]; | ||
|
||
$provider = new CognitoIdentityProvider('poolId', $options); | ||
$credentials = call_user_func($provider)->wait(); | ||
|
||
$this->assertSame('foo', $credentials->getAccessKeyId()); | ||
$this->assertSame('bar', $credentials->getSecretKey()); | ||
$this->assertSame('baz', $credentials->getSecurityToken()); | ||
$this->assertFalse($credentials->isExpired()); | ||
} | ||
|
||
public function testAccessTokensCanBeRefreshed() | ||
{ | ||
$provider = new CognitoIdentityProvider( | ||
'poolId', | ||
['region' => 'us-east-1', 'version' => 'latest'], | ||
[ | ||
'www.amazon.com' => 'access-token-old', | ||
'graph.facebook.com' => 'access-token-fb', | ||
] | ||
); | ||
|
||
$provider->updateLogin('www.amazon.com', 'access-token-new'); | ||
$this->assertSame( | ||
'access-token-new', | ||
$this->readAttribute($provider, 'logins')['www.amazon.com'] | ||
); | ||
} | ||
} |
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