|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
// --------------------------------------------------------------------- |
|
4
|
|
|
// |
|
5
|
|
|
// Copyright (C) 2018-2024 Artem Rodygin |
|
6
|
|
|
// |
|
7
|
|
|
// You should have received a copy of the MIT License along with |
|
8
|
|
|
// this file. If not, see <http://opensource.org/licenses/MIT>. |
|
9
|
|
|
// |
|
10
|
|
|
// --------------------------------------------------------------------- |
|
11
|
|
|
|
|
12
|
|
|
namespace Linode\Entity; |
|
13
|
|
|
|
|
14
|
|
|
use Linode\Entity\Account\UserGrant; |
|
15
|
|
|
use Linode\Entity\Profile\ProfileInformation; |
|
16
|
|
|
use Linode\Entity\Profile\TwoFactorSecret; |
|
17
|
|
|
use Linode\Exception\LinodeException; |
|
18
|
|
|
use Linode\Internal\Profile\AuthorizedAppRepository; |
|
19
|
|
|
use Linode\Internal\Profile\PersonalAccessTokenRepository; |
|
20
|
|
|
use Linode\Internal\Profile\SSHKeyRepository; |
|
21
|
|
|
use Linode\LinodeClient; |
|
22
|
|
|
use Linode\Repository\Profile as ProfileRepository; |
|
23
|
|
|
use Linode\Repository\RepositoryInterface; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* A Profile represents your User in our system. This is where you can change |
|
27
|
|
|
* information about your User. This information is available to any OAuth Client |
|
28
|
|
|
* regardless of requested scopes, and can be used to populate User information |
|
29
|
|
|
* in third-party applications. |
|
30
|
|
|
* |
|
31
|
|
|
* @property ProfileRepository\AuthorizedAppRepositoryInterface $apps |
|
32
|
|
|
* @property ProfileRepository\SSHKeyRepositoryInterface $ssh_keys |
|
33
|
|
|
* @property ProfileRepository\PersonalAccessTokenRepositoryInterface $tokens |
|
34
|
|
|
*/ |
|
35
|
|
|
class Profile |
|
36
|
|
|
{ |
|
37
|
|
|
protected const SUCCESS_NO_CONTENT = 204; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Profile constructor. |
|
41
|
|
|
* |
|
42
|
|
|
* @param LinodeClient $client linode API client |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(protected LinodeClient $client) {} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Returns requested repository. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $name repository name |
|
50
|
|
|
*/ |
|
51
|
1 |
|
public function __get(string $name): ?RepositoryInterface |
|
52
|
|
|
{ |
|
53
|
1 |
|
return match ($name) { |
|
54
|
1 |
|
'apps' => new AuthorizedAppRepository($this->client), |
|
55
|
1 |
|
'ssh_keys' => new SSHKeyRepository($this->client), |
|
56
|
1 |
|
'tokens' => new PersonalAccessTokenRepository($this->client), |
|
57
|
1 |
|
default => null, |
|
58
|
1 |
|
}; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Returns information about the current User. This can be used to see |
|
63
|
|
|
* who is acting in applications where more than one token is managed. For |
|
64
|
|
|
* example, in third-party OAuth applications. |
|
65
|
|
|
* |
|
66
|
|
|
* This endpoint is always accessible, no matter what OAuth scopes the acting token has. |
|
67
|
|
|
* |
|
68
|
|
|
* @throws LinodeException |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function getProfileInformation(): ProfileInformation |
|
71
|
|
|
{ |
|
72
|
1 |
|
$response = $this->client->api($this->client::REQUEST_GET, '/profile'); |
|
73
|
1 |
|
$contents = $response->getBody()->getContents(); |
|
74
|
1 |
|
$json = json_decode($contents, true); |
|
75
|
|
|
|
|
76
|
1 |
|
return new ProfileInformation($this->client, $json); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Update information in your Profile. This endpoint requires the |
|
81
|
|
|
* "account:read_write" OAuth Scope. |
|
82
|
|
|
* |
|
83
|
|
|
* @throws LinodeException |
|
84
|
|
|
*/ |
|
85
|
1 |
|
public function setProfileInformation(array $parameters): ProfileInformation |
|
86
|
|
|
{ |
|
87
|
1 |
|
$response = $this->client->api($this->client::REQUEST_PUT, '/profile', $parameters); |
|
88
|
1 |
|
$contents = $response->getBody()->getContents(); |
|
89
|
1 |
|
$json = json_decode($contents, true); |
|
90
|
|
|
|
|
91
|
1 |
|
return new ProfileInformation($this->client, $json); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* View a list of user preferences tied to the OAuth client that generated |
|
96
|
|
|
* the token making the request. The user preferences endpoints allow |
|
97
|
|
|
* consumers of the API to store arbitrary JSON data, such as a user's font |
|
98
|
|
|
* size preference or preferred display name. User preferences are available |
|
99
|
|
|
* for each OAuth client registered to your account, and as such an account can |
|
100
|
|
|
* have multiple user preferences. |
|
101
|
|
|
* |
|
102
|
|
|
* @return array a dictionary of user preferences |
|
103
|
|
|
* |
|
104
|
|
|
* @throws LinodeException |
|
105
|
|
|
*/ |
|
106
|
1 |
|
public function getProfilePreferences(): array |
|
107
|
|
|
{ |
|
108
|
1 |
|
$response = $this->client->api($this->client::REQUEST_GET, '/profile/preferences'); |
|
109
|
1 |
|
$contents = $response->getBody()->getContents(); |
|
110
|
|
|
|
|
111
|
1 |
|
return json_decode($contents, true); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Updates a user's preferences. These preferences are tied to the |
|
116
|
|
|
* OAuth client that generated the token making the request. The user |
|
117
|
|
|
* preferences endpoints allow consumers of the API to store arbitrary |
|
118
|
|
|
* JSON data, such as a user's font size preference or preferred display |
|
119
|
|
|
* name. An account may have multiple preferences. Preferences, and the |
|
120
|
|
|
* pertaining request body, may contain any arbitrary JSON data that |
|
121
|
|
|
* the user would like to store. |
|
122
|
|
|
* |
|
123
|
|
|
* @param array $preferences the user preferences to update or store |
|
124
|
|
|
* |
|
125
|
|
|
* @return array a dictionary of user preferences |
|
126
|
|
|
* |
|
127
|
|
|
* @throws LinodeException |
|
128
|
|
|
*/ |
|
129
|
1 |
|
public function setProfilePreferences(array $preferences): array |
|
130
|
|
|
{ |
|
131
|
1 |
|
$response = $this->client->api($this->client::REQUEST_PUT, '/profile/preferences', $preferences); |
|
132
|
1 |
|
$contents = $response->getBody()->getContents(); |
|
133
|
|
|
|
|
134
|
1 |
|
return json_decode($contents, true); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Disables Two Factor Authentication for your User. Once successful, |
|
139
|
|
|
* login attempts from untrusted computers will only require a password |
|
140
|
|
|
* before being successful. This is less secure, and is discouraged. |
|
141
|
|
|
* |
|
142
|
|
|
* @throws LinodeException |
|
143
|
|
|
*/ |
|
144
|
1 |
|
public function disable2FA(): void |
|
145
|
|
|
{ |
|
146
|
1 |
|
$this->client->api($this->client::REQUEST_POST, '/profile/tfa-disable'); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Generates a Two Factor secret for your User. TFA will not be enabled until you |
|
151
|
|
|
* have successfully confirmed the code you were given with `confirm2FA` (see below). |
|
152
|
|
|
* Once enabled, logins from untrusted computers will be required to provide |
|
153
|
|
|
* a TFA code before they are successful. |
|
154
|
|
|
* |
|
155
|
|
|
* @return TwoFactorSecret two Factor secret generated |
|
156
|
|
|
* |
|
157
|
|
|
* @throws LinodeException |
|
158
|
|
|
*/ |
|
159
|
1 |
|
public function enable2FA(): TwoFactorSecret |
|
160
|
|
|
{ |
|
161
|
1 |
|
$response = $this->client->api($this->client::REQUEST_POST, '/profile/tfa-enable'); |
|
162
|
1 |
|
$contents = $response->getBody()->getContents(); |
|
163
|
1 |
|
$json = json_decode($contents, true); |
|
164
|
|
|
|
|
165
|
1 |
|
return new TwoFactorSecret($this->client, $json); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Confirms that you can successfully generate Two Factor codes and |
|
170
|
|
|
* enables TFA on your Account. Once this is complete, login attempts |
|
171
|
|
|
* from untrusted computers will be required to provide a Two Factor code |
|
172
|
|
|
* before they are successful. |
|
173
|
|
|
* |
|
174
|
|
|
* @param string $tfa_code The Two Factor code you generated with your Two Factor secret. |
|
175
|
|
|
* These codes are time-based, so be sure it is current. |
|
176
|
|
|
* |
|
177
|
|
|
* @return string A one-use code that can be used in place of your Two Factor |
|
178
|
|
|
* code, in case you are unable to generate one. Keep this in |
|
179
|
|
|
* a safe place to avoid being locked out of your Account. |
|
180
|
|
|
* |
|
181
|
|
|
* @throws LinodeException |
|
182
|
|
|
*/ |
|
183
|
1 |
|
public function confirm2FA(string $tfa_code): string |
|
184
|
|
|
{ |
|
185
|
1 |
|
$parameters = [ |
|
186
|
1 |
|
'tfa_code' => $tfa_code, |
|
187
|
1 |
|
]; |
|
188
|
|
|
|
|
189
|
1 |
|
$response = $this->client->api($this->client::REQUEST_POST, '/profile/tfa-enable-confirm', $parameters); |
|
190
|
1 |
|
$contents = $response->getBody()->getContents(); |
|
191
|
1 |
|
$json = json_decode($contents, true); |
|
192
|
|
|
|
|
193
|
1 |
|
return $json['scratch']; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* This returns a GrantsResponse describing what the acting User has been |
|
198
|
|
|
* granted access to. For unrestricted users, this will return a 204 and |
|
199
|
|
|
* no body because unrestricted users have access to everything without |
|
200
|
|
|
* grants. This will not return information about entities you do not have |
|
201
|
|
|
* access to. This endpoint is useful when writing third-party OAuth |
|
202
|
|
|
* applications to see what options you should present to the acting User. |
|
203
|
|
|
* |
|
204
|
|
|
* @throws LinodeException |
|
205
|
|
|
*/ |
|
206
|
2 |
|
public function getGrants(): ?UserGrant |
|
207
|
|
|
{ |
|
208
|
2 |
|
$response = $this->client->api($this->client::REQUEST_GET, '/profile/grants'); |
|
209
|
|
|
|
|
210
|
2 |
|
if (self::SUCCESS_NO_CONTENT === $response->getStatusCode()) { |
|
211
|
1 |
|
return null; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
1 |
|
$contents = $response->getBody()->getContents(); |
|
215
|
1 |
|
$json = json_decode($contents, true); |
|
216
|
|
|
|
|
217
|
1 |
|
return new UserGrant($this->client, $json); |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|