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