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\Token;
3
 
1441 ariadna 4
use Aws\Identity\BearerTokenIdentity;
1 efrain 5
use Aws\Token\TokenInterface;
6
 
7
/**
8
 * Basic implementation of the AWS Token interface that allows callers to
9
 * pass in an AWS token in the constructor.
10
 */
1441 ariadna 11
class Token extends BearerTokenIdentity implements TokenInterface, \Serializable
1 efrain 12
{
13
    protected $token;
14
    protected $expires;
15
 
16
    /**
17
     * Constructs a new basic token object, with the specified AWS
18
     * token
19
     *
20
     * @param string $token   Security token to use
21
     * @param int    $expires UNIX timestamp for when the token expires
22
     */
23
    public function __construct($token, $expires = null)
24
    {
25
        $this->token = $token;
26
        $this->expires = $expires;
27
    }
28
 
29
    /**
30
     * Sets the state of a token object
31
     *
32
     * @param array $state   array containing 'token' and 'expires'
33
     */
34
    public static function __set_state(array $state)
35
    {
36
        return new self(
37
            $state['token'],
38
            $state['expires']
39
        );
40
    }
41
 
42
    /**
43
     * @return string
44
     */
45
    public function getToken()
46
    {
47
        return $this->token;
48
    }
49
 
50
    /**
51
     * @return int
52
     */
53
    public function getExpiration()
54
    {
55
        return $this->expires;
56
    }
57
 
58
    /**
59
     * @return bool
60
     */
61
    public function isExpired()
62
    {
63
        return $this->expires !== null && time() >= $this->expires;
64
    }
65
 
66
    /**
67
     * @return array
68
     */
69
    public function toArray()
70
    {
71
        return [
72
            'token'   => $this->token,
73
            'expires' => $this->expires
74
        ];
75
    }
76
 
77
    /**
78
     * @return string
79
     */
80
    public function serialize()
81
    {
82
        return json_encode($this->__serialize());
83
    }
84
 
85
    /**
86
     * Sets the state of the object from serialized json data
87
     */
88
    public function unserialize($serialized)
89
    {
90
        $data = json_decode($serialized, true);
91
 
92
        $this->__unserialize($data);
93
    }
94
 
95
    /**
96
     * @return array
97
     */
98
    public function __serialize()
99
    {
100
        return $this->toArray();
101
    }
102
 
103
    /**
104
     *  Sets the state of this object from an array
105
     */
106
    public function __unserialize($data)
107
    {
108
        $this->token = $data['token'];
109
        $this->expires = $data['expires'];
110
    }
111
}