Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
namespace Aws\Credentials;
3
 
1441 ariadna 4
use Aws\Identity\AwsCredentialIdentity;
5
 
1 efrain 6
/**
7
 * Basic implementation of the AWS Credentials interface that allows callers to
8
 * pass in the AWS Access Key and AWS Secret Access Key in the constructor.
9
 */
1441 ariadna 10
class Credentials extends AwsCredentialIdentity implements
11
    CredentialsInterface,
12
    \Serializable
1 efrain 13
{
14
    private $key;
15
    private $secret;
16
    private $token;
17
    private $expires;
1441 ariadna 18
    private $accountId;
19
    private $source;
1 efrain 20
 
21
    /**
22
     * Constructs a new BasicAWSCredentials object, with the specified AWS
23
     * access key and AWS secret key
24
     *
25
     * @param string $key     AWS access key ID
26
     * @param string $secret  AWS secret access key
27
     * @param string $token   Security token to use
28
     * @param int    $expires UNIX timestamp for when credentials expire
29
     */
1441 ariadna 30
    public function __construct(
31
        $key,
32
        $secret,
33
        $token = null,
34
        $expires = null,
35
        $accountId = null,
36
        $source = CredentialSources::STATIC
37
    )
1 efrain 38
    {
1441 ariadna 39
        $this->key = trim((string) $key);
40
        $this->secret = trim((string) $secret);
1 efrain 41
        $this->token = $token;
42
        $this->expires = $expires;
1441 ariadna 43
        $this->accountId = $accountId;
44
        $this->source = $source ?? CredentialSources::STATIC;
1 efrain 45
    }
46
 
47
    public static function __set_state(array $state)
48
    {
49
        return new self(
50
            $state['key'],
51
            $state['secret'],
52
            $state['token'],
1441 ariadna 53
            $state['expires'],
54
            $state['accountId'],
55
            $state['source'] ?? null
1 efrain 56
        );
57
    }
58
 
59
    public function getAccessKeyId()
60
    {
61
        return $this->key;
62
    }
63
 
64
    public function getSecretKey()
65
    {
66
        return $this->secret;
67
    }
68
 
69
    public function getSecurityToken()
70
    {
71
        return $this->token;
72
    }
73
 
74
    public function getExpiration()
75
    {
76
        return $this->expires;
77
    }
78
 
79
    public function isExpired()
80
    {
81
        return $this->expires !== null && time() >= $this->expires;
82
    }
83
 
1441 ariadna 84
    public function getAccountId()
85
    {
86
        return $this->accountId;
87
    }
88
 
89
    public function getSource()
90
    {
91
        return $this->source;
92
    }
93
 
1 efrain 94
    public function toArray()
95
    {
96
        return [
97
            'key'     => $this->key,
98
            'secret'  => $this->secret,
99
            'token'   => $this->token,
1441 ariadna 100
            'expires' => $this->expires,
101
            'accountId' =>  $this->accountId,
102
            'source' => $this->source
1 efrain 103
        ];
104
    }
105
 
106
    public function serialize()
107
    {
108
        return json_encode($this->__serialize());
109
    }
110
 
111
    public function unserialize($serialized)
112
    {
113
        $data = json_decode($serialized, true);
114
 
115
        $this->__unserialize($data);
116
    }
117
 
118
    public function __serialize()
119
    {
120
        return $this->toArray();
121
    }
122
 
123
    public function __unserialize($data)
124
    {
125
        $this->key = $data['key'];
126
        $this->secret = $data['secret'];
127
        $this->token = $data['token'];
128
        $this->expires = $data['expires'];
1441 ariadna 129
        $this->accountId = $data['accountId'] ?? null;
130
        $this->source = $data['source'] ?? null;
1 efrain 131
    }
132
 
133
    /**
134
     * Internal-only. Used when IMDS is unreachable
135
     * or returns expires credentials.
136
     *
137
     * @internal
138
     */
139
    public function extendExpiration() {
140
        $extension = mt_rand(5, 10);
141
        $this->expires = time() + $extension * 60;
142
 
143
        $message = <<<EOT
144
Attempting credential expiration extension due to a credential service
145
availability issue. A refresh of these credentials will be attempted again
146
after {$extension} minutes.\n
147
EOT;
148
        trigger_error($message, E_USER_WARNING);
149
    }
150
}