| 1 |
<?php |
| 2 |
namespace Aws\Token; |
| 3 |
|
| 4 |
use Aws\Identity\BearerTokenIdentity; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
class Token extends BearerTokenIdentity implements TokenInterface, \Serializable |
| 11 |
{ |
| 12 |
protected $token; |
| 13 |
protected $expires; |
| 14 |
protected ?TokenSource $source; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public function __construct( |
| 24 |
$token, |
| 25 |
$expires = null, |
| 26 |
?TokenSource $source = null |
| 27 |
){ |
| 28 |
$this->token = $token; |
| 29 |
$this->expires = $expires; |
| 30 |
$this->source = $source; |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public static function __set_state(array $state) |
| 39 |
{ |
| 40 |
return new self( |
| 41 |
$state['token'], |
| 42 |
$state['expires'] |
| 43 |
); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public function getToken() |
| 50 |
{ |
| 51 |
return $this->token; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function getExpiration() |
| 58 |
{ |
| 59 |
return $this->expires; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public function getSource(): ?string |
| 66 |
{ |
| 67 |
return $this->source?->value; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public function isExpired() |
| 74 |
{ |
| 75 |
return $this->expires !== null && time() >= $this->expires; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public function toArray() |
| 82 |
{ |
| 83 |
return [ |
| 84 |
'token' => $this->token, |
| 85 |
'expires' => $this->expires, |
| 86 |
'source' => $this->source?->value |
| 87 |
]; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public function serialize() |
| 94 |
{ |
| 95 |
return json_encode($this->__serialize()); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public function unserialize($serialized) |
| 102 |
{ |
| 103 |
$data = json_decode($serialized, true); |
| 104 |
|
| 105 |
$this->__unserialize($data); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public function __serialize() |
| 112 |
{ |
| 113 |
return $this->toArray(); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public function __unserialize($data) |
| 120 |
{ |
| 121 |
$this->token = $data['token']; |
| 122 |
$this->expires = $data['expires']; |
| 123 |
$this->source = isset($data['source']) |
| 124 |
? TokenSource::from($data['source']) |
| 125 |
: null; |
| 126 |
} |
| 127 |
} |