Skip to content

Commit 3190144

Browse files
committed
Added MFA method resource and endpoint
1 parent 8473b52 commit 3190144

4 files changed

Lines changed: 376 additions & 0 deletions

File tree

src/Endpoints/Users/UserEndpoint.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ class UserEndpoint extends CollectionEndpointAbstract
2727
*/
2828
public UserLoginEndpoint $logins;
2929

30+
/**
31+
* RESTful UserMfaMethod resource.
32+
*
33+
* @var UserMfaMethodEndpoint
34+
*/
35+
public UserMfaMethodEndpoint $mfa;
36+
3037
/**
3138
* RESTful SocialProviderUser resource.
3239
*
@@ -54,6 +61,7 @@ public function __construct(OminityApiClient $client)
5461

5562
$this->customers = new UserCustomerEndpoint($client);
5663
$this->logins = new UserLoginEndpoint($client);
64+
$this->mfa = new UserMfaMethodEndpoint($client);
5765
$this->oauthaccounts = new UserOauthAccountEndpoint($client);
5866
$this->passwordreset = new UserPasswordResetEndpoint($client);
5967
$this->recoverycodes = new UserRecoveryCodeEndpoint($client);
Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
<?php
2+
3+
namespace Ominity\Api\Endpoints\Users;
4+
5+
use Ominity\Api\Endpoints\CollectionEndpointAbstract;
6+
use Ominity\Api\Exceptions\ApiException;
7+
use Ominity\Api\Resources\ResourceFactory;
8+
use Ominity\Api\Resources\Users\User;
9+
use Ominity\Api\Resources\Users\UserMfaMethod;
10+
use Ominity\Api\Resources\Users\UserMfaMethodCollection;
11+
12+
class UserMfaMethodEndpoint extends CollectionEndpointAbstract
13+
{
14+
/**
15+
* @var string
16+
*/
17+
protected $resourcePath = "users/{userId}/mfa-methods";
18+
19+
/**
20+
* @inheritDoc
21+
*/
22+
protected function getResourceCollectionObject($count, $_links)
23+
{
24+
return new UserMfaMethodCollection($this->client, $count, $_links);
25+
}
26+
27+
/**
28+
* @inheritDoc
29+
*/
30+
protected function getResourceObject()
31+
{
32+
return new UserMfaMethod($this->client);
33+
}
34+
35+
/**
36+
* Enable a specific mfa method for a specific User.
37+
*
38+
* @param User $user
39+
* @param string $method
40+
* @param array $data
41+
* @param array $filters
42+
*
43+
* @return UserMfaMethodCollection
44+
* @throws \Ominity\Api\Exceptions\ApiException
45+
*/
46+
public function enableFor(User $user, string $method, array $data = [], array $filters = [])
47+
{
48+
return $this->enableForId($user->id, $method, $data, $filters);
49+
}
50+
51+
/**
52+
* Enable a specific mfa method for a specific User ID.
53+
*
54+
* @param int $userId
55+
* @param array $data
56+
* @param array $filters
57+
*
58+
* @return UserMfaMethodCollection
59+
* @throws \Ominity\Api\Exceptions\ApiException
60+
*/
61+
public function enableForId($userId, string $method, array $data = [], array $filters = [])
62+
{
63+
$this->setPathVariables(['userId' => $userId]);
64+
65+
$result = $this->client->performHttpCall(
66+
self::REST_CREATE,
67+
$this->getResourcePath() . '/' . $method . $this->buildQueryString($filters),
68+
$this->parseRequestBody($data)
69+
);
70+
71+
return ResourceFactory::createFromApiResult($result, $this->getResourceObject());
72+
}
73+
74+
/**
75+
* Get the mfa method for a specific User.
76+
*
77+
* @param User $user
78+
* @param string $method
79+
* @return UserMfaMethod
80+
*
81+
* @throws \Ominity\Api\Exceptions\ApiException
82+
*/
83+
public function getFor(User $user, string $method, array $parameters = []) {
84+
if (empty($user)) {
85+
throw new ApiException("User is empty.");
86+
}
87+
88+
if (empty($method)) {
89+
throw new ApiException("Method is empty.");
90+
}
91+
92+
return $this->getForId($user->id, $method, $parameters);
93+
}
94+
95+
/**
96+
* Get the mfa method for a specific User ID.
97+
*
98+
* @param int $userId
99+
* @param string $method
100+
* @return UserMfaMethod
101+
*
102+
* @throws \Ominity\Api\Exceptions\ApiException
103+
*/
104+
public function getForId(int $userId, string $method, array $parameters = []) {
105+
if (empty($userId)) {
106+
throw new ApiException("User ID is empty.");
107+
}
108+
109+
if (empty($method)) {
110+
throw new ApiException("Method is empty.");
111+
}
112+
113+
$this->setPathVariables(['userId' => $userId]);
114+
return parent::rest_read($method, $parameters);
115+
}
116+
117+
/**
118+
* List the mfa methods for a specific User.
119+
*
120+
* @param User $user
121+
* @param array $parameters
122+
* @return UserMfaMethodCollection
123+
*
124+
* @throws \Ominity\Api\Exceptions\ApiException
125+
*/
126+
public function listFor(User $user, array $parameters = [])
127+
{
128+
return $this->listForId($user->id, $parameters);
129+
}
130+
131+
/**
132+
* List the mfa methods for a specific User ID.
133+
*
134+
* @param int $userId
135+
* @param array $parameters
136+
* @return UserMfaMethodCollection
137+
*
138+
* @throws \Ominity\Api\Exceptions\ApiException
139+
*/
140+
public function listForId(int $userId, array $parameters = [])
141+
{
142+
$this->setPathVariables(['userId' => $userId]);
143+
144+
return parent::rest_list(null, null, $parameters);
145+
}
146+
147+
/**
148+
* Validate MFA code for a specific User.
149+
*
150+
* @param User $user
151+
* @param string $method
152+
* @param array $data
153+
* @param array $filters
154+
*
155+
* @return bool
156+
* @throws \Ominity\Api\Exceptions\ApiException
157+
*/
158+
public function validateFor(User $user, string $method, array $data, array $filters = [])
159+
{
160+
return $this->validateForId($user->id, $method, $data, $filters);
161+
}
162+
163+
/**
164+
* Validate MFA code for a specific User ID.
165+
*
166+
* @param int $userId
167+
* @param string $method
168+
* @param array $data
169+
* @param array $filters
170+
*
171+
* @return bool
172+
* @throws \Ominity\Api\Exceptions\ApiException
173+
*/
174+
public function validateForId($userId, string $method, array $data, array $filters = [])
175+
{
176+
$this->setPathVariables(['userId' => $userId]);
177+
178+
$result = $this->client->performHttpCall(
179+
self::REST_CREATE,
180+
$this->getResourcePath() . '/' . $method . '/validate' . $this->buildQueryString($filters),
181+
$this->parseRequestBody($data)
182+
);
183+
184+
return $result->success ?? false;
185+
}
186+
187+
/**
188+
* Disable the given mfa method for a specific User.
189+
*
190+
* Will throw a ApiException if the method is invalid or the resource cannot be found.
191+
* Returns with HTTP status No Content (204) if successful.
192+
*
193+
* @param User $user
194+
* @param string $method
195+
* @param array $data
196+
* @return UserMfaMethod
197+
* @throws ApiException
198+
*/
199+
public function disableFor(User $user, string $method, array $data = [])
200+
{
201+
return $this->disableForId($user->id, $method, $data);
202+
}
203+
204+
/**
205+
* Disables the given mfa method for a specific User ID.
206+
*
207+
* Will throw a ApiException if the method is invalid or the resource cannot be found.
208+
* Returns with HTTP status No Content (204) if successful.
209+
*
210+
* @param int $userId
211+
* @param string $method
212+
* @param array $data
213+
* @return UserMfaMethod
214+
* @throws ApiException
215+
*/
216+
public function disableForId(int $userId, string $method, array $data = [])
217+
{
218+
$this->setPathVariables(['userId' => $userId]);
219+
220+
return $this->rest_delete($method, $data);
221+
}
222+
223+
/**
224+
* Send MFA code for a specific User.
225+
*
226+
* @param User $user
227+
* @param string $method
228+
* @param array $data
229+
* @param array $filters
230+
*
231+
* @return bool
232+
* @throws \Ominity\Api\Exceptions\ApiException
233+
*/
234+
public function sendFor(User $user, string $method, array $data, array $filters = [])
235+
{
236+
return $this->validateForId($user->id, $method, $data, $filters);
237+
}
238+
239+
/**
240+
* Send MFA code for a specific User ID.
241+
*
242+
* @param int $userId
243+
* @param string $method
244+
* @param array $data
245+
* @param array $filters
246+
*
247+
* @return bool
248+
* @throws \Ominity\Api\Exceptions\ApiException
249+
*/
250+
public function sendForId($userId, string $method, array $data, array $filters = [])
251+
{
252+
$this->setPathVariables(['userId' => $userId]);
253+
254+
$result = $this->client->performHttpCall(
255+
self::REST_CREATE,
256+
$this->getResourcePath() . '/' . $method . '/send' . $this->buildQueryString($filters),
257+
$this->parseRequestBody($data)
258+
);
259+
260+
return $result->success ?? false;
261+
}
262+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace Ominity\Api\Resources\Users;
4+
5+
use Ominity\Api\Resources\BaseResource;
6+
7+
class UserMfaMethod extends BaseResource
8+
{
9+
/**
10+
* Always 'user_mfa_method'
11+
*
12+
* @var string
13+
*/
14+
public $resource;
15+
16+
/**
17+
* Id of the user.
18+
*
19+
* @var int
20+
*/
21+
public $userId;
22+
23+
/**
24+
* The MFA method.
25+
*
26+
* @var string
27+
*/
28+
public $method;
29+
30+
/**
31+
* Is this MFA method enabled?
32+
*
33+
* @var bool
34+
*/
35+
public $isEnabled;
36+
37+
/**
38+
* UTC datetime the mfa method was verified in ISO-8601 format.
39+
*
40+
* @example "2013-12-25T10:30:54+00:00"
41+
* @var string|null
42+
*/
43+
public $verifiedAt;
44+
45+
/**
46+
* UTC datetime the mfa method was last used in ISO-8601 format.
47+
*
48+
* @example "2013-12-25T10:30:54+00:00"
49+
* @var string|null
50+
*/
51+
public $lastUsedAt;
52+
53+
/**
54+
* UTC datetime the mfa method was last sent in ISO-8601 format.
55+
*
56+
* @example "2013-12-25T10:30:54+00:00"
57+
* @var string|null
58+
*/
59+
public $lastSentAt;
60+
61+
/**
62+
* The details of the MFA method.
63+
*
64+
* @var \stdClass
65+
*/
66+
public $details;
67+
68+
/**
69+
* @var \stdClass
70+
*/
71+
public $_links;
72+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Ominity\Api\Resources\Users;
4+
5+
use Ominity\Api\Resources\BaseCollection;
6+
7+
class UserMfaMethodCollection extends BaseCollection
8+
{
9+
/**
10+
* @return string|null
11+
*/
12+
public function getCollectionResourceName()
13+
{
14+
return 'user_mfa_methods';
15+
}
16+
17+
/**
18+
* Get a specific mfa method.
19+
* Returns null if the mfa method cannot be found.
20+
*
21+
* @param string $method
22+
* @return UserMfaMethod|null
23+
*/
24+
public function get($method)
25+
{
26+
foreach ($this as $mfa) {
27+
if ($mfa->method == $method) {
28+
return $mfa;
29+
}
30+
}
31+
32+
return null;
33+
}
34+
}

0 commit comments

Comments
 (0)