1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace OpenStack\Identity\v3\Models; |
4
|
|
|
|
5
|
|
|
use OpenStack\Common\Resource\Alias; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
use OpenStack\Common\Resource\OperatorResource; |
8
|
|
|
use OpenStack\Common\Resource\Creatable; |
9
|
|
|
use OpenStack\Common\Resource\Retrievable; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @property \OpenStack\Identity\v3\Api $api |
13
|
|
|
*/ |
14
|
|
|
class Token extends OperatorResource implements Creatable, Retrievable, \OpenStack\Common\Auth\Token |
15
|
|
|
{ |
16
|
|
|
/** @var array */ |
17
|
|
|
public $methods; |
18
|
|
|
|
19
|
|
|
/** @var []Role */ |
20
|
|
|
public $roles; |
21
|
|
|
|
22
|
|
|
/** @var \DateTimeImmutable */ |
23
|
|
|
public $expires; |
24
|
|
|
|
25
|
|
|
/** @var Project */ |
26
|
|
|
public $project; |
27
|
|
|
|
28
|
|
|
/** @var Catalog */ |
29
|
|
|
public $catalog; |
30
|
|
|
|
31
|
|
|
/** @var mixed */ |
32
|
|
|
public $extras; |
33
|
|
|
|
34
|
|
|
/** @var User */ |
35
|
|
|
public $user; |
36
|
|
|
|
37
|
|
|
/** @var \DateTimeImmutable */ |
38
|
|
|
public $issued; |
39
|
|
|
|
40
|
|
|
/** @var string */ |
41
|
|
|
public $id; |
42
|
|
|
|
43
|
|
|
protected $resourceKey = 'token'; |
44
|
|
|
protected $resourcesKey = 'tokens'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritdoc |
48
|
|
|
*/ |
49
|
|
|
protected function getAliases(): array |
50
|
|
|
{ |
51
|
|
|
return parent::getAliases() + [ |
52
|
|
|
'roles' => new Alias('roles', Role::class, true), |
53
|
5 |
|
'expires_at' => new Alias('expires', \DateTimeImmutable::class), |
54
|
|
|
'project' => new Alias('project', Project::class), |
55
|
5 |
|
'catalog' => new Alias('catalog', Catalog::class), |
56
|
|
|
'user' => new Alias('user', User::class), |
57
|
5 |
|
'issued_at' => new Alias('issued', \DateTimeImmutable::class) |
58
|
|
|
]; |
59
|
5 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritDoc} |
63
|
|
|
*/ |
64
|
|
|
public function populateFromResponse(ResponseInterface $response) |
65
|
1 |
|
{ |
66
|
|
|
parent::populateFromResponse($response); |
67
|
1 |
|
$this->id = $response->getHeaderLine('X-Subject-Token'); |
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return string |
73
|
1 |
|
*/ |
74
|
|
|
public function getId(): string |
75
|
1 |
|
{ |
76
|
|
|
return $this->id; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return bool TRUE if the token has expired (and is invalid); FALSE otherwise. |
81
|
1 |
|
*/ |
82
|
|
|
public function hasExpired(): bool |
83
|
1 |
|
{ |
84
|
|
|
return $this->expires <= new \DateTimeImmutable('now', $this->expires->getTimezone()); |
85
|
1 |
|
} |
86
|
1 |
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritDoc} |
89
|
|
|
*/ |
90
|
|
|
public function retrieve() |
91
|
|
|
{ |
92
|
|
|
$response = $this->execute($this->api->getTokens(), ['tokenId' => $this->id]); |
93
|
10 |
|
$this->populateFromResponse($response); |
94
|
|
|
} |
95
|
10 |
|
|
96
|
4 |
|
/** |
97
|
4 |
|
* {@inheritDoc} |
98
|
1 |
|
* |
99
|
|
|
* @param array $data {@see \OpenStack\Identity\v3\Api::postTokens} |
100
|
|
|
*/ |
101
|
1 |
|
public function create(array $data): Creatable |
102
|
1 |
|
{ |
103
|
|
|
if (isset($data['user'])) { |
104
|
9 |
|
$data['methods'] = ['password']; |
105
|
1 |
|
if (!isset($data['user']['id']) && empty($data['user']['domain'])) { |
106
|
1 |
|
throw new \InvalidArgumentException( |
107
|
5 |
|
'When authenticating with a username, you must also provide either the domain name or domain ID to ' |
108
|
|
|
. 'which the user belongs to. Alternatively, if you provide a user ID instead, you do not need to ' |
109
|
|
|
. 'provide domain information.' |
110
|
4 |
|
); |
111
|
4 |
|
} |
112
|
|
|
} elseif (isset($data['tokenId'])) { |
113
|
|
|
$data['methods'] = ['token']; |
114
|
|
|
} else { |
115
|
|
|
throw new \InvalidArgumentException('Either a user or token must be provided.'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$response = $this->execute($this->api->postTokens(), $data); |
119
|
|
|
return $this->populateFromResponse($response); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|