CognitoIdentityProvider.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace Aws\CognitoIdentity;
  3. use Aws\Credentials\Credentials;
  4. use GuzzleHttp\Promise;
  5. class CognitoIdentityProvider
  6. {
  7. /** @var CognitoIdentityClient */
  8. private $client;
  9. /** @var string */
  10. private $identityPoolId;
  11. /** @var string|null */
  12. private $accountId;
  13. /** @var array */
  14. private $logins;
  15. public function __construct(
  16. $poolId,
  17. array $clientOptions,
  18. array $logins = [],
  19. $accountId = null
  20. ) {
  21. $this->identityPoolId = $poolId;
  22. $this->logins = $logins;
  23. $this->accountId = $accountId;
  24. $this->client = new CognitoIdentityClient($clientOptions + [
  25. 'credentials' => false,
  26. ]);
  27. }
  28. public function __invoke()
  29. {
  30. return Promise\Coroutine::of(function () {
  31. $params = $this->logins ? ['Logins' => $this->logins] : [];
  32. $getIdParams = $params + ['IdentityPoolId' => $this->identityPoolId];
  33. if ($this->accountId) {
  34. $getIdParams['AccountId'] = $this->accountId;
  35. }
  36. $id = (yield $this->client->getId($getIdParams));
  37. $result = (yield $this->client->getCredentialsForIdentity([
  38. 'IdentityId' => $id['IdentityId'],
  39. ] + $params));
  40. yield new Credentials(
  41. $result['Credentials']['AccessKeyId'],
  42. $result['Credentials']['SecretKey'],
  43. $result['Credentials']['SessionToken'],
  44. (int) $result['Credentials']['Expiration']->format('U')
  45. );
  46. });
  47. }
  48. public function updateLogin($key, $value)
  49. {
  50. $this->logins[$key] = $value;
  51. return $this;
  52. }
  53. }