Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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