7
7
8
8
namespace Magento \TwoFactorAuth \Test \Api ;
9
9
10
+ use Magento \Framework \HTTP \ClientInterface ;
11
+ use Magento \Framework \Serialize \SerializerInterface ;
12
+ use Magento \Framework \UrlInterface ;
10
13
use Magento \Framework \Webapi \Rest \Request ;
14
+ use Magento \Integration \Model \Oauth \TokenFactory ;
15
+ use Magento \Integration \Model \ResourceModel \Oauth \Token as TokenResource ;
11
16
use Magento \TestFramework \Helper \Bootstrap ;
12
17
use Magento \TestFramework \TestCase \WebapiAbstract ;
13
18
use Magento \TwoFactorAuth \Api \TfaInterface ;
14
19
use Magento \TwoFactorAuth \Model \Provider \Engine \Google ;
15
20
use Magento \User \Model \UserFactory ;
16
21
use OTPHP \TOTP ;
17
22
23
+ /**
24
+ * Class checks google authentication behaviour
25
+ */
18
26
class GoogleAuthenticateTest extends WebapiAbstract
19
27
{
20
28
const SERVICE_VERSION = 'V1 ' ;
@@ -37,18 +45,53 @@ class GoogleAuthenticateTest extends WebapiAbstract
37
45
*/
38
46
private $ tfa ;
39
47
48
+ /**
49
+ * @var ClientInterface
50
+ */
51
+ private $ client ;
52
+
53
+ /**
54
+ * @var UrlInterface
55
+ */
56
+ private $ url ;
57
+
58
+ /**
59
+ * @var SerializerInterface
60
+ */
61
+ private $ json ;
62
+
63
+ /**
64
+ * @var TokenResource
65
+ */
66
+ private $ tokenResource ;
67
+
68
+ /**
69
+ * @var TokenFactory
70
+ */
71
+ private $ tokenFactory ;
72
+
73
+ /**
74
+ * @inheritdoc
75
+ */
40
76
protected function setUp (): void
41
77
{
42
78
$ objectManager = Bootstrap::getObjectManager ();
43
79
$ this ->userFactory = $ objectManager ->get (UserFactory::class);
44
80
$ this ->google = $ objectManager ->get (Google::class);
45
81
$ this ->tfa = $ objectManager ->get (TfaInterface::class);
82
+ $ this ->client = $ objectManager ->get (ClientInterface::class);
83
+ $ this ->url = $ objectManager ->get (UrlInterface::class);
84
+ $ this ->json = $ objectManager ->get (SerializerInterface::class);
85
+ $ this ->tokenResource = $ objectManager ->get (TokenResource::class);
86
+ $ this ->tokenFactory = $ objectManager ->get (TokenFactory::class);
46
87
}
47
88
48
89
/**
49
90
* @magentoApiDataFixture Magento/User/_files/user_with_custom_role.php
91
+ *
92
+ * @return void
50
93
*/
51
- public function testInvalidCredentials ()
94
+ public function testInvalidCredentials (): void
52
95
{
53
96
$ serviceInfo = $ this ->buildServiceInfo ();
54
97
@@ -80,8 +123,10 @@ public function testInvalidCredentials()
80
123
/**
81
124
* @magentoConfigFixture twofactorauth/general/force_providers duo_security
82
125
* @magentoApiDataFixture Magento/User/_files/user_with_custom_role.php
126
+ *
127
+ * @return void
83
128
*/
84
- public function testUnavailableProvider ()
129
+ public function testUnavailableProvider (): void
85
130
{
86
131
$ serviceInfo = $ this ->buildServiceInfo ();
87
132
@@ -109,8 +154,10 @@ public function testUnavailableProvider()
109
154
/**
110
155
* @magentoConfigFixture twofactorauth/general/force_providers google
111
156
* @magentoApiDataFixture Magento/User/_files/user_with_custom_role.php
157
+ *
158
+ * @return void
112
159
*/
113
- public function testInvalidToken ()
160
+ public function testInvalidToken (): void
114
161
{
115
162
$ userId = $ this ->getUserId ();
116
163
$ serviceInfo = $ this ->buildServiceInfo ();
@@ -141,8 +188,10 @@ public function testInvalidToken()
141
188
/**
142
189
* @magentoConfigFixture twofactorauth/general/force_providers google
143
190
* @magentoApiDataFixture Magento/User/_files/user_with_custom_role.php
191
+ *
192
+ * @return void
144
193
*/
145
- public function testNotConfiguredProvider ()
194
+ public function testNotConfiguredProvider (): void
146
195
{
147
196
$ userId = $ this ->getUserId ();
148
197
$ serviceInfo = $ this ->buildServiceInfo ();
@@ -174,8 +223,10 @@ public function testNotConfiguredProvider()
174
223
* @magentoConfigFixture twofactorauth/general/force_providers google
175
224
* @magentoApiDataFixture Magento/User/_files/user_with_custom_role.php
176
225
* @magentoConfigFixture twofactorauth/google/otp_window 120
226
+ *
227
+ * @return void
177
228
*/
178
- public function testValidToken ()
229
+ public function testValidToken (): void
179
230
{
180
231
$ userId = $ this ->getUserId ();
181
232
$ otp = $ this ->getUserOtp ();
@@ -195,6 +246,37 @@ public function testValidToken()
195
246
self ::assertMatchesRegularExpression ('/^[a-z0-9]{32}$/ ' , $ response );
196
247
}
197
248
249
+ /**
250
+ * @magentoConfigFixture default/oauth/access_token_lifetime/admin 1
251
+ * @magentoConfigFixture twofactorauth/general/force_providers google
252
+ *
253
+ * @magentoApiDataFixture Magento/Webapi/_files/webapi_user.php
254
+ * @magentoApiDataFixture Magento/Customer/_files/customer.php
255
+ *
256
+ * @return void
257
+ */
258
+ public function testAdminTokenLifetime (): void
259
+ {
260
+ $ this ->_markTestAsRestOnly ();
261
+ $ this ->tfa ->getProviderByCode (Google::CODE )->activate ($ this ->getUserId ('webapi_user ' ));
262
+ $ otp = $ this ->getUserOtp ('webapi_user ' );
263
+ $ serviceInfo = $ this ->buildServiceInfo ();
264
+ $ requestData = [
265
+ 'otp ' => $ otp ,
266
+ 'username ' => 'webapi_user ' ,
267
+ 'password ' => \Magento \TestFramework \Bootstrap::ADMIN_PASSWORD ,
268
+ ];
269
+ $ accessToken = $ this ->_webApiCall ($ serviceInfo , $ requestData );
270
+ $ result = $ this ->doCustomerRequest ($ accessToken , 1 );
271
+ $ this ->assertContains ('customer@example.com ' , $ this ->json ->unserialize ($ result ));
272
+ $ this ->updateTokenCreatedTime ($ accessToken );
273
+ $ result = $ this ->doCustomerRequest ($ accessToken , 1 );
274
+ $ this ->assertContains (
275
+ 'The consumer isn \'t authorized to access %resources. ' ,
276
+ $ this ->json ->unserialize ($ result )
277
+ );
278
+ }
279
+
198
280
/**
199
281
* @return array
200
282
*/
@@ -217,20 +299,61 @@ private function buildServiceInfo(): array
217
299
];
218
300
}
219
301
220
- private function getUserId (): int
302
+ /**
303
+ * Get user id
304
+ *
305
+ * @param string $userName
306
+ * @return int
307
+ */
308
+ private function getUserId ($ userName = 'customRoleUser ' ): int
221
309
{
222
310
$ user = $ this ->userFactory ->create ();
223
- $ user ->loadByUsername (' customRoleUser ' );
311
+ $ user ->loadByUsername ($ userName );
224
312
225
313
return (int )$ user ->getId ();
226
314
}
227
315
228
- private function getUserOtp (): string
316
+ /**
317
+ * Get user otp
318
+ *
319
+ * @param string $userName
320
+ * @return string
321
+ */
322
+ private function getUserOtp ($ userName = 'customRoleUser ' ): string
229
323
{
230
324
$ user = $ this ->userFactory ->create ();
231
- $ user ->loadByUsername (' customRoleUser ' );
325
+ $ user ->loadByUsername ($ userName );
232
326
$ totp = TOTP ::create ($ this ->google ->getSecretCode ($ user ));
233
327
234
328
return $ totp ->now ();
235
329
}
330
+
331
+ /**
332
+ * Perform request to customers endpoint
333
+ *
334
+ * @param string $accessToken
335
+ * @return string
336
+ */
337
+ private function doCustomerRequest (string $ accessToken , $ customerId ): string
338
+ {
339
+ $ this ->client ->addHeader ('Authorization ' , 'Bearer ' . $ accessToken );
340
+ $ this ->client ->get ($ this ->url ->getBaseUrl () . 'rest/V1/customers/ ' . $ customerId );
341
+
342
+ return $ this ->client ->getBody ();
343
+ }
344
+
345
+ /**
346
+ * Update token created time
347
+ *
348
+ * @param string $accessToken
349
+ * @return void
350
+ */
351
+ private function updateTokenCreatedTime (string $ accessToken ): void
352
+ {
353
+ $ token = $ this ->tokenFactory ->create ();
354
+ $ token ->loadByToken ($ accessToken );
355
+ $ createdAt = (new \DateTime ('-1 day ' ))->format ('Y-m-d H:i:s ' );
356
+ $ token ->setCreatedAt ($ createdAt );
357
+ $ this ->tokenResource ->save ($ token );
358
+ }
236
359
}
0 commit comments