Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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