Passed
Push — master ( e8a696...8de9f1 )
by Artem
12:01
created

Profile::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 1
ccs 0
cts 0
cp 0
crap 2
rs 10
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
     * Disables Two Factor Authentication for your User. Once successful,
96
     * login attempts from untrusted computers will only require a password
97
     * before being successful. This is less secure, and is discouraged.
98
     *
99
     * @throws LinodeException
100
     */
101 1
    public function disable2FA(): void
102
    {
103 1
        $this->client->api($this->client::REQUEST_POST, '/profile/tfa-disable');
104
    }
105
106
    /**
107
     * Generates a Two Factor secret for your User. TFA will not be enabled until you
108
     * have successfully confirmed the code you were given with `confirm2FA` (see below).
109
     * Once enabled, logins from untrusted computers will be required to provide
110
     * a TFA code before they are successful.
111
     *
112
     * @return TwoFactorSecret two Factor secret generated
113
     *
114
     * @throws LinodeException
115
     */
116 1
    public function enable2FA(): TwoFactorSecret
117
    {
118 1
        $response = $this->client->api($this->client::REQUEST_POST, '/profile/tfa-enable');
119 1
        $contents = $response->getBody()->getContents();
120 1
        $json     = json_decode($contents, true);
121
122 1
        return new TwoFactorSecret($this->client, $json);
123
    }
124
125
    /**
126
     * Confirms that you can successfully generate Two Factor codes and
127
     * enables TFA on your Account. Once this is complete, login attempts
128
     * from untrusted computers will be required to provide a Two Factor code
129
     * before they are successful.
130
     *
131
     * @param string $tfa_code The Two Factor code you generated with your Two Factor secret.
132
     *                         These codes are time-based, so be sure it is current.
133
     *
134
     * @return string A one-use code that can be used in place of your Two Factor
135
     *                code, in case you are unable to generate one. Keep this in
136
     *                a safe place to avoid being locked out of your Account.
137
     *
138
     * @throws LinodeException
139
     */
140 1
    public function confirm2FA(string $tfa_code): string
141
    {
142 1
        $parameters = [
143 1
            'tfa_code' => $tfa_code,
144 1
        ];
145
146 1
        $response = $this->client->api($this->client::REQUEST_POST, '/profile/tfa-enable-confirm', $parameters);
147 1
        $contents = $response->getBody()->getContents();
148 1
        $json     = json_decode($contents, true);
149
150 1
        return $json['scratch'];
151
    }
152
153
    /**
154
     * This returns a GrantsResponse describing what the acting User has been
155
     * granted access to. For unrestricted users, this will return a 204 and
156
     * no body because unrestricted users have access to everything without
157
     * grants. This will not return information about entities you do not have
158
     * access to. This endpoint is useful when writing third-party OAuth
159
     * applications to see what options you should present to the acting User.
160
     *
161
     * @throws LinodeException
162
     */
163 2
    public function getGrants(): ?UserGrant
164
    {
165 2
        $response = $this->client->api($this->client::REQUEST_GET, '/profile/grants');
166
167 2
        if (self::SUCCESS_NO_CONTENT === $response->getStatusCode()) {
168 1
            return null;
169
        }
170
171 1
        $contents = $response->getBody()->getContents();
172 1
        $json     = json_decode($contents, true);
173
174 1
        return new UserGrant($this->client, $json);
175
    }
176
}
177